|
| 1 | +// Optional hub load benchmarks for maintainers (not run by plain go test ./...). |
| 2 | +// They use in-memory SQLite, a real Hub with plugin manager, synthetic Clients |
| 3 | +// (nil conn, large send buffers) and TypingMessage to avoid plugin IPC on broadcast. |
| 4 | +// |
| 5 | +// Run (from repo root): |
| 6 | +// |
| 7 | +// go test ./server -run=^$ -bench=Loadverify -benchmem -count=5 | tee loadverify-bench.txt |
| 8 | +// |
| 9 | +// CPU profile (GC / hot paths). The -bench regexp must match the full sub-benchmark |
| 10 | +// name (underscores): BenchmarkLoadverify_HubBroadcast_ChannelMessage/all_in_channel_128. |
| 11 | +// |
| 12 | +// PowerShell: quote -cpuprofile or ".pprof" is parsed incorrectly and you get a wrong filename. |
| 13 | +// |
| 14 | +// From repo root, -cpuprofile="name.pprof" is usually created in that same directory (your shell cwd). |
| 15 | +// If pprof cannot find it, also try .\server\name.pprof (behavior can depend on Go version). |
| 16 | +// |
| 17 | +// go test ./server -run=^$ -bench=Loadverify_HubBroadcast_ChannelMessage/all_in_channel_128 -cpuprofile="loadverify-cpu.pprof" |
| 18 | +// go tool pprof -top .\loadverify-cpu.pprof |
| 19 | +// |
| 20 | +// Code reality check: channel-scoped broadcasts still range over every entry in |
| 21 | +// h.clients (see hub.go broadcast case); recipients are filtered by channel |
| 22 | +// membership. Benchmarks vary both total clients and in-channel count. |
| 23 | +// |
| 24 | +// Compare ChannelMessage/all_in_channel_* vs ChannelMessage_fixedChannel8: |
| 25 | +// cost grows with total registered clients, not only #bench population. |
| 26 | + |
| 27 | +package server |
| 28 | + |
| 29 | +import ( |
| 30 | + "database/sql" |
| 31 | + "encoding/json" |
| 32 | + "fmt" |
| 33 | + "testing" |
| 34 | + "time" |
| 35 | + |
| 36 | + "github.com/Cod-e-Codes/marchat/shared" |
| 37 | + _ "modernc.org/sqlite" |
| 38 | +) |
| 39 | + |
| 40 | +func loadverifyDrain(ch <-chan interface{}) { |
| 41 | + go func() { |
| 42 | + for range ch { |
| 43 | + } |
| 44 | + }() |
| 45 | +} |
| 46 | + |
| 47 | +// setupLoadverifyHub registers total clients; the first inChannel join "bench", |
| 48 | +// the rest join "lobby" only. Starts hub.Run in the background. |
| 49 | +func setupLoadverifyHub(b *testing.B, total, inChannel int) *Hub { |
| 50 | + b.Helper() |
| 51 | + if inChannel > total { |
| 52 | + b.Fatal("inChannel > total") |
| 53 | + } |
| 54 | + db, err := sql.Open("sqlite", ":memory:") |
| 55 | + if err != nil { |
| 56 | + b.Fatalf("sqlite: %v", err) |
| 57 | + } |
| 58 | + b.Cleanup(func() { db.Close() }) |
| 59 | + CreateSchema(db) |
| 60 | + hub := NewHub("", "", "", db) |
| 61 | + go hub.Run() |
| 62 | + time.Sleep(20 * time.Millisecond) |
| 63 | + |
| 64 | + // Large buffer avoids hub drop-on-full during fast benchmarks; production uses 256 |
| 65 | + // (handlers.go) and then conn.Close() runs. These Clients have conn == nil. |
| 66 | + const loadverifySendBuf = 65536 |
| 67 | + for i := 0; i < total; i++ { |
| 68 | + c := &Client{ |
| 69 | + username: fmt.Sprintf("loadverify-%d", i), |
| 70 | + send: make(chan interface{}, loadverifySendBuf), |
| 71 | + } |
| 72 | + loadverifyDrain(c.send) |
| 73 | + hub.clientsMutex.Lock() |
| 74 | + hub.clients[c] = true |
| 75 | + hub.clientsMutex.Unlock() |
| 76 | + if i < inChannel { |
| 77 | + hub.joinChannel(c, "bench") |
| 78 | + } else { |
| 79 | + hub.joinChannel(c, "lobby") |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return hub |
| 84 | +} |
| 85 | + |
| 86 | +func BenchmarkLoadverify_HubBroadcast_ChannelMessage(b *testing.B) { |
| 87 | + for _, n := range []int{8, 32, 64, 128} { |
| 88 | + b.Run(fmt.Sprintf("all_in_channel_%d", n), func(b *testing.B) { |
| 89 | + hub := setupLoadverifyHub(b, n, n) |
| 90 | + |
| 91 | + // TypingMessage avoids plugin IPC in hub Run (TextMessage triggers SendMessageToPlugins). |
| 92 | + msg := shared.Message{ |
| 93 | + Sender: "loadverify", |
| 94 | + Channel: "bench", |
| 95 | + Type: shared.TypingMessage, |
| 96 | + CreatedAt: time.Now(), |
| 97 | + } |
| 98 | + |
| 99 | + b.ResetTimer() |
| 100 | + for i := 0; i < b.N; i++ { |
| 101 | + hub.broadcast <- msg |
| 102 | + } |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func BenchmarkLoadverify_HubBroadcast_ChannelMessage_fixedChannel8(b *testing.B) { |
| 108 | + // 8 recipients in #bench, many extra clients only in #lobby; highlights |
| 109 | + // iteration over all registered clients vs channel population. |
| 110 | + for _, total := range []int{16, 64, 128} { |
| 111 | + b.Run(fmt.Sprintf("in_bench_8_total_%d", total), func(b *testing.B) { |
| 112 | + hub := setupLoadverifyHub(b, total, 8) |
| 113 | + |
| 114 | + // TypingMessage avoids plugin IPC in hub Run (TextMessage triggers SendMessageToPlugins). |
| 115 | + msg := shared.Message{ |
| 116 | + Sender: "loadverify", |
| 117 | + Channel: "bench", |
| 118 | + Type: shared.TypingMessage, |
| 119 | + CreatedAt: time.Now(), |
| 120 | + } |
| 121 | + |
| 122 | + b.ResetTimer() |
| 123 | + for i := 0; i < b.N; i++ { |
| 124 | + hub.broadcast <- msg |
| 125 | + } |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func BenchmarkLoadverify_HubBroadcast_SystemWide(b *testing.B) { |
| 131 | + for _, n := range []int{8, 32, 64} { |
| 132 | + b.Run(fmt.Sprintf("clients_%d", n), func(b *testing.B) { |
| 133 | + hub := setupLoadverifyHub(b, n, n) |
| 134 | + |
| 135 | + msg := shared.Message{ |
| 136 | + Sender: "System", |
| 137 | + Channel: "bench", |
| 138 | + Content: "announce", |
| 139 | + Type: shared.TypingMessage, |
| 140 | + CreatedAt: time.Now(), |
| 141 | + } |
| 142 | + |
| 143 | + b.ResetTimer() |
| 144 | + for i := 0; i < b.N; i++ { |
| 145 | + hub.broadcast <- msg |
| 146 | + } |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func BenchmarkLoadverify_HubBroadcast_ParallelSenders(b *testing.B) { |
| 152 | + const clients = 32 |
| 153 | + hub := setupLoadverifyHub(b, clients, clients) |
| 154 | + |
| 155 | + msg := shared.Message{ |
| 156 | + Sender: "loadverify", |
| 157 | + Channel: "bench", |
| 158 | + Type: shared.TypingMessage, |
| 159 | + CreatedAt: time.Now(), |
| 160 | + } |
| 161 | + |
| 162 | + b.ResetTimer() |
| 163 | + b.RunParallel(func(pb *testing.PB) { |
| 164 | + for pb.Next() { |
| 165 | + hub.broadcast <- msg |
| 166 | + } |
| 167 | + }) |
| 168 | +} |
| 169 | + |
| 170 | +func BenchmarkLoadverify_JSONMarshal_TextMessage(b *testing.B) { |
| 171 | + msg := shared.Message{ |
| 172 | + Sender: "user", |
| 173 | + Channel: "general", |
| 174 | + Content: "hello world loadverify", |
| 175 | + Type: shared.TextMessage, |
| 176 | + CreatedAt: time.Now(), |
| 177 | + } |
| 178 | + b.ResetTimer() |
| 179 | + for i := 0; i < b.N; i++ { |
| 180 | + _, _ = json.Marshal(&msg) |
| 181 | + } |
| 182 | +} |
0 commit comments