Skip to content

Commit eb24b39

Browse files
committed
Make random example more interesting.
Have different RPC rates for the three services and vary them according to a diurnal period.
1 parent 300983d commit eb24b39

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

examples/random/main.go

+26-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818

1919
import (
2020
"flag"
21+
"math"
2122
"math/rand"
2223
"net/http"
2324
"time"
@@ -26,10 +27,11 @@ import (
2627
)
2728

2829
var (
29-
addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
30-
uniformDomain = flag.Float64("random.uniform.domain", 200, "The domain for the uniform distribution.")
31-
normDomain = flag.Float64("random.exponential.domain", 200, "The domain for the normal distribution.")
32-
normMean = flag.Float64("random.exponential.mean", 10, "The mean for the normal distribution.")
30+
addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
31+
uniformDomain = flag.Float64("uniform.domain", 200, "The domain for the uniform distribution.")
32+
normDomain = flag.Float64("exponential.domain", 200, "The domain for the normal distribution.")
33+
normMean = flag.Float64("exponential.mean", 10, "The mean for the normal distribution.")
34+
oscillationPeriod = flag.Duration("oscillation-period", 10*time.Minute, "The duration of the rate oscillation period.")
3335
)
3436

3537
var (
@@ -53,14 +55,31 @@ func init() {
5355
func main() {
5456
flag.Parse()
5557

58+
start := time.Now()
59+
60+
oscillationFactor := func() float64 {
61+
return 2 + math.Sin(math.Sin(2*math.Pi*float64(time.Since(start))/float64(*oscillationPeriod)))
62+
}
63+
64+
// Periodically record some sample latencies for the three services.
5665
go func() {
5766
for {
58-
// Periodically record some sample latencies for the three services.
5967
rpcDurations.WithLabelValues("uniform").Observe(rand.Float64() * *uniformDomain)
68+
time.Sleep(time.Duration(100*oscillationFactor()) * time.Millisecond)
69+
}
70+
}()
71+
72+
go func() {
73+
for {
6074
rpcDurations.WithLabelValues("normal").Observe((rand.NormFloat64() * *normDomain) + *normMean)
61-
rpcDurations.WithLabelValues("exponential").Observe(rand.ExpFloat64())
75+
time.Sleep(time.Duration(75*oscillationFactor()) * time.Millisecond)
76+
}
77+
}()
6278

63-
time.Sleep(100 * time.Millisecond)
79+
go func() {
80+
for {
81+
rpcDurations.WithLabelValues("exponential").Observe(rand.ExpFloat64())
82+
time.Sleep(time.Duration(50*oscillationFactor()) * time.Millisecond)
6483
}
6584
}()
6685

0 commit comments

Comments
 (0)