2023-10-12 15:42:36 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2023-10-12 15:42:36 +02:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type benchCase struct {
|
|
|
|
name string
|
|
|
|
storeFn func(b *testing.B) Store
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
|
|
|
var newFs = func(b *testing.B) Store {
|
2023-11-10 16:33:13 +01:00
|
|
|
b.Helper()
|
2024-07-03 11:33:02 +02:00
|
|
|
store, _ := NewFileStore(context.Background(), b.TempDir(), nil)
|
2023-10-12 15:42:36 +02:00
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
|
|
|
var newSqlite = func(b *testing.B) Store {
|
2023-11-10 16:33:13 +01:00
|
|
|
b.Helper()
|
2024-07-03 11:33:02 +02:00
|
|
|
store, _ := NewSqliteStore(context.Background(), b.TempDir(), nil)
|
2023-10-12 15:42:36 +02:00
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTest_StoreWrite(b *testing.B) {
|
|
|
|
cases := []benchCase{
|
|
|
|
{name: "FileStore_Write", storeFn: newFs, size: 100},
|
|
|
|
{name: "SqliteStore_Write", storeFn: newSqlite, size: 100},
|
|
|
|
{name: "FileStore_Write", storeFn: newFs, size: 500},
|
|
|
|
{name: "SqliteStore_Write", storeFn: newSqlite, size: 500},
|
|
|
|
{name: "FileStore_Write", storeFn: newFs, size: 1000},
|
|
|
|
{name: "SqliteStore_Write", storeFn: newSqlite, size: 1000},
|
|
|
|
{name: "FileStore_Write", storeFn: newFs, size: 2000},
|
|
|
|
{name: "SqliteStore_Write", storeFn: newSqlite, size: 2000},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
name := fmt.Sprintf("%s_%d", c.name, c.size)
|
|
|
|
store := c.storeFn(b)
|
|
|
|
|
|
|
|
for i := 0; i < c.size; i++ {
|
|
|
|
_ = newAccount(store, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Run(name, func(b *testing.B) {
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
i := c.size
|
|
|
|
for pb.Next() {
|
|
|
|
i++
|
|
|
|
err := newAccount(store, i)
|
|
|
|
require.NoError(b, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTest_StoreRead(b *testing.B) {
|
|
|
|
cases := []benchCase{
|
|
|
|
{name: "FileStore_Read", storeFn: newFs, size: 100},
|
|
|
|
{name: "SqliteStore_Read", storeFn: newSqlite, size: 100},
|
|
|
|
{name: "FileStore_Read", storeFn: newFs, size: 500},
|
|
|
|
{name: "SqliteStore_Read", storeFn: newSqlite, size: 500},
|
|
|
|
{name: "FileStore_Read", storeFn: newFs, size: 1000},
|
|
|
|
{name: "SqliteStore_Read", storeFn: newSqlite, size: 1000},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
name := fmt.Sprintf("%s_%d", c.name, c.size)
|
|
|
|
store := c.storeFn(b)
|
|
|
|
|
|
|
|
for i := 0; i < c.size; i++ {
|
|
|
|
_ = newAccount(store, i)
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
accounts := store.GetAllAccounts(context.Background())
|
2023-10-12 15:42:36 +02:00
|
|
|
id := accounts[c.size-1].Id
|
|
|
|
|
|
|
|
b.Run(name, func(b *testing.B) {
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
2024-07-03 11:33:02 +02:00
|
|
|
_, _ = store.GetAccount(context.Background(), id)
|
2023-10-12 15:42:36 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|