2021-02-03 05:06:34 +01:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2022-06-14 01:15:30 +02:00
|
|
|
"strconv"
|
2021-02-03 05:06:34 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-10-08 03:28:04 +02:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2021-10-23 22:47:12 +02: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())
|
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
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 {
|
2022-06-14 01:15:30 +02:00
|
|
|
numberOfEndpoints := []int{10, 25, 50, 100}
|
|
|
|
for _, numberOfEndpointsToCreate := range numberOfEndpoints {
|
|
|
|
// Create endpoints and insert results
|
|
|
|
for i := 0; i < numberOfEndpointsToCreate; i++ {
|
|
|
|
endpoint := testEndpoint
|
|
|
|
endpoint.Name = "endpoint" + strconv.Itoa(i)
|
|
|
|
// Insert 20 results for each endpoint
|
|
|
|
for j := 0; j < 20; j++ {
|
|
|
|
scenario.Store.Insert(&endpoint, &testSuccessfulResult)
|
2021-07-14 05:06:52 +02:00
|
|
|
}
|
2021-02-03 05:06:34 +01:00
|
|
|
}
|
2022-06-14 01:15:30 +02: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) {
|
2021-07-15 07:44:36 +02:00
|
|
|
memoryStore, err := memory.NewStore("")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal("failed to create store:", err.Error())
|
|
|
|
}
|
2021-09-11 00:00:04 +02:00
|
|
|
sqliteStore, err := sql.NewStore("sqlite", b.TempDir()+"/BenchmarkStore_Insert.db")
|
2021-07-12 06:56:30 +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-13 04:52:06 +02:00
|
|
|
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-13 04:52:06 +02:00
|
|
|
{
|
2021-07-18 22:13:05 +02:00
|
|
|
Name: "sqlite",
|
|
|
|
Store: sqliteStore,
|
2021-07-13 04:52:06 +02:00
|
|
|
Parallel: false,
|
|
|
|
},
|
|
|
|
{
|
2021-07-18 22:13:05 +02:00
|
|
|
Name: "sqlite-parallel",
|
|
|
|
Store: sqliteStore,
|
2021-07-13 04:52:06 +02:00
|
|
|
Parallel: false,
|
2021-07-12 06:56:30 +02:00
|
|
|
},
|
2021-02-03 05:06:34 +01:00
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
|
|
|
b.Run(scenario.Name, func(b *testing.B) {
|
2021-07-13 04:52:06 +02:00
|
|
|
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()
|
2021-10-23 22:47:12 +02:00
|
|
|
scenario.Store.Insert(&testEndpoint, &result)
|
2021-07-13 04:52:06 +02:00
|
|
|
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()
|
2021-10-23 22:47:12 +02:00
|
|
|
scenario.Store.Insert(&testEndpoint, &result)
|
2021-03-05 06:50:24 +01:00
|
|
|
}
|
2021-02-03 05:06:34 +01:00
|
|
|
}
|
|
|
|
b.ReportAllocs()
|
2021-07-13 04:52:06 +02:00
|
|
|
scenario.Store.Clear()
|
2021-02-03 05:06:34 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 01:12:14 +02:00
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
func BenchmarkStore_GetEndpointStatusByKey(b *testing.B) {
|
2021-07-17 01:12:14 +02:00
|
|
|
memoryStore, err := memory.NewStore("")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal("failed to create store:", err.Error())
|
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
sqliteStore, err := sql.NewStore("sqlite", b.TempDir()+"/BenchmarkStore_GetEndpointStatusByKey.db")
|
2021-07-17 01:12:14 +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-07-17 01:12:14 +02:00
|
|
|
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-17 01:12:14 +02:00
|
|
|
{
|
2021-07-18 22:13:05 +02:00
|
|
|
Name: "sqlite",
|
|
|
|
Store: sqliteStore,
|
2021-07-17 01:12:14 +02:00
|
|
|
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,
|
|
|
|
},
|
2021-07-17 01:12:14 +02:00
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
2021-07-17 02:39:32 +02:00
|
|
|
for i := 0; i < 50; i++ {
|
2021-10-23 22:47:12 +02:00
|
|
|
scenario.Store.Insert(&testEndpoint, &testSuccessfulResult)
|
|
|
|
scenario.Store.Insert(&testEndpoint, &testUnsuccessfulResult)
|
2021-07-17 01:12:14 +02:00
|
|
|
}
|
|
|
|
b.Run(scenario.Name, func(b *testing.B) {
|
|
|
|
if scenario.Parallel {
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
2021-10-23 22:47:12 +02:00
|
|
|
scenario.Store.GetEndpointStatusByKey(testEndpoint.Key(), paging.NewEndpointStatusParams().WithResults(1, 20))
|
2021-07-17 01:12:14 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
for n := 0; n < b.N; n++ {
|
2021-10-23 22:47:12 +02:00
|
|
|
scenario.Store.GetEndpointStatusByKey(testEndpoint.Key(), paging.NewEndpointStatusParams().WithResults(1, 20))
|
2021-07-17 01:12:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
b.ReportAllocs()
|
|
|
|
})
|
|
|
|
scenario.Store.Clear()
|
|
|
|
}
|
|
|
|
}
|