clerk/internal/analyzer/benchmark_test.go (view raw)
| 1 | package analyzer |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "olexsmir.xyz/clerk/journal" |
| 7 | ) |
| 8 | |
| 9 | func BenchmarkBuild(b *testing.B) { |
| 10 | b.Run("basic", bench("../../journal/testdata/journals/basic.journal")) |
| 11 | b.Run("standard", bench("../../journal/testdata/journals/actual-ledger-input-standard.dat")) |
| 12 | b.Run("personal", bench("../../journal/testdata/journals/actual-personal.journal")) |
| 13 | b.Run("1k txns", bench("../../journal/testdata/journals/actual-1ktxns-100accts.journal")) |
| 14 | b.Run("wow", bench("../../journal/testdata/journals/actual-ledger-input-wow.dat")) |
| 15 | b.Run("1k txns, prefix lookup", func(b *testing.B) { |
| 16 | rj := loadJournal("../../journal/testdata/journals/actual-1ktxns-100accts.journal") |
| 17 | a := Build(rj) |
| 18 | prefixes := make([]string, 0, len(a.AccountsByPrefix)) |
| 19 | for p := range a.AccountsByPrefix { |
| 20 | prefixes = append(prefixes, p) |
| 21 | } |
| 22 | b.ResetTimer() |
| 23 | for b.Loop() { |
| 24 | for _, p := range prefixes { |
| 25 | _ = a.AccountsByPrefix[p] |
| 26 | } |
| 27 | } |
| 28 | }) |
| 29 | |
| 30 | b.Run("1k txns, account iter", func(b *testing.B) { |
| 31 | rj := loadJournal("../../journal/testdata/journals/actual-1ktxns-100accts.journal") |
| 32 | a := Build(rj) |
| 33 | names := a.AccountNames |
| 34 | b.ResetTimer() |
| 35 | for b.Loop() { |
| 36 | for _, name := range names { |
| 37 | _ = a.Accounts[name].UsedCount |
| 38 | } |
| 39 | } |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | func bench(fpath string) func(b *testing.B) { |
| 44 | files := loadJournal(fpath) |
| 45 | return func(b *testing.B) { |
| 46 | b.ResetTimer() |
| 47 | b.ReportAllocs() |
| 48 | for b.Loop() { |
| 49 | Build(files) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func loadJournal(path string) *journal.ResolvedJournal { |
| 55 | ldr := journal.NewLoader() |
| 56 | rj, err := ldr.Resolve(path) |
| 57 | if err != nil { |
| 58 | panic("loadJournal: " + err.Error()) |
| 59 | } |
| 60 | return rj |
| 61 | } |