// Test of the ETM monitor. func TestEtmMonitor(t *testing.T) { assert := audit.NewTestingAssertion(t, true) // Generate measurings. for i := 0; i < 500; i++ { n := rand.Intn(10) id := fmt.Sprintf("mp:task:%d", n) m := monitoring.BeginMeasuring(id) work(n * 5000) m.EndMeasuring() } // Need some time to let that backend catch up queued mesurings. time.Sleep(time.Millisecond) // Asserts. mp, err := monitoring.ReadMeasuringPoint("foo") assert.ErrorMatch(err, `.* measuring point "foo" does not exist`) mp, err = monitoring.ReadMeasuringPoint("mp:task:5") assert.Nil(err, "No error expected.") assert.Equal(mp.Id, "mp:task:5", "should get the right one") assert.True(mp.Count > 0, "should be measured several times") assert.Match(mp.String(), `Measuring Point "mp:task:5" \(.*\)`, "string representation should look fine") monitoring.MeasuringPointsDo(func(mp *monitoring.MeasuringPoint) { assert.Match(mp.Id, "mp:task:[0-9]", "id has to match the pattern") assert.True(mp.MinDuration <= mp.AvgDuration && mp.AvgDuration <= mp.MaxDuration, "avg should be somewhere between min and max") }) }
// TestBackendSwitch tests the correct switching between backends. func TestBackendSwitch(t *testing.T) { assert := audit.NewTestingAssertion(t, true) sleep := 10 * time.Millisecond // First standard. monitoring.SetBackend(monitoring.NewStandardBackend()) monitoring.Measure("test-a", func() { time.Sleep(sleep) }) time.Sleep(sleep) mp, err := monitoring.ReadMeasuringPoint("test-a") assert.Nil(err) assert.Equal(mp.Count(), int64(1)) assert.True(sleep <= mp.AvgDuration() && mp.AvgDuration() <= 2*sleep) // Then null. monitoring.SetBackend(monitoring.NewNullBackend()) monitoring.Measure("test", func() { time.Sleep(sleep) }) mp, err = monitoring.ReadMeasuringPoint("test") assert.Nil(err) assert.Equal(mp.ID(), "null") assert.Equal(mp.Count(), int64(0)) // Finally standard again. monitoring.SetBackend(monitoring.NewStandardBackend()) monitoring.Measure("test-b", func() { time.Sleep(sleep) }) time.Sleep(sleep) mp, err = monitoring.ReadMeasuringPoint("test-a") assert.ErrorMatch(err, `.* measuring point "test-a" does not exist`) mp, err = monitoring.ReadMeasuringPoint("test-b") assert.Nil(err) assert.Equal(mp.Count(), int64(1)) assert.True(sleep <= mp.AvgDuration() && mp.AvgDuration() <= 2*sleep) }