2021-02-03 05:06:34 +01:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/TwinProduction/gatus/core"
|
2021-07-12 06:56:30 +02:00
|
|
|
"github.com/TwinProduction/gatus/storage/store/database"
|
2021-02-03 05:06:34 +01:00
|
|
|
"github.com/TwinProduction/gatus/storage/store/memory"
|
2021-07-15 04:26:51 +02:00
|
|
|
"github.com/TwinProduction/gatus/storage/store/paging"
|
2021-02-03 05:06:34 +01:00
|
|
|
)
|
|
|
|
|
2021-07-15 04:26:51 +02:00
|
|
|
func BenchmarkStore_GetAllServiceStatuses(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-07-15 04:26:51 +02:00
|
|
|
databaseStore, err := database.NewStore("sqlite", b.TempDir()+"/BenchmarkStore_GetAllServiceStatuses.db")
|
2021-07-14 05:06:52 +02:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal("failed to create store:", err.Error())
|
|
|
|
}
|
|
|
|
defer databaseStore.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,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "database",
|
|
|
|
Store: databaseStore,
|
|
|
|
Parallel: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "database-parallel",
|
|
|
|
Store: databaseStore,
|
|
|
|
Parallel: true,
|
2021-02-03 05:06:34 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
|
|
|
scenario.Store.Insert(&testService, &testSuccessfulResult)
|
|
|
|
scenario.Store.Insert(&testService, &testUnsuccessfulResult)
|
|
|
|
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() {
|
2021-07-15 04:26:51 +02:00
|
|
|
scenario.Store.GetAllServiceStatuses(paging.NewServiceStatusParams().WithResults(1, 20))
|
2021-07-14 05:06:52 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
for n := 0; n < b.N; n++ {
|
2021-07-15 04:26:51 +02:00
|
|
|
scenario.Store.GetAllServiceStatuses(paging.NewServiceStatusParams().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())
|
|
|
|
}
|
2021-07-12 06:56:30 +02:00
|
|
|
databaseStore, err := database.NewStore("sqlite", b.TempDir()+"/BenchmarkStore_Insert.db")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal("failed to create store:", err.Error())
|
|
|
|
}
|
|
|
|
defer databaseStore.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
|
|
|
{
|
|
|
|
Name: "database",
|
|
|
|
Store: databaseStore,
|
|
|
|
Parallel: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "database-parallel",
|
|
|
|
Store: databaseStore,
|
|
|
|
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()
|
|
|
|
scenario.Store.Insert(&testService, &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(&testService, &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
|
|
|
|
|
|
|
func BenchmarkStore_GetServiceStatusByKey(b *testing.B) {
|
|
|
|
memoryStore, err := memory.NewStore("")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal("failed to create store:", err.Error())
|
|
|
|
}
|
|
|
|
databaseStore, err := database.NewStore("sqlite", b.TempDir()+"/BenchmarkStore_GetServiceStatusByKey.db")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal("failed to create store:", err.Error())
|
|
|
|
}
|
|
|
|
defer databaseStore.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-17 01:12:14 +02:00
|
|
|
{
|
|
|
|
Name: "database",
|
|
|
|
Store: databaseStore,
|
|
|
|
Parallel: false,
|
|
|
|
},
|
2021-07-17 02:39:32 +02:00
|
|
|
{
|
|
|
|
Name: "database-parallel",
|
|
|
|
Store: databaseStore,
|
|
|
|
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-07-17 01:12:14 +02:00
|
|
|
scenario.Store.Insert(&testService, &testSuccessfulResult)
|
|
|
|
scenario.Store.Insert(&testService, &testUnsuccessfulResult)
|
|
|
|
}
|
|
|
|
b.Run(scenario.Name, func(b *testing.B) {
|
|
|
|
if scenario.Parallel {
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
scenario.Store.GetServiceStatusByKey(testService.Key(), paging.NewServiceStatusParams().WithResults(1, 20))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
scenario.Store.GetServiceStatusByKey(testService.Key(), paging.NewServiceStatusParams().WithResults(1, 20))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.ReportAllocs()
|
|
|
|
})
|
|
|
|
scenario.Store.Clear()
|
|
|
|
}
|
|
|
|
}
|