Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
| 3 | import com.codahale.metrics.Meter; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 4 | import net.onrc.onos.core.metrics.OnosMetrics; |
| 5 | import org.json.JSONArray; |
| 6 | import org.json.JSONException; |
| 7 | import org.json.JSONObject; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 8 | import org.junit.Test; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 9 | import org.restlet.resource.ClientResource; |
| 10 | |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 11 | import static org.hamcrest.MatcherAssert.assertThat; |
| 12 | import static org.hamcrest.Matchers.both; |
| 13 | import static org.hamcrest.Matchers.equalTo; |
| 14 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 15 | import static org.hamcrest.Matchers.is; |
| 16 | import static org.hamcrest.Matchers.lessThanOrEqualTo; |
| 17 | import static org.hamcrest.Matchers.notNullValue; |
| 18 | |
| 19 | /** |
| 20 | * Unit tests for REST APIs for Meter Metrics. |
| 21 | */ |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 22 | public class TestRestMetricsMeters extends TestRestMetrics { |
| 23 | |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 24 | // Test data for Meters |
| 25 | |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 26 | private static final OnosMetrics.MetricsComponent COMPONENT = |
| 27 | OnosMetrics.registerComponent("MetricsUnitTest"); |
| 28 | private static final OnosMetrics.MetricsFeature FEATURE = |
| 29 | COMPONENT.registerFeature("Meters"); |
| 30 | |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 31 | private static final String METER1_NAME = "METER1"; |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 32 | private static final String METER1_FULL_NAME = |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 33 | OnosMetrics.generateName(COMPONENT, FEATURE, METER1_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 34 | private static final int METER1_ITERATIONS = 1; |
| 35 | |
| 36 | private static final String METER2_NAME = "METER2"; |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 37 | private static final String METER2_FULL_NAME = |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 38 | OnosMetrics.generateName(COMPONENT, FEATURE, METER2_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 39 | private static final int METER2_ITERATIONS = 10; |
| 40 | |
| 41 | private static final String METER3_NAME = "METER3"; |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 42 | private static final String METER3_FULL_NAME = |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 43 | OnosMetrics.generateName(COMPONENT, FEATURE, METER3_NAME); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 44 | private static final int METER3_ITERATIONS = 100; |
| 45 | |
Ray Milkey | 40eb9c8 | 2014-07-18 10:28:11 -0700 | [diff] [blame] | 46 | private final Meter meter1 = new Meter(getMockClock()); |
| 47 | private final Meter meter2 = new Meter(getMockClock()); |
| 48 | private final Meter meter3 = new Meter(getMockClock()); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Fill in test data for a given Meter. |
| 52 | * |
| 53 | * @param meter Meter object to fill in |
| 54 | * @param iterations How many times to mark the meter |
| 55 | */ |
| 56 | private void fillMeter(final Meter meter, |
| 57 | final long iterations) { |
| 58 | for (int i = 0; i < iterations; i++) { |
| 59 | meter.mark(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Fill in test data in the test Meters. |
| 65 | */ |
| 66 | private void fillMeters() { |
| 67 | fillMeter(meter1, METER1_ITERATIONS); |
| 68 | fillMeter(meter2, METER2_ITERATIONS); |
| 69 | fillMeter(meter3, METER3_ITERATIONS); |
| 70 | |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 71 | OnosMetrics.registerMetric(COMPONENT, |
| 72 | FEATURE, |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 73 | METER1_NAME, |
| 74 | meter1); |
| 75 | |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 76 | OnosMetrics.registerMetric(COMPONENT, |
| 77 | FEATURE, |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 78 | METER2_NAME, |
| 79 | meter2); |
| 80 | |
Ray Milkey | 71cd2c8 | 2014-07-16 15:02:33 -0700 | [diff] [blame] | 81 | OnosMetrics.registerMetric(COMPONENT, |
| 82 | FEATURE, |
| 83 | METER3_NAME, |
| 84 | meter3); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Check if the data in a Meter matches the expected values. |
| 89 | * |
| 90 | * @param meter the meter object that this JSON representation should match |
| 91 | * @param meterContainer JSON object for the Meter |
| 92 | * @param name name of the Meter |
| 93 | * @param iterations count of marks that should be in the Meter |
| 94 | * @throws JSONException if any of the JSON processing fails |
| 95 | */ |
| 96 | private void checkMeter(final Meter meter, |
| 97 | final JSONObject meterContainer, |
| 98 | final String name, |
| 99 | final int iterations) |
| 100 | throws JSONException { |
| 101 | final String meterName = meterContainer.getString("name"); |
| 102 | assertThat(meterName, is(notNullValue())); |
| 103 | assertThat(meterName, is(equalTo(name))); |
| 104 | |
| 105 | final JSONObject meterObject = meterContainer.getJSONObject("meter"); |
| 106 | assertThat(meterObject, is(notNullValue())); |
| 107 | |
| 108 | final int meterCount = meterObject.getInt("count"); |
| 109 | assertThat(meterCount, is(equalTo(iterations))); |
| 110 | |
| 111 | final double m15Rate = meterObject.getDouble("m15_rate"); |
| 112 | assertThat(m15Rate, is(equalTo(meter.getFifteenMinuteRate()))); |
| 113 | |
| 114 | final double m5Rate = meterObject.getDouble("m5_rate"); |
| 115 | assertThat(m5Rate, is(equalTo(meter.getFiveMinuteRate()))); |
| 116 | |
| 117 | final double m1Rate = meterObject.getDouble("m1_rate"); |
| 118 | assertThat(m1Rate, is(equalTo(meter.getOneMinuteRate()))); |
| 119 | |
| 120 | // mean should be between 0.0 and the longest rate. Since all the rates |
| 121 | // are the same because of the mocked clock, use the 15 minute rate as |
| 122 | // the max - all the rates should be the same. |
| 123 | final double meanRate = meterObject.getDouble("mean_rate"); |
| 124 | assertThat(meanRate, |
| 125 | is(both(greaterThanOrEqualTo(0.0)). |
| 126 | and(lessThanOrEqualTo(m15Rate)))); |
| 127 | |
| 128 | final String units = meterObject.getString("units"); |
| 129 | assertThat(units, is(equalTo("events/second"))); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * UNIT test for the Metrics REST API for Meters. |
| 134 | * |
| 135 | * @throws JSONException if any JSON processing fails |
| 136 | */ |
| 137 | @Test |
| 138 | public void testMeters() throws JSONException { |
| 139 | |
| 140 | fillMeters(); |
| 141 | |
| 142 | // Read the metrics from the REST API for the test data |
| 143 | final ClientResource client = new ClientResource(getBaseRestMetricsUrl()); |
| 144 | |
| 145 | final JSONObject metrics = getJSONObject(client); |
| 146 | assertThat(metrics.length(), is(equalTo(5))); |
| 147 | |
| 148 | // There should be 3 meters |
| 149 | final JSONArray meters = metrics.getJSONArray("meters"); |
| 150 | assertThat(meters, is(notNullValue())); |
| 151 | assertThat(meters.length(), is(3)); |
| 152 | |
| 153 | // There should be no timers, gauges, histograms or counters |
| 154 | checkEmptyLists(metrics, "timers", "gauges", "histograms", "counters"); |
| 155 | |
| 156 | // Check the values for meter 1 |
| 157 | final JSONObject meter1Container = meters.getJSONObject(0); |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 158 | checkMeter(meter1, meter1Container, METER1_FULL_NAME, METER1_ITERATIONS); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 159 | |
| 160 | // Check the values for meter 2 |
| 161 | final JSONObject meter2Container = meters.getJSONObject(1); |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 162 | checkMeter(meter2, meter2Container, METER2_FULL_NAME, METER2_ITERATIONS); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 163 | |
| 164 | // Check the values for meter 3 |
| 165 | final JSONObject meter3Container = meters.getJSONObject(2); |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 166 | checkMeter(meter3, meter3Container, METER3_FULL_NAME, METER3_ITERATIONS); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 167 | |
| 168 | } |
| 169 | |
| 170 | } |
| 171 | |