Skip to content

Commit

Permalink
tests(checker): Add checker tests and benchmarks
Browse files Browse the repository at this point in the history
Add checker tests before improvement of advanced mode and benchmark to
compare performance before and after improvements.

Relates #428
  • Loading branch information
litleleprikon committed Nov 26, 2019
1 parent 9f49388 commit 6b2e022
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 7 deletions.
172 changes: 166 additions & 6 deletions checker/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import (
"time"

"github.com/golang/mock/gomock"
"github.com/moira-alert/moira/metric_source"
"github.com/moira-alert/moira"
metricSource "github.com/moira-alert/moira/metric_source"
"github.com/moira-alert/moira/metric_source/local"
"github.com/moira-alert/moira/metric_source/remote"
"github.com/moira-alert/moira/mock/metric_source"
"github.com/moira-alert/moira/metrics/graphite/go-metrics"
mock_metric_source "github.com/moira-alert/moira/mock/metric_source"
mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert"
"github.com/op/go-logging"
. "github.com/smartystreets/goconvey/convey"

"github.com/moira-alert/moira"
"github.com/moira-alert/moira/metrics/graphite/go-metrics"
"github.com/moira-alert/moira/mock/moira-alert"
)

func TestGetMetricDataState(t *testing.T) {
Expand Down Expand Up @@ -1288,3 +1287,164 @@ func TestHandleTriggerCheck(t *testing.T) {
So(actual, ShouldResemble, expected)
})
}

func TestTriggerChecker_Check(t *testing.T) {
mockCtrl := gomock.NewController(t)
dataBase := mock_moira_alert.NewMockDatabase(mockCtrl)
source := mock_metric_source.NewMockMetricSource(mockCtrl)
fetchResult := mock_metric_source.NewMockFetchResult(mockCtrl)
logger, _ := logging.GetLogger("Test")
defer mockCtrl.Finish()

var retention int64 = 10
var warnValue float64 = 10
var errValue float64 = 20
pattern := "super.puper.pattern"
metric := "super.puper.metric"

var ttl int64 = 30

triggerChecker := TriggerChecker{
triggerID: "SuperId",
database: dataBase,
source: source,
logger: logger,
config: &Config{
MetricsTTLSeconds: 10,
},
metrics: metrics.ConfigureCheckerMetrics("checker", false).LocalMetrics,
from: 17,
until: 67,
ttl: ttl,
ttlState: moira.TTLStateNODATA,
trigger: &moira.Trigger{
Name: "Super trigger",
ErrorValue: &errValue,
WarnValue: &warnValue,
TriggerType: moira.RisingTrigger,
Targets: []string{pattern},
Patterns: []string{pattern},
},
lastCheck: &moira.CheckData{
State: moira.StateOK,
Timestamp: 57,
Metrics: map[string]moira.MetricState{
metric: {
State: moira.StateOK,
Timestamp: 26,
},
},
},
}
lastValue := float64(4)
eventMetrics := map[string]moira.MetricState{
metric: {
EventTimestamp: 17,
State: moira.StateOK,
Suppressed: false,
Timestamp: 57,
Value: &lastValue,
},
}

lastCheck := moira.CheckData{
Metrics: eventMetrics,
State: moira.StateOK,
Timestamp: triggerChecker.until,
EventTimestamp: 0,
Score: 0,
LastSuccessfulCheckTimestamp: triggerChecker.until,
}

dataBase.EXPECT().RemoveMetricsValues([]string{metric}, int64(57)).Return(nil)
source.EXPECT().Fetch(pattern, triggerChecker.from, triggerChecker.until, true).Return(fetchResult, nil)
fetchResult.EXPECT().GetMetricsData().Return([]*metricSource.MetricData{metricSource.MakeMetricData(metric, []float64{0, 1, 2, 3, 4}, retention, triggerChecker.from)})
fetchResult.EXPECT().GetPatternMetrics().Return([]string{metric}, nil)
dataBase.EXPECT().SetTriggerLastCheck(triggerChecker.triggerID, &lastCheck, triggerChecker.trigger.IsRemote).Return(nil)
_ = triggerChecker.Check()

}
func BenchmarkTriggerChecker_Check(b *testing.B) {
if testing.Short() {
b.Skip()
}
b.ReportAllocs()
mockCtrl := gomock.NewController(b)
dataBase := mock_moira_alert.NewMockDatabase(mockCtrl)
source := mock_metric_source.NewMockMetricSource(mockCtrl)
fetchResult := mock_metric_source.NewMockFetchResult(mockCtrl)
logger, _ := logging.GetLogger("Test")
defer mockCtrl.Finish()

var retention int64 = 10
var warnValue float64 = 10
var errValue float64 = 20
pattern := "super.puper.pattern"
metric := "super.puper.metric"

var ttl int64 = 30

triggerChecker := TriggerChecker{
triggerID: "SuperId",
database: dataBase,
source: source,
logger: logger,
config: &Config{
MetricsTTLSeconds: 10,
},
metrics: metrics.ConfigureCheckerMetrics("checker", false).LocalMetrics,
from: 17,
until: 67,
ttl: ttl,
ttlState: moira.TTLStateNODATA,
trigger: &moira.Trigger{
Name: "Super trigger",
ErrorValue: &errValue,
WarnValue: &warnValue,
TriggerType: moira.RisingTrigger,
Targets: []string{pattern},
Patterns: []string{pattern},
},
lastCheck: &moira.CheckData{
State: moira.StateOK,
Timestamp: 57,
Metrics: map[string]moira.MetricState{
metric: {
State: moira.StateOK,
Timestamp: 26,
},
},
},
}
lastValue := float64(4)
eventMetrics := map[string]moira.MetricState{
metric: {
EventTimestamp: 17,
State: moira.StateOK,
Suppressed: false,
Timestamp: 57,
Value: &lastValue,
},
}

lastCheck := moira.CheckData{
Metrics: eventMetrics,
State: moira.StateOK,
Timestamp: triggerChecker.until,
EventTimestamp: 0,
Score: 0,
LastSuccessfulCheckTimestamp: triggerChecker.until,
}

dataBase.EXPECT().RemoveMetricsValues([]string{metric}, int64(57)).Return(nil).AnyTimes()
source.EXPECT().Fetch(pattern, triggerChecker.from, triggerChecker.until, true).Return(fetchResult, nil).AnyTimes()
fetchResult.EXPECT().GetMetricsData().Return([]*metricSource.MetricData{metricSource.MakeMetricData(metric, []float64{0, 1, 2, 3, 4}, retention, triggerChecker.from)}).AnyTimes()
fetchResult.EXPECT().GetPatternMetrics().Return([]string{metric}, nil).AnyTimes()
dataBase.EXPECT().SetTriggerLastCheck(triggerChecker.triggerID, &lastCheck, triggerChecker.trigger.IsRemote).Return(nil).AnyTimes()
for n := 0; n < b.N; n++ {
err := triggerChecker.Check()
if err != nil {
b.Errorf("Check() returned error: %w", err)
}
}
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ require (
github.com/gofrs/uuid v3.2.0+incompatible
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/golang/mock v1.3.1
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
github.com/golangci/go-tools v0.0.0-20190318055746-e32c54105b7c // indirect
github.com/golangci/golangci-lint v1.19.1 // indirect
github.com/golangci/gosec v0.0.0-20190211064107-66fb7fc33547 // indirect
Expand Down

0 comments on commit 6b2e022

Please sign in to comment.