-
Notifications
You must be signed in to change notification settings - Fork 151
/
do.go
75 lines (58 loc) · 2.14 KB
/
do.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"context"
"fmt"
"log"
"time"
)
func do(duration time.Duration, eventsPerSecond int, delay time.Duration, wait time.Duration, concurrency int, workerDelay time.Duration) error {
l.Info().Msgf("testing with duration=%s, eventsPerSecond=%d, delay=%s, wait=%s, concurrency=%d", duration, eventsPerSecond, delay, wait, concurrency)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
after := 10 * time.Second
go func() {
time.Sleep(duration + after + wait + 5*time.Second)
cancel()
}()
ch := make(chan int64, 2)
durations := make(chan time.Duration, eventsPerSecond*int(duration.Seconds())*3)
go func() {
if workerDelay.Seconds() > 0 {
l.Info().Msgf("wait %s before starting the worker", workerDelay)
time.Sleep(workerDelay)
}
l.Info().Msg("starting worker now")
count, uniques := run(ctx, delay, durations, concurrency)
ch <- count
ch <- uniques
}()
time.Sleep(after)
scheduled := make(chan time.Duration, eventsPerSecond*int(duration.Seconds())*2)
emitted := emit(ctx, eventsPerSecond, duration, scheduled)
executed := <-ch
uniques := <-ch
l.Info().Msgf("emitted %d, executed %d, uniques %d, using %d events/s", emitted, executed, uniques, eventsPerSecond)
if executed == 0 {
return fmt.Errorf("❌ no events executed")
}
var totalDurationExecuted time.Duration
for i := 0; i < int(executed); i++ {
totalDurationExecuted += <-durations
}
durationPerEventExecuted := totalDurationExecuted / time.Duration(executed)
log.Printf("ℹ️ average duration per executed event: %s", durationPerEventExecuted)
var totalDurationScheduled time.Duration
for i := 0; i < int(emitted); i++ {
totalDurationScheduled += <-scheduled
}
scheduleTimePerEvent := totalDurationScheduled / time.Duration(emitted)
log.Printf("ℹ️ average scheduling time per event: %s", scheduleTimePerEvent)
if emitted != executed {
log.Printf("⚠️ warning: emitted and executed counts do not match: %d != %d", emitted, executed)
}
if emitted != uniques {
return fmt.Errorf("❌ emitted and unique executed counts do not match: %d != %d", emitted, uniques)
}
log.Printf("✅ success")
return nil
}