gatus/storage/store/store_bench_test.go

214 lines
5.0 KiB
Go
Raw Normal View History

2021-02-03 05:06:34 +01:00
package store
import (
"strconv"
2021-02-03 05:06:34 +01:00
"testing"
"time"
"github.com/TwiN/gatus/v5/config/endpoint"
2022-12-06 07:41:09 +01:00
"github.com/TwiN/gatus/v5/storage/store/common/paging"
"github.com/TwiN/gatus/v5/storage/store/memory"
"github.com/TwiN/gatus/v5/storage/store/sql"
2021-02-03 05:06:34 +01:00
)
func BenchmarkStore_GetAllEndpointStatuses(b *testing.B) {
memoryStore, err := memory.NewStore()
2021-02-03 05:06:34 +01:00
if err != nil {
b.Fatal("failed to create store:", err.Error())
}
sqliteStore, err := sql.NewStore("sqlite", b.TempDir()+"/BenchmarkStore_GetAllEndpointStatuses.db", false)
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 {
numberOfEndpoints := []int{10, 25, 50, 100}
for _, numberOfEndpointsToCreate := range numberOfEndpoints {
// Create endpoints and insert results
for i := 0; i < numberOfEndpointsToCreate; i++ {
ep := testEndpoint
ep.Name = "endpoint" + strconv.Itoa(i)
// Insert 20 results for each endpoint
for j := 0; j < 20; j++ {
scenario.Store.Insert(&ep, &testSuccessfulResult)
2021-07-14 05:06:52 +02:00
}
2021-02-03 05:06:34 +01:00
}
// Run the scenarios
b.Run(scenario.Name+"-with-"+strconv.Itoa(numberOfEndpointsToCreate)+"-endpoints", func(b *testing.B) {
if scenario.Parallel {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = scenario.Store.GetAllEndpointStatuses(paging.NewEndpointStatusParams().WithResults(1, 20))
}
})
} else {
for n := 0; n < b.N; n++ {
_, _ = scenario.Store.GetAllEndpointStatuses(paging.NewEndpointStatusParams().WithResults(1, 20))
}
}
b.ReportAllocs()
})
scenario.Store.Clear()
}
2021-02-03 05:06:34 +01:00
}
}
func BenchmarkStore_Insert(b *testing.B) {
memoryStore, err := memory.NewStore()
2021-07-15 07:44:36 +02:00
if err != nil {
b.Fatal("failed to create store:", err.Error())
}
sqliteStore, err := sql.NewStore("sqlite", b.TempDir()+"/BenchmarkStore_Insert.db", false)
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 endpoint.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 endpoint.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", false)
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()
}
}