Skip to content

Commit ea41883

Browse files
committed
prepared cache bench improvements
1 parent 0a580a0 commit ea41883

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

handler_websocket_test.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -833,21 +833,21 @@ func TestWsBroadcastCompressionCache(t *testing.T) {
833833

834834
url := "ws" + server.URL[4:]
835835

836-
conns := make([]*websocket.Conn, 0, numConns)
836+
connections := make([]*websocket.Conn, 0, numConns)
837837
for i := 0; i < numConns; i++ {
838838
conn := bm.getConn(t, "test", url, bm.compression)
839-
conns = append(conns, conn)
839+
connections = append(connections, conn)
840840
}
841841
defer func() {
842-
for _, conn := range conns {
842+
for _, conn := range connections {
843843
_ = conn.Close()
844844
}
845845
}()
846846
_, err := n.Publish("test", payload)
847847
if err != nil {
848848
require.NoError(t, err)
849849
}
850-
for _, conn := range conns {
850+
for _, conn := range connections {
851851
_, _, err = conn.ReadMessage()
852852
require.NoError(t, err)
853853
}
@@ -859,24 +859,29 @@ func BenchmarkWsBroadcastCompressionCache(b *testing.B) {
859859
n := defaultTestNodeBenchmark(b)
860860
defer func() { _ = n.Shutdown(context.Background()) }()
861861

862-
payload := []byte(`{"input": "test"}`)
862+
// Pre-generate payloads for unique message scenarios.
863+
numUniqueMessages := 100
864+
payloads := make([][]byte, numUniqueMessages)
865+
for i := 0; i < numUniqueMessages; i++ {
866+
payloads[i] = []byte(fmt.Sprintf(`{"input": "test", "id": %d}`, i))
867+
}
863868

864869
benchmarks := []struct {
865-
getConn func(b testing.TB, channel string, url string, compression bool) *websocket.Conn
866-
compression bool
867-
cacheSizeMB int64
870+
name string
871+
compression bool
872+
cacheSizeMB int64
873+
uniqueMessages int // number of unique messages to cycle through
868874
}{
869-
{newRealConnJSON, false, 0},
870-
{newRealConnJSON, true, 0},
871-
{newRealConnJSON, true, 50},
875+
{"no_compress", false, 0, 1},
876+
{"compress_no_cache", true, 0, 1},
877+
{"compress_cache_same_msg", true, 50, 1},
878+
{"compress_cache_100_msgs", true, 50, 100},
872879
}
873880

874881
numConns := 100
875882

876883
for _, bm := range benchmarks {
877-
benchName := "compress_" + fmt.Sprintf("%v", bm.compression) + "_" +
878-
"cache_" + strconv.FormatInt(bm.cacheSizeMB, 10) + "MB"
879-
b.Run(benchName, func(b *testing.B) {
884+
b.Run(bm.name, func(b *testing.B) {
880885
b.ReportAllocs()
881886

882887
mux := http.NewServeMux()
@@ -891,7 +896,7 @@ func BenchmarkWsBroadcastCompressionCache(b *testing.B) {
891896

892897
conns := make([]*websocket.Conn, 0, numConns)
893898
for i := 0; i < numConns; i++ {
894-
conn := bm.getConn(b, "test", url, bm.compression)
899+
conn := newRealConnJSON(b, "test", url, bm.compression)
895900
conns = append(conns, conn)
896901
}
897902
defer func() {
@@ -901,6 +906,7 @@ func BenchmarkWsBroadcastCompressionCache(b *testing.B) {
901906
}()
902907
b.ResetTimer()
903908
for i := 0; i < b.N; i++ {
909+
payload := payloads[i%bm.uniqueMessages]
904910
_, err := n.Publish("test", payload)
905911
if err != nil {
906912
panic(err)

0 commit comments

Comments
 (0)