github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/monitoring/testonly/metrics.go (about) 1 // Copyright 2017 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package testonly 16 17 import ( 18 "testing" 19 20 "github.com/google/trillian/monitoring" 21 ) 22 23 // TestCounter runs a test on a Counter produced from the provided MetricFactory. 24 func TestCounter(t *testing.T, factory monitoring.MetricFactory) { 25 var tests = []struct { 26 name string 27 labelNames []string 28 labelVals []string 29 }{ 30 { 31 name: "counter0", 32 labelNames: nil, 33 labelVals: nil, 34 }, 35 { 36 name: "counter1", 37 labelNames: []string{"key1"}, 38 labelVals: []string{"val1"}, 39 }, 40 { 41 name: "counter2", 42 labelNames: []string{"key1", "key2"}, 43 labelVals: []string{"val1", "val2"}, 44 }, 45 } 46 for _, test := range tests { 47 counter := factory.NewCounter("test_"+test.name, "Test only", test.labelNames...) 48 if got, want := counter.Value(test.labelVals...), 0.0; got != want { 49 t.Errorf("Counter(test_%s)[%v].Value()=%v; want %v", test.name, test.labelVals, got, want) 50 } 51 counter.Inc(test.labelVals...) 52 if got, want := counter.Value(test.labelVals...), 1.0; got != want { 53 t.Errorf("Counter(test_%s)[%v].Value()=%v; want %v", test.name, test.labelVals, got, want) 54 } 55 counter.Add(2.5, test.labelVals...) 56 if got, want := counter.Value(test.labelVals...), 3.5; got != want { 57 t.Errorf("Counter(test_%s)[%v].Value()=%v; want %v", test.name, test.labelVals, got, want) 58 } 59 // Use an invalid number of labels. 60 libels := append(test.labelVals, "bogus") 61 counter.Add(10.0, libels...) 62 counter.Inc(libels...) 63 if got, want := counter.Value(libels...), 0.0; got != want { 64 t.Errorf("Counter(test_%s)[%v].Value()=%v; want %v", test.name, test.labelVals, got, want) 65 } 66 } 67 } 68 69 // TestGauge runs a test on a Gauge produced from the provided MetricFactory. 70 func TestGauge(t *testing.T, factory monitoring.MetricFactory) { 71 var tests = []struct { 72 name string 73 labelNames []string 74 labelVals []string 75 }{ 76 { 77 name: "gauge0", 78 labelNames: nil, 79 labelVals: nil, 80 }, 81 { 82 name: "gauge1", 83 labelNames: []string{"key1"}, 84 labelVals: []string{"val1"}, 85 }, 86 { 87 name: "gauge2", 88 labelNames: []string{"key1", "key2"}, 89 labelVals: []string{"val1", "val2"}, 90 }, 91 } 92 for _, test := range tests { 93 gauge := factory.NewGauge("test_"+test.name, "Test only", test.labelNames...) 94 if got, want := gauge.Value(test.labelVals...), 0.0; got != want { 95 t.Errorf("Gauge(test_%s)[%v].Value()=%v; want %v", test.name, test.labelVals, got, want) 96 } 97 gauge.Inc(test.labelVals...) 98 if got, want := gauge.Value(test.labelVals...), 1.0; got != want { 99 t.Errorf("Gauge(test_%s)[%v].Value()=%v; want %v", test.name, test.labelVals, got, want) 100 } 101 gauge.Dec(test.labelVals...) 102 if got, want := gauge.Value(test.labelVals...), 0.0; got != want { 103 t.Errorf("Gauge(test_%s)[%v].Value()=%v; want %v", test.name, test.labelVals, got, want) 104 } 105 gauge.Add(2.5, test.labelVals...) 106 if got, want := gauge.Value(test.labelVals...), 2.5; got != want { 107 t.Errorf("Gauge(test_%s)[%v].Value()=%v; want %v", test.name, test.labelVals, got, want) 108 } 109 gauge.Set(42.0, test.labelVals...) 110 if got, want := gauge.Value(test.labelVals...), 42.0; got != want { 111 t.Errorf("Gauge(test_%s)[%v].Value()=%v; want %v", test.name, test.labelVals, got, want) 112 } 113 // Use an invalid number of labels. 114 libels := append(test.labelVals, "bogus") 115 gauge.Add(10.0, libels...) 116 gauge.Inc(libels...) 117 gauge.Dec(libels...) 118 gauge.Set(120.0, libels...) 119 // Ask for an invalid number of labels. 120 if got, want := gauge.Value(libels...), 0.0; got != want { 121 t.Errorf("Counter(test_%s)[%v].Value()=%v; want %v", test.name, test.labelVals, got, want) 122 } 123 } 124 } 125 126 // TestHistogram runs a test on a Histogram produced from the provided MetricFactory. 127 func TestHistogram(t *testing.T, factory monitoring.MetricFactory) { 128 var tests = []struct { 129 name string 130 labelNames []string 131 labelVals []string 132 }{ 133 { 134 name: "histogram0", 135 labelNames: nil, 136 labelVals: nil, 137 }, 138 { 139 name: "histogram1", 140 labelNames: []string{"key1"}, 141 labelVals: []string{"val1"}, 142 }, 143 { 144 name: "histogram2", 145 labelNames: []string{"key1", "key2"}, 146 labelVals: []string{"val1", "val2"}, 147 }, 148 } 149 for _, test := range tests { 150 histogram := factory.NewHistogram("test_"+test.name, "Test only", test.labelNames...) 151 gotCount, gotSum := histogram.Info(test.labelVals...) 152 if wantCount, wantSum := uint64(0), 0.0; gotCount != wantCount || gotSum != wantSum { 153 t.Errorf("Gauge(test_%s)[%v].Value()=%v,%v; want %v,%v", test.name, test.labelVals, gotCount, gotSum, wantCount, wantSum) 154 } 155 histogram.Observe(1.0, test.labelVals...) 156 histogram.Observe(2.0, test.labelVals...) 157 histogram.Observe(3.0, test.labelVals...) 158 gotCount, gotSum = histogram.Info(test.labelVals...) 159 if wantCount, wantSum := uint64(3), 6.0; gotCount != wantCount || gotSum != wantSum { 160 t.Errorf("Gauge(test_%s)[%v].Value()=%v,%v; want %v,%v", test.name, test.labelVals, gotCount, gotSum, wantCount, wantSum) 161 } 162 163 // Use an invalid number of labels. 164 libels := append(test.labelVals, "bogus") 165 histogram.Observe(100.0, libels...) 166 histogram.Observe(200.0, libels...) 167 gotCount, gotSum = histogram.Info(libels...) 168 if wantCount, wantSum := uint64(0), 0.0; gotCount != wantCount || gotSum != wantSum { 169 t.Errorf("Gauge(test_%s)[%v].Value()=%v,%v; want %v,%v", test.name, test.labelVals, gotCount, gotSum, wantCount, wantSum) 170 } 171 } 172 }