Skip to content

Commit 314218f

Browse files
committed
Add a rand instance for the generator
Signed-off-by: Hongyu <[email protected]>
1 parent 218062e commit 314218f

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

cmd/load.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ var (
3535
rpsSlot = flag.Int("slot", 1, "Time slot in minutes for each RPS in the `stress` mode")
3636
rpsStep = flag.Int("step", 1, "Step size for increasing RPS in the `stress` mode")
3737

38+
seed = flag.Int64("seed", 42, "Random seed for the generator")
39+
3840
// withWarmup = flag.Int("withWarmup", -1000, "Duration of the withWarmup")
3941
withWarmup = flag.Bool("warmup", false, "Enable warmup")
4042
)
@@ -64,6 +66,8 @@ func init() {
6466
}
6567

6668
func main() {
69+
gen.InitSeed(*seed)
70+
6771
switch *mode {
6872
case "trace":
6973
runTraceMode()

pkg/generate/generate.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ const (
2626
// rand.Seed(time.Now().UnixNano())
2727
// }
2828

29-
func GenerateInterarrivalTimesInMicro(seed int, invocationsPerMinute int, uniform bool) []float64 {
30-
rand.Seed(int64(seed)) //! Fix randomness.
29+
var iatRand *rand.Rand
30+
31+
func InitSeed(s int64) {
32+
iatRand = rand.New(rand.NewSource(s))
33+
}
34+
35+
func GenerateInterarrivalTimesInMicro(invocationsPerMinute int, uniform bool) []float64 {
3136
oneSecondInMicro := 1000_000.0
3237
oneMinuteInMicro := 60*oneSecondInMicro - 1000
3338

@@ -40,7 +45,7 @@ func GenerateInterarrivalTimesInMicro(seed int, invocationsPerMinute int, unifor
4045
if uniform {
4146
iat = oneSecondInMicro / rps
4247
} else {
43-
iat = rand.ExpFloat64() / rps * oneSecondInMicro
48+
iat = iatRand.ExpFloat64() / rps * oneSecondInMicro
4449
}
4550
//* Only guarantee microsecond-level ganularity.
4651
if iat < 1 {
@@ -104,7 +109,6 @@ func GenerateStressLoads(rpsStart int, rpsStep int, stressSlotInMinutes int, fun
104109
stress_generation:
105110
for {
106111
iats := GenerateInterarrivalTimesInMicro(
107-
999, //! Fix randomness.
108112
rps*60,
109113
true,
110114
)
@@ -230,7 +234,6 @@ trace_generation:
230234
var invocationCount int32
231235

232236
iats = GenerateInterarrivalTimesInMicro(
233-
minute, //! Fix randomness.
234237
numInvocatonsThisMinute,
235238
isFixedRate,
236239
)

pkg/test/generate_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ func TestGenCheckOverload(t *testing.T) {
2525
}
2626

2727
func TestGenerateIat(t *testing.T) {
28+
gen.InitSeed(42)
29+
2830
invocationsPerMinute := 20_000
29-
iats := gen.GenerateInterarrivalTimesInMicro(0, invocationsPerMinute, true)
31+
iats := gen.GenerateInterarrivalTimesInMicro(invocationsPerMinute, true)
3032
duration, _ := stats.Sum(stats.LoadRawData(iats))
3133

3234
assert.Equal(t, iats[rand.Intn(len(iats))], iats[rand.Intn(len(iats))])
@@ -37,7 +39,7 @@ func TestGenerateIat(t *testing.T) {
3739
assert.GreaterOrEqual(t, iat, 1.0)
3840
}
3941

40-
iats = gen.GenerateInterarrivalTimesInMicro(0, invocationsPerMinute, false)
42+
iats = gen.GenerateInterarrivalTimesInMicro(invocationsPerMinute, false)
4143
duration, _ = stats.Sum(stats.LoadRawData(iats))
4244

4345
assert.Equal(t, invocationsPerMinute, len(iats))

0 commit comments

Comments
 (0)