textmachine/platform/internal/pgstore/perf_test.go

48 lines
1.7 KiB
Go

package pgstore
import (
"fmt"
"testing"
"time"
)
// perf_test.go: the measurements a projection change has to be judged by.
//
// Benchmarks rather than tests: `go test` does not run them, so the battery stays fast, and the
// numbers are reproducible on demand — `go test ./internal/pgstore/ -bench LibraryPage -run xxx`.
// The note counter was added to the library row from reasoning alone and cost 96% of the page's time
// on a corpus-sized account; this is what says so before and after.
// corpus seeds one account with a library the size of a real one: books whose chapters carry
// resolutions, a fifth of them flagged.
func corpus(t testing.TB, s *Store, books, chapters, units int) string {
t.Helper()
ctx := t.Context()
now := fundedAccount(t, s, t.Context(), "u1", "1000")
for b := range books {
id := fmt.Sprintf("bk%03d", b)
seedBook(t, s, ctx, id, "u1", chapters)
exec(t, s, ctx, `
insert into chapters (id, book_id, number, units_total, units_edit_done)
select $1 || '_' || n, $1, n, $2, $2 from generate_series(1, $3) as n`, id, units, chapters)
exec(t, s, ctx, `
insert into unit_resolutions (book_id, chapter, unit, wave, shipped, flagged, at, revision)
select $1, n, u, 'edit', true, (u % 5 = 0), $2, 1
from generate_series(1, $3) as n, generate_series(0, $4) as u`,
id, now, chapters, units-1)
}
exec(t, s, ctx, `vacuum analyze`)
return "u1"
}
func BenchmarkLibraryPage(b *testing.B) {
s, ctx := testDB(b)
user := corpus(b, s, 40, 500, 10)
start := time.Now()
for b.Loop() {
if _, err := s.ListBooks(ctx, user, 100, ""); err != nil {
b.Fatal(err)
}
}
b.ReportMetric(float64(time.Since(start).Milliseconds())/float64(b.N), "ms/page")
}