Skip to content

hrtime.Since() sometimes returns negative durations #14

@macsmol

Description

@macsmol

I was running some benchmarks and started seeing impossible results (negative MB/s speeds). So upon some debugging I've found out that some of the time samples collected are negative values (where typical values could be 14-digit decimal number so it cannot be an int64 overflow).
Here is relevant code sample:

type benchmarkResult struct {
  name           string
  packTimings    []time.Duration
  unpackTimings  []time.Duration
  packInputSize  int64
  packOutputSize int64
}
//... more code...
benchResult := newBenchmarkResult(e.Name())
benchResults = append(benchResults, benchResult)
//... more code...

for i := 0; i < samples_count; i++ {
  var start time.Duration
  var elapsed time.Duration
  
  start = hrtime.Now()                                                   
  // code that I benchmark
  benchResult.packOutputSize = benchmarkee.packBuffer(packedStage2Buff,  
  	inputBuff[:benchResult.packInputSize])
  //------------- elapsed is sometimes negative for no apparent reason --------------------
  elapsed = hrtime.Since(start)                                          
  benchResult.packTimings = append(benchResult.packTimings, elapsed)
  
  //... more code...
}
//... more code...

and here is an example content of benchResult.packTimings after running this loop:

[]time.Duration len: 10, cap: 10, [
13056411500,
12958258400,
13248242400,
12969406300,
13192194100,
12935541500,
-1831576101470, // <- obviously a bad measurement
13263809300,
13024965000,
13255804100]

I use: github.com/loov/hrtime v1.0.3
I'm running this on go version go1.21.5 windows/amd64
My OS is:
Windows 10 Pro
Version 22H2
OS Build 19045.6456

If you need I can send you the code+data that produced this bug. Just let me know.
I hope you can look into this. Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions