Skip to content

Commit

Permalink
Fix max number of samples in experiments on non-64-bit systems.
Browse files Browse the repository at this point in the history
The use of `math.MaxInt64` as the maximum number of samples in the context
of general int variables can lead to inconsistent behavior on non-64-bit
systems.

This patch addresses this using the arbitrary-precision `math/big.Int`
type to store and compare sampling numbers to ensure no
architecture-dependant int size mismatches will occur.

Signed-off-by: Nashwan Azhari <nazhari@cloudbasesolutions.com>
  • Loading branch information
aznashwan committed Mar 21, 2022
1 parent c8ba582 commit 13b6c1e
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions gmeasure/experiment.go
Expand Up @@ -40,6 +40,7 @@ package gmeasure
import (
"fmt"
"math"
"math/big"
"reflect"
"sync"
"time"
Expand Down Expand Up @@ -452,9 +453,11 @@ func (e *Experiment) Sample(callback func(idx int), samplingConfig SamplingConfi
if samplingConfig.Duration > 0 {
maxTime = time.Now().Add(samplingConfig.Duration)
}
maxN := math.MaxInt64
maxN := big.NewInt(math.MaxInt64)
if samplingConfig.N > 0 {
maxN = samplingConfig.N
// NOTE(aznashwan): we print then parse the samplingConfig.N to future-proof
// against variations in the bit size of the standard int type:
maxN.SetString(fmt.Sprintf("%d", samplingConfig.N), 10)
}
numParallel := 1
if samplingConfig.NumParallel > numParallel {
Expand Down Expand Up @@ -491,7 +494,12 @@ func (e *Experiment) Sample(callback func(idx int), samplingConfig SamplingConfi
avgDt = (avgDt*time.Duration(idx-numParallel) + dt) / time.Duration(idx-numParallel+1)
}
idx += 1
if idx >= maxN {

idxBigInt := big.NewInt(0)
// NOTE(aznashwan): we print then parse the integer idx to future-proof
// against variations in the bit size of the standard int type:
idxBigInt.SetString(fmt.Sprintf("%d", idx), 10)
if idxBigInt.Cmp(maxN) >= 0 {
return
}
if time.Now().Add(avgDt).After(maxTime) {
Expand Down

0 comments on commit 13b6c1e

Please sign in to comment.