Cache total stats in memory

This commit is contained in:
2021-11-03 21:24:23 +01:00
parent 492c88c5b2
commit bd6eafd9f7
8 changed files with 177 additions and 36 deletions

View File

@@ -188,6 +188,52 @@ func testLoginAttemptStore(s store.LoginAttemptStore, t *testing.T) {
})
}
func benchmarkLoginAttemptStore(setupFunc func() store.LoginAttemptStore, b *testing.B) {
b.Run("BenchmarkAdd", func(b *testing.B) {
s := setupFunc()
for i := 0; i < b.N; i++ {
attempt := randomAttempts(1)
err := s.AddAttempt(attempt[0])
if err != nil {
b.Fatalf("Error adding attempt: %s", err)
}
}
})
b.Run("BenchmarkAdd10k", func(b *testing.B) {
attempts := randomAttempts(10_000)
for i := 0; i < b.N; i++ {
b.StopTimer()
s := setupFunc()
b.StartTimer()
for _, attempt := range attempts {
err := s.AddAttempt(attempt)
if err != nil {
b.Fatalf("Error adding attempt: %s", err)
}
}
}
})
b.Run("BenchmarkAll10k", func(b *testing.B) {
s := setupFunc()
attempts := randomAttempts(10_000)
for _, attempt := range attempts {
err := s.AddAttempt(attempt)
if err != nil {
b.Fatalf("Error adding attempt: %s", err)
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
all, err := s.All()
if err != nil {
b.Fatalf("Error fetchin all: %s", err)
}
_ = len(all)
}
})
}
func randomAttempts(count int) []*models.LoginAttempt {
var attempts []*models.LoginAttempt
for i := 0; i < count; i++ {