diff --git a/core/service-status_bench_test.go b/core/service-status_bench_test.go new file mode 100644 index 00000000..fb981296 --- /dev/null +++ b/core/service-status_bench_test.go @@ -0,0 +1,92 @@ +package core + +import ( + "testing" + "time" +) + +var ( + firstCondition = Condition("[STATUS] == 200") + secondCondition = Condition("[RESPONSE_TIME] < 500") + thirdCondition = Condition("[CERTIFICATE_EXPIRATION] < 72h") + + timestamp = time.Now() + + testService = Service{ + Name: "name", + Group: "group", + URL: "https://example.org/what/ever", + Method: "GET", + Body: "body", + Interval: 30 * time.Second, + Conditions: []*Condition{&firstCondition, &secondCondition, &thirdCondition}, + Alerts: nil, + Insecure: false, + NumberOfFailuresInARow: 0, + NumberOfSuccessesInARow: 0, + } + testSuccessfulResult = Result{ + Hostname: "example.org", + IP: "127.0.0.1", + HTTPStatus: 200, + Body: []byte("body"), + Errors: nil, + Connected: true, + Success: true, + Timestamp: timestamp, + Duration: 150 * time.Millisecond, + CertificateExpiration: 10 * time.Hour, + ConditionResults: []*ConditionResult{ + { + Condition: "[STATUS] == 200", + Success: true, + }, + { + Condition: "[RESPONSE_TIME] < 500", + Success: true, + }, + { + Condition: "[CERTIFICATE_EXPIRATION] < 72h", + Success: true, + }, + }, + } + testUnsuccessfulResult = Result{ + Hostname: "example.org", + IP: "127.0.0.1", + HTTPStatus: 200, + Body: []byte("body"), + Errors: []string{"error-1", "error-2"}, + Connected: true, + Success: false, + Timestamp: timestamp, + Duration: 750 * time.Millisecond, + CertificateExpiration: 10 * time.Hour, + ConditionResults: []*ConditionResult{ + { + Condition: "[STATUS] == 200", + Success: true, + }, + { + Condition: "[RESPONSE_TIME] < 500", + Success: false, + }, + { + Condition: "[CERTIFICATE_EXPIRATION] < 72h", + Success: false, + }, + }, + } +) + +func BenchmarkServiceStatus_WithResultPagination(b *testing.B) { + service := &testService + serviceStatus := NewServiceStatus(service) + for i := 0; i < MaximumNumberOfResults; i++ { + serviceStatus.AddResult(&testSuccessfulResult) + } + for n := 0; n < b.N; n++ { + serviceStatus.WithResultPagination(1, 20) + } + b.ReportAllocs() +} diff --git a/storage/store/store_bench_test.go b/storage/store/store_bench_test.go index e3c34c20..1637d59d 100644 --- a/storage/store/store_bench_test.go +++ b/storage/store/store_bench_test.go @@ -127,8 +127,11 @@ func BenchmarkStore_Insert(b *testing.B) { for _, scenario := range scenarios { b.Run(scenario.Name, func(b *testing.B) { for n := 0; n < b.N; n++ { - scenario.Store.Insert(&testService, &testSuccessfulResult) - scenario.Store.Insert(&testService, &testUnsuccessfulResult) + if n%100 == 0 { + scenario.Store.Insert(&testService, &testSuccessfulResult) + } else { + scenario.Store.Insert(&testService, &testUnsuccessfulResult) + } } b.ReportAllocs() })