Files
karma/models/alert_test.go
Łukasz Mierzwa 81ce5d3098 Speed up alert fingerprint generation
Dynamic fingerprints made the code much slower, pprof shows they are responsible for ~70% of all cpu usage for any API call. To make it worse they are applied to all alerts, since dedup layer doesn't know which alerts will be filtered later, it operates on all of them. This PR will:
1. add benchmarks to so it's easier to track performance
2. Keep current methods for accessing fingerprints, but use precomputed static fields in those
3. Refactor Alert methods to use pointers, so we're not working on a copy

Benchmark timing went down from ~4000ns to 0.4ns for fingerprint calls and API response times from 1.3s (for my test sample) to 0.2s, which puts it back to the same level as before moving fingerprints to be dynamic. I still don't like this code much, it's all over the place, but I don't have a good idea how to better structure this, let's hope I'll be wiser in the future.
2017-07-05 23:35:00 -07:00

102 lines
2.5 KiB
Go

package models_test
import (
"testing"
"time"
"github.com/cloudflare/unsee/models"
)
type alertStateTest struct {
alert models.Alert
isSilenced bool
isInhibited bool
isActive bool
}
var alertStateTests = []alertStateTest{
alertStateTest{
alert: models.Alert{
State: models.AlertStateActive,
},
isActive: true,
},
alertStateTest{
alert: models.Alert{
State: models.AlertStateSuppressed,
InhibitedBy: []string{"1234"},
},
isInhibited: true,
},
alertStateTest{
alert: models.Alert{
State: models.AlertStateSuppressed,
SilencedBy: []string{"1234"},
},
isSilenced: true,
},
}
func TestAlertState(t *testing.T) {
for _, testCase := range alertStateTests {
if testCase.alert.IsActive() != testCase.isActive {
t.Errorf("alert.IsActive() returned %t while %t was expected for alert %v",
testCase.alert.IsActive(), testCase.isActive, testCase.alert)
}
if testCase.alert.IsInhibited() != testCase.isInhibited {
t.Errorf("alert.IsInhibited() returned %t while %t was expected for alert %v",
testCase.alert.IsInhibited(), testCase.isInhibited, testCase.alert)
}
if testCase.alert.IsSilenced() != testCase.isSilenced {
t.Errorf("alert.IsSilenced() returned %t while %t was expected for alert %v",
testCase.alert.IsSilenced(), testCase.isSilenced, testCase.alert)
}
}
}
func BenchmarkLabelsFingerprint(b *testing.B) {
alert := models.Alert{
Labels: map[string]string{
"foo1": "bar1",
"foo1bar1": "545jjjssd",
"foo1xxxx": "bdjjs88ff",
"agdfdfd": "bar1",
"fossdsf3o1": "bar11111",
"fdfdgfdgoo1": "bar1",
},
}
for n := 0; n < b.N; n++ {
alert.LabelsFingerprint()
}
}
func BenchmarkLabelsContent(b *testing.B) {
alert := models.Alert{
Annotations: map[string]string{
"foo": "bar",
"abc": "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...",
},
Labels: map[string]string{
"foo1": "bar1",
"foo1bar1": "545jjjssd",
"foo1xxxx": "bdjjs88ff",
"agdfdfd": "bar1",
"fossdsf3o1": "bar11111",
"fdfdgfdgoo1": "bar1",
},
State: models.AlertStateActive,
StartsAt: time.Date(2015, time.March, 10, 0, 0, 0, 0, time.UTC),
Alertmanager: []models.AlertmanagerInstance{
models.AlertmanagerInstance{
Name: "default",
URI: "http://localhost",
State: models.AlertStateActive,
},
},
}
alert.UpdateFingerprints()
for n := 0; n < b.N; n++ {
alert.LabelsFingerprint()
}
}