blob: 1e4f628a0396799173d499b600612c297047845e [file] [log] [blame]
Jian Li64dd8892015-12-30 14:13:18 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jian Li64dd8892015-12-30 14:13:18 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.codec.impl;
17
18import com.codahale.metrics.Counter;
19import com.codahale.metrics.Meter;
20import com.codahale.metrics.Metric;
21import com.codahale.metrics.Timer;
22import com.fasterxml.jackson.databind.JsonNode;
23import org.hamcrest.Description;
24import org.hamcrest.TypeSafeDiagnosingMatcher;
25
26import java.util.concurrent.TimeUnit;
27
28/**
29 * Hamcrest matcher for metrics.
30 */
31public final class MetricJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
32
33 private final Metric metric;
34
35 private MetricJsonMatcher(Metric metric) {
36 this.metric = metric;
37 }
38
39 @Override
40 protected boolean matchesSafely(JsonNode jsonMetric, Description description) {
41
42 // check counter
43 if (metric instanceof Counter) {
44 Counter counter = (Counter) metric;
45 long jsonCounter = jsonMetric.get("counter").asLong();
46 long counterVal = counter.getCount();
47 if (jsonCounter != counterVal) {
48 description.appendText("counter was " + jsonCounter);
49 return false;
50 }
51 }
52
53 // check meter
54 if (metric instanceof Meter) {
55 Meter meter = (Meter) metric;
56
57 long jsonCounter = jsonMetric.get("counter").asLong();
58 long counterVal = meter.getCount();
59 if (jsonCounter != counterVal) {
60 description.appendText("counter was " + jsonCounter);
61 return false;
62 }
63
64 double jsonOneMinRate = jsonMetric.get("1_min_rate").asDouble();
65 double oneMinRate = meter.getOneMinuteRate();
66 if (jsonOneMinRate != oneMinRate) {
67 description.appendText("one minute rate was " + jsonOneMinRate);
68 return false;
69 }
70
71 double jsonFiveMinRate = jsonMetric.get("5_min_rate").asDouble();
72 double fiveMinRate = meter.getFiveMinuteRate();
73 if (jsonFiveMinRate != fiveMinRate) {
74 description.appendText("five minute rate was " + jsonFiveMinRate);
75 return false;
76 }
77
78 double jsonFiftMinRate = jsonMetric.get("15_min_rate").asDouble();
79 double fiftMinRate = meter.getFifteenMinuteRate();
80 if (jsonFiftMinRate != fiftMinRate) {
81 description.appendText("fifteen minute rate was " + jsonFiftMinRate);
82 return false;
83 }
84 }
85
86 // check timer
87 if (metric instanceof Timer) {
88 Timer timer = (Timer) metric;
89
90 long jsonCounter = jsonMetric.get("counter").asLong();
91 long counterVal = timer.getCount();
92 if (jsonCounter != counterVal) {
93 description.appendText("counter was " + jsonCounter);
94 return false;
95 }
96
97 double jsonOneMinRate = jsonMetric.get("1_min_rate").asDouble();
98 double oneMinRate = timer.getOneMinuteRate();
99 if (jsonOneMinRate != oneMinRate) {
100 description.appendText("one minute rate was " + jsonOneMinRate);
101 return false;
102 }
103
104 double jsonFiveMinRate = jsonMetric.get("5_min_rate").asDouble();
105 double fiveMinRate = timer.getFiveMinuteRate();
106 if (jsonFiveMinRate != fiveMinRate) {
107 description.appendText("five minute rate was " + jsonFiveMinRate);
108 return false;
109 }
110
111 double jsonFiftMinRate = jsonMetric.get("15_min_rate").asDouble();
112 double fiftMinRate = timer.getFifteenMinuteRate();
113 if (jsonFiftMinRate != fiftMinRate) {
114 description.appendText("fifteen minute rate was " + jsonFiftMinRate);
115 return false;
116 }
117
118 double jsonMean = jsonMetric.get("mean").asDouble();
119 double mean = nanoToMs(timer.getSnapshot().getMean());
120 if (jsonMean != mean) {
121 description.appendText("mean was " + jsonMean);
122 return false;
123 }
124
125 double jsonMin = jsonMetric.get("min").asDouble();
126 double min = nanoToMs(timer.getSnapshot().getMin());
127 if (jsonMin != min) {
128 description.appendText("json min was " + jsonMin);
129 return false;
130 }
131
132 double jsonMax = jsonMetric.get("max").asDouble();
133 double max = nanoToMs(timer.getSnapshot().getMax());
134 if (jsonMax != max) {
135 description.appendText("max was " + jsonMax);
136 return false;
137 }
138
139 double jsonStdDev = jsonMetric.get("stddev").asDouble();
140 double stdDev = nanoToMs(timer.getSnapshot().getStdDev());
141 if (jsonStdDev != stdDev) {
142 description.appendText("stddev was " + jsonStdDev);
143 return false;
144 }
145 }
146
147 return true;
148 }
149
150 @Override
151 public void describeTo(Description description) {
152 description.appendText(metric.toString());
153 }
154
155 /**
156 * Factory to allocate a metric matcher.
157 *
158 * @param metric metric object we are looking for
159 * @return matcher
160 */
161 public static MetricJsonMatcher matchesMetric(Metric metric) {
162 return new MetricJsonMatcher(metric);
163 }
164
165 private double nanoToMs(double nano) {
166 return TimeUnit.MILLISECONDS.convert((long) nano, TimeUnit.NANOSECONDS);
167 }
168}