gatus/storage/store/store_bench_test.go

202 lines
4.6 KiB
Go
Raw Normal View History

2021-02-03 05:06:34 +01:00
package store
import (
"testing"
"time"
"github.com/TwiN/gatus/v3/core"
"github.com/TwiN/gatus/v3/storage/store/common/paging"
"github.com/TwiN/gatus/v3/storage/store/memory"
"github.com/TwiN/gatus/v3/storage/store/sql"
2021-02-03 05:06:34 +01:00
)
func BenchmarkStore_GetAllEndpointStatuses(b *testing.B) {
2021-02-03 05:06:34 +01:00
memoryStore, err := memory.NewStore("")
if err != nil {
b.Fatal("failed to create store:", err.Error())
}
sqliteStore, err := sql.NewStore("sqlite", b.TempDir()+"/BenchmarkStore_GetAllEndpointStatuses.db")
2021-07-14 05:06:52 +02:00
if err != nil {
b.Fatal("failed to create store:", err.Error())
}
2021-07-18 22:13:05 +02:00
defer sqliteStore.Close()
2021-02-03 05:06:34 +01:00
type Scenario struct {
2021-07-14 05:06:52 +02:00
Name string
Store Store
Parallel bool
2021-02-03 05:06:34 +01:00
}
scenarios := []Scenario{
{
2021-07-14 05:06:52 +02:00
Name: "memory",
Store: memoryStore,
Parallel: false,
},
{
Name: "memory-parallel",
Store: memoryStore,
Parallel: true,
},
{
2021-07-18 22:13:05 +02:00
Name: "sqlite",
Store: sqliteStore,
2021-07-14 05:06:52 +02:00
Parallel: false,
},
{
2021-07-18 22:13:05 +02:00
Name: "sqlite-parallel",
Store: sqliteStore,
2021-07-14 05:06:52 +02:00
Parallel: true,
2021-02-03 05:06:34 +01:00
},
}
for _, scenario := range scenarios {
scenario.Store.Insert(&testEndpoint, &testSuccessfulResult)
scenario.Store.Insert(&testEndpoint, &testUnsuccessfulResult)
2021-02-03 05:06:34 +01:00
b.Run(scenario.Name, func(b *testing.B) {
2021-07-14 05:06:52 +02:00
if scenario.Parallel {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
scenario.Store.GetAllEndpointStatuses(paging.NewEndpointStatusParams().WithResults(1, 20))
2021-07-14 05:06:52 +02:00
}
})
} else {
for n := 0; n < b.N; n++ {
scenario.Store.GetAllEndpointStatuses(paging.NewEndpointStatusParams().WithResults(1, 20))
2021-07-14 05:06:52 +02:00
}
2021-02-03 05:06:34 +01:00
}
b.ReportAllocs()
})
2021-07-14 05:06:52 +02:00
scenario.Store.Clear()
2021-02-03 05:06:34 +01:00
}
}
func BenchmarkStore_Insert(b *testing.B) {
2021-07-15 07:44:36 +02:00
memoryStore, err := memory.NewStore("")
if err != nil {
b.Fatal("failed to create store:", err.Error())
}
sqliteStore, err := sql.NewStore("sqlite", b.TempDir()+"/BenchmarkStore_Insert.db")
if err != nil {
b.Fatal("failed to create store:", err.Error())
}
2021-07-18 22:13:05 +02:00
defer sqliteStore.Close()
2021-02-03 05:06:34 +01:00
type Scenario struct {
Name string
Store Store
Parallel bool
2021-02-03 05:06:34 +01:00
}
scenarios := []Scenario{
2021-07-15 07:44:36 +02:00
{
Name: "memory",
Store: memoryStore,
Parallel: false,
},
{
Name: "memory-parallel",
Store: memoryStore,
Parallel: true,
},
{
2021-07-18 22:13:05 +02:00
Name: "sqlite",
Store: sqliteStore,
Parallel: false,
},
{
2021-07-18 22:13:05 +02:00
Name: "sqlite-parallel",
Store: sqliteStore,
Parallel: false,
},
2021-02-03 05:06:34 +01:00
}
for _, scenario := range scenarios {
b.Run(scenario.Name, func(b *testing.B) {
if scenario.Parallel {
b.RunParallel(func(pb *testing.PB) {
n := 0
for pb.Next() {
var result core.Result
if n%10 == 0 {
result = testUnsuccessfulResult
} else {
result = testSuccessfulResult
}
result.Timestamp = time.Now()
scenario.Store.Insert(&testEndpoint, &result)
n++
}
})
} else {
for n := 0; n < b.N; n++ {
var result core.Result
if n%10 == 0 {
result = testUnsuccessfulResult
} else {
result = testSuccessfulResult
}
result.Timestamp = time.Now()
scenario.Store.Insert(&testEndpoint, &result)
2021-03-05 06:50:24 +01:00
}
2021-02-03 05:06:34 +01:00
}
b.ReportAllocs()
scenario.Store.Clear()
2021-02-03 05:06:34 +01:00
})
}
}
func BenchmarkStore_GetEndpointStatusByKey(b *testing.B) {
memoryStore, err := memory.NewStore("")
if err != nil {
b.Fatal("failed to create store:", err.Error())
}
sqliteStore, err := sql.NewStore("sqlite", b.TempDir()+"/BenchmarkStore_GetEndpointStatusByKey.db")
if err != nil {
b.Fatal("failed to create store:", err.Error())
}
2021-07-18 22:13:05 +02:00
defer sqliteStore.Close()
type Scenario struct {
Name string
Store Store
Parallel bool
}
scenarios := []Scenario{
{
Name: "memory",
Store: memoryStore,
Parallel: false,
},
2021-07-17 02:39:32 +02:00
{
Name: "memory-parallel",
Store: memoryStore,
Parallel: true,
},
{
2021-07-18 22:13:05 +02:00
Name: "sqlite",
Store: sqliteStore,
Parallel: false,
},
2021-07-17 02:39:32 +02:00
{
2021-07-18 22:13:05 +02:00
Name: "sqlite-parallel",
Store: sqliteStore,
2021-07-17 02:39:32 +02:00
Parallel: true,
},
}
for _, scenario := range scenarios {
2021-07-17 02:39:32 +02:00
for i := 0; i < 50; i++ {
scenario.Store.Insert(&testEndpoint, &testSuccessfulResult)
scenario.Store.Insert(&testEndpoint, &testUnsuccessfulResult)
}
b.Run(scenario.Name, func(b *testing.B) {
if scenario.Parallel {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
scenario.Store.GetEndpointStatusByKey(testEndpoint.Key(), paging.NewEndpointStatusParams().WithResults(1, 20))
}
})
} else {
for n := 0; n < b.N; n++ {
scenario.Store.GetEndpointStatusByKey(testEndpoint.Key(), paging.NewEndpointStatusParams().WithResults(1, 20))
}
}
b.ReportAllocs()
})
scenario.Store.Clear()
}
}