|
| 1 | +package monitoring |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +func TestSimpleStartStop(t *testing.T) { |
| 12 | + go Start(1234, "/random", 100) |
| 13 | + time.Sleep(time.Millisecond * 100) |
| 14 | + Stop() |
| 15 | +} |
| 16 | + |
| 17 | +func TestStartMultipleStop(t *testing.T) { |
| 18 | + go Start(1234, "/random", 100) |
| 19 | + time.Sleep(time.Millisecond * 100) |
| 20 | + Stop() |
| 21 | + Stop() |
| 22 | +} |
| 23 | + |
| 24 | +func cleaningUpAfterTest() { |
| 25 | + endChannel <- struct{}{} |
| 26 | + <-endChannel |
| 27 | + closeChannels() |
| 28 | + metrics = make([]Metric, 0) |
| 29 | +} |
| 30 | + |
| 31 | +func initTest() { |
| 32 | + // we need message channel to block so we don't end to quickly |
| 33 | + initChannels(0) |
| 34 | + go eventLoop() |
| 35 | +} |
| 36 | + |
| 37 | +// decorator function that properly inits and cleans after higher level test of Monitoring package |
| 38 | +func testFunction(t *testing.T, testToRun func(*testing.T)) { |
| 39 | + initTest() |
| 40 | + testToRun(t) |
| 41 | + cleaningUpAfterTest() |
| 42 | +} |
| 43 | + |
| 44 | +func TestSendingSingleMetric(t *testing.T) { |
| 45 | + testFunction(t, func(t *testing.T) { |
| 46 | + metric := Metric{Name: "test"} |
| 47 | + Send(metric) |
| 48 | + if len(metrics) != 1 { |
| 49 | + t.Error("wrong number of metrics, should be 1") |
| 50 | + } |
| 51 | + |
| 52 | + if metrics[0].Name != "test" { |
| 53 | + t.Errorf("Got wrong name %s in stored metric", metrics[0].Name) |
| 54 | + } |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +func TestExportingMetrics(t *testing.T) { |
| 59 | + testFunction(t, func(t *testing.T) { |
| 60 | + metric := Metric{Name: "test"} |
| 61 | + Send(metric) |
| 62 | + |
| 63 | + metricsRequestChannel <- struct{}{} |
| 64 | + metrics := <-metricsToRequest |
| 65 | + |
| 66 | + if len(metrics) != 1 { |
| 67 | + t.Errorf("Got wrong amount of metrics %d, expected 1", len(metrics)) |
| 68 | + } |
| 69 | + |
| 70 | + if metrics[0].Name != "test" { |
| 71 | + t.Errorf("Got wrong name of metric %s, expected test", metrics[0].Name) |
| 72 | + } |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +func TestHttpRun(t *testing.T) { |
| 77 | + go Start(12345, "/metrics", 10) |
| 78 | + defer Stop() |
| 79 | + |
| 80 | + time.Sleep(time.Second) |
| 81 | + |
| 82 | + metric := Metric{Name: "test"} |
| 83 | + metric.Timestamp = 10 |
| 84 | + metric.AddTag("tag1", 42) |
| 85 | + metric.AddValue("value1", 11) |
| 86 | + Send(metric) |
| 87 | + |
| 88 | + response, err := http.Get("http://localhost:12345/metrics") |
| 89 | + if err != nil { |
| 90 | + t.Fatalf("Failed to GET metrics at port 12345: %v", err) |
| 91 | + } |
| 92 | + decoder := json.NewDecoder(response.Body) |
| 93 | + var receivedMetrics []Metric |
| 94 | + if err = decoder.Decode(&receivedMetrics); err != nil { |
| 95 | + t.Fatalf("Failed to decoded Metric: %v", err) |
| 96 | + } |
| 97 | + |
| 98 | + receivedMetric := receivedMetrics[0] |
| 99 | + |
| 100 | + if receivedMetric.Name != "test" { |
| 101 | + t.Errorf("Got wrong name of metric %s, expected test", receivedMetric.Name) |
| 102 | + } |
| 103 | + |
| 104 | + if receivedMetric.Timestamp != 10 { |
| 105 | + t.Errorf("Got wrong timestamp of metric %d, expected 10", receivedMetric.Timestamp) |
| 106 | + } |
| 107 | + |
| 108 | + if len(receivedMetric.Tags) != 1 { |
| 109 | + t.Errorf("Got wrong number of tags %d, expected 1", len(receivedMetric.Tags)) |
| 110 | + } |
| 111 | + |
| 112 | + if receivedMetric.Tags["tag1"].(float64) != 42 { |
| 113 | + t.Error("Failed to retreive tags: tag1 with value 42") |
| 114 | + } |
| 115 | + |
| 116 | + if len(receivedMetric.Values) != 1 { |
| 117 | + t.Errorf("Got wrong number of values %d, expected 1", len(receivedMetric.Values)) |
| 118 | + } |
| 119 | + |
| 120 | + if receivedMetric.Values["value1"].(float64) != 11 { |
| 121 | + t.Error("Failed to retreive tags: value1 with value 11") |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// This benchmark cannot be run for too long as it will fill whole RAM even with |
| 126 | +// results: |
| 127 | +// goos: linux |
| 128 | +// goarch: amd64 |
| 129 | +// pkg: github.com/AliceO2Group/Control/common/monitoring |
| 130 | +// cpu: 11th Gen Intel(R) Core(TM) i9-11900H @ 2.50GHz |
| 131 | +// BenchmarkSendingMetrics-16 |
| 132 | +// |
| 133 | +// 123365481 192.6 ns/op |
| 134 | +// PASS |
| 135 | +// ok github.com/AliceO2Group/Control/common/monitoring 44.686s |
| 136 | +func BenchmarkSendingMetrics(b *testing.B) { |
| 137 | + Start(12345, "/metrics", 100) |
| 138 | + |
| 139 | + // this goroutine keeps clearing results so RAM does not exhausted |
| 140 | + go func() { |
| 141 | + for { |
| 142 | + select { |
| 143 | + case <-endChannel: |
| 144 | + endChannel <- struct{}{} |
| 145 | + break |
| 146 | + default: |
| 147 | + if len(metrics) >= 10000000 { |
| 148 | + metricsRequestChannel <- struct{}{} |
| 149 | + <-metricsToRequest |
| 150 | + } |
| 151 | + } |
| 152 | + time.Sleep(100 * time.Millisecond) |
| 153 | + } |
| 154 | + }() |
| 155 | + |
| 156 | + defer Stop() |
| 157 | + |
| 158 | + metric := Metric{Name: "testname", Timestamp: 12345} |
| 159 | + metric.AddValue("value", 42) |
| 160 | + metric.AddTag("tag", 40) |
| 161 | + |
| 162 | + for i := 0; i < b.N; i++ { |
| 163 | + Send(metric) |
| 164 | + } |
| 165 | + |
| 166 | + fmt.Println("") |
| 167 | +} |
0 commit comments