-
Notifications
You must be signed in to change notification settings - Fork 0
/
infinite_channel_benchmark_test.go
78 lines (68 loc) · 1.72 KB
/
infinite_channel_benchmark_test.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
76
77
78
package main
import (
"fmt"
"log"
"os"
"sync"
"testing"
"time"
)
func writeResultsToFile(fileName string, data map[string]int) {
file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatalf("failed to open file: %s", err)
}
defer file.Close()
// writing the header
_, err = file.WriteString("Size,MessagesToSend,DroppedMessages\n")
if err != nil {
log.Fatalf("failed to write to file: %s", err)
}
for key, value := range data {
line := fmt.Sprintf("%s,%d\n", key, value)
_, err := file.WriteString(line)
if err != nil {
log.Fatalf("failed to write to file: %s", err)
}
}
}
func runBenchmark(size, messagesToSend int, results map[string]int, mu *sync.Mutex) {
dm := 0
for i := 0; i < 100; i++ {
ic := NewInfiniteChannel[*int](size, 0)
// generate messages
for j := 0; j < messagesToSend; j++ {
if added := ic.Add(&j); !added {
dm++
}
time.Sleep(10 * time.Microsecond)
}
ic.ForceStop()
}
avgDroppedMessages := dm / 100
key := fmt.Sprintf("%d,%d", size, messagesToSend)
mu.Lock()
results[key] = avgDroppedMessages
mu.Unlock()
}
func BenchmarkInfiniteChannel(b *testing.B) {
results := make(map[string]int)
var mu sync.Mutex
for size := 0; size <= 8; size += 1 {
for messagesToSend := 64; messagesToSend <= 512; messagesToSend += 64 {
size := size
k := messagesToSend
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
runBenchmark(size, k, results, &mu)
}
})
fmt.Printf("benchmark for size %d and messages to send %d completed\n", size, messagesToSend)
}
}
writeResultsToFile("benchmark_results.csv", results)
}
// func main() {
// b := testing.Benchmark(BenchmarkInfiniteChannel)
// fmt.Println(b)
// }