package memory import ( "context" "fmt" "testing" "time" "github.com/google/uuid" "mtusi-messenger/internal/domain" ) type benchmarkClock struct{ now time.Time } func (c benchmarkClock) Now() time.Time { return c.now } func BenchmarkMessageCreate(b *testing.B) { for _, size := range []int{1000, 10000, 50000} { b.Run(fmt.Sprintf("history=%d", size), func(b *testing.B) { base := time.Date(2026, 9, 1, 12, 0, 0, 0, time.UTC) store := NewStore(WithClock(benchmarkClock{now: base.Add(time.Hour)})) chatID, senderID := uuid.New(), uuid.New() store.chatsByID[chatID] = domain.Chat{ID: chatID, MemberIDs: []uuid.UUID{senderID}} history := make([]domain.Message, size, size+1) for i := range history { history[i] = domain.Message{ ID: uuid.New(), ChatID: chatID, SenderID: senderID, CreatedAt: base.Add(time.Duration(i) * time.Microsecond), } } store.messagesByChat[chatID] = history draft := domain.MessageDraft{ChatID: chatID, SenderID: senderID, Text: "benchmark"} b.ReportAllocs() b.ResetTimer() for b.Loop() { created, err := store.Messages().Create(context.Background(), draft) if err != nil { b.Fatal(err) } if len(store.messagesByChat[chatID]) != size+1 || compareKeyset(history[size-1].CreatedAt, history[size-1].ID, created.CreatedAt, created.ID) >= 0 { b.Fatal("message not appended in keyset order") } store.messagesByChat[chatID] = store.messagesByChat[chatID][:size] } }) } }