Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mcp/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
)

// If true, MemoryEventStore will do frequent validation to check invariants, slowing it down.
// Remove when we're confident in the code.
const validateMemoryEventStore = true
// Enable for debugging.
const validateMemoryEventStore = false

// An Event is a server-sent event.
// See https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#fields.
Expand Down
44 changes: 44 additions & 0 deletions mcp/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"slices"
"strings"
"testing"
"time"
)

func TestScanEvents(t *testing.T) {
Expand Down Expand Up @@ -252,3 +253,46 @@ func TestMemoryEventStoreAfter(t *testing.T) {
})
}
}

func BenchmarkMemoryEventStore(b *testing.B) {
// Benchmark with various settings for event store size, number of session,
// and payload size.
//
// Assume a small number of streams per session, which is probably realistic.
tests := []struct {
name string
limit int
sessions int
datasize int
}{
{"1KB", 1024, 1, 16},
{"1MB", 1024 * 1024, 10, 16},
{"10MB", 10 * 1024 * 1024, 100, 16},
{"10MB_big", 10 * 1024 * 1024, 1000, 128},
}

for _, test := range tests {
b.Run(test.name, func(b *testing.B) {
store := NewMemoryEventStore(nil)
store.SetMaxBytes(test.limit)
ctx := context.Background()
sessionIDs := make([]string, test.sessions)
streamIDs := make([][3]StreamID, test.sessions)
for i := range sessionIDs {
sessionIDs[i] = fmt.Sprint(i)
for j := range 3 {
streamIDs[i][j] = StreamID(randText())
}
}
payload := make([]byte, test.datasize)
start := time.Now()
b.ResetTimer()
for i := 0; i < b.N; i++ {
sessionID := sessionIDs[i%len(sessionIDs)]
streamID := streamIDs[i%len(sessionIDs)][i%3]
store.Append(ctx, sessionID, streamID, payload)
}
b.ReportMetric(float64(test.datasize)*float64(b.N)/time.Since(start).Seconds(), "bytes/s")
})
}
}