forked from alexandrevicenzi/go-sse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
108 lines (78 loc) · 1.55 KB
/
bench_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package sse
import (
"fmt"
"net/http"
"sync"
"testing"
)
type mockResponseWriter struct {
c chan bool
}
func (m *mockResponseWriter) Header() (h http.Header) {
return http.Header{}
}
func (m *mockResponseWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}
func (m *mockResponseWriter) WriteString(s string) (n int, err error) {
return len(s), nil
}
func (m *mockResponseWriter) WriteHeader(int) {}
func (m *mockResponseWriter) Flush() {}
func (m *mockResponseWriter) CloseNotify() <-chan bool {
return m.c
}
func (m *mockResponseWriter) Close() {
m.c <- true
}
func BenchmarkServeHTTP(b *testing.B) {
srv := NewServer(&Options{
Logger: nil,
})
defer srv.Shutdown()
req, _ := http.NewRequest("GET", "/channel-name", nil)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
res := &mockResponseWriter{
make(chan bool),
}
go func() {
res.Close()
}()
srv.ServeHTTP(res, req)
}
}
func BenchmarkSendMessage(b *testing.B) {
srv := NewServer(&Options{
Logger: nil,
})
defer srv.Shutdown()
for n := 0; n < 10; n++ {
srv.addChannel(fmt.Sprintf("CH-%d", n+1))
}
wgReg := sync.WaitGroup{}
wgCh := sync.WaitGroup{}
for n := 0; n < 10; n++ {
for name, ch := range srv.channels {
wgReg.Add(1)
wgCh.Add(1)
go func() {
c := newClient("", name)
ch.addClient(c)
wgReg.Done()
for range c.send {
}
wgCh.Done()
}()
}
}
wgReg.Wait()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
srv.SendMessage("", SimpleMessage("hello"))
}
srv.close()
wgCh.Wait()
}