/** * Returns the first measurement with a given tag value. * * @param ms * A set of measurements. * @param k * Key to search for. * @param v * Value that should be associated with k on the ids. * @return * Measurement or null if no matches are found. */ public static Measurement first(final Iterable<Measurement> ms, final String k, final String v) { return first(ms, value -> v.equals(getTagValue(value.id(), k))); }
/** * Returns the first measurement with a given tag value. * * @param ms * A set of measurements. * @param k * Key to search for. * @param v * Value that should be associated with k on the ids. * @return * Measurement or null if no matches are found. */ public static Measurement first(final Iterable<Measurement> ms, final String k, final String v) { return first(ms, value -> v.equals(getTagValue(value.id(), k))); }
/** * Returns the first measurement with a given tag value. * * @param ms * A set of measurements. * @param t * The key and value to search for. * @return * Measurement or null if no matches are found. */ public static Measurement first(Iterable<Measurement> ms, Tag t) { return first(ms, t.key(), t.value()); }
/** * Returns the first measurement with a given tag value. * * @param ms * A set of measurements. * @param t * The key and value to search for. * @return * Measurement or null if no matches are found. */ public static Measurement first(Iterable<Measurement> ms, Tag t) { return first(ms, t.key(), t.value()); }
@Test public void totalOfSquaresOverflow() { final long seconds = 10; final long nanos = TimeUnit.SECONDS.toNanos(seconds); final BigInteger s = new BigInteger("" + nanos); final BigInteger s2 = s.multiply(s); Timer t = newTimer("foo"); t.record(seconds, TimeUnit.SECONDS); clock.setWallTime(61000L); final double v = Utils.first(t.measure(), Statistic.totalOfSquares).value(); final double factor = 1e9 * 1e9; final BigInteger perSec = s2.divide(BigInteger.valueOf(60)); Assertions.assertEquals(perSec.doubleValue() / factor, v, 1e-12); }
@Test public void firstPredicateEmpty() { List<Measurement> ms = newList(10); Measurement m = Utils.first(ms, v -> false); Assertions.assertEquals(null, m); }
@Test public void totalOfSquaresManySmallValues() { Timer t = newTimer("foo"); BigInteger sumOfSq = new BigInteger("0"); for (int i = 0; i < 100000; ++i) { final long nanos = i; final BigInteger s = new BigInteger("" + nanos); final BigInteger s2 = s.multiply(s); sumOfSq = sumOfSq.add(s2); t.record(i, TimeUnit.NANOSECONDS); } clock.setWallTime(61000L); final double v = Utils.first(t.measure(), Statistic.totalOfSquares).value(); final double factor = 1e9 * 1e9; sumOfSq = sumOfSq.divide(BigInteger.valueOf(60)); Assertions.assertEquals(sumOfSq.doubleValue() / factor, v, 1e-12); }
@Test public void totalOfSquaresManyBigValues() { Timer t = newTimer("foo"); BigInteger sumOfSq = new BigInteger("0"); for (int i = 0; i < 100000; ++i) { final long nanos = TimeUnit.SECONDS.toNanos(i); final BigInteger s = new BigInteger("" + nanos); final BigInteger s2 = s.multiply(s); sumOfSq = sumOfSq.add(s2); t.record(i, TimeUnit.SECONDS); } clock.setWallTime(61000L); final double v = Utils.first(t.measure(), Statistic.totalOfSquares).value(); // Expected :3.3332833335E14 // Actual :3.3332833334999825E14 final double factor = 1e9 * 1e9; sumOfSq = sumOfSq.divide(BigInteger.valueOf(60)); Assertions.assertEquals(sumOfSq.doubleValue() / factor, v, 2.0); }
@Test public void testMeasure() { Timer t = newTimer("foo"); t.record(42, TimeUnit.MILLISECONDS); clock.setWallTime(61000L); for (Measurement m : t.measure()) { Assertions.assertEquals(m.timestamp(), 61000L); final double count = Utils.first(t.measure(), Statistic.count).value(); final double totalTime = Utils.first(t.measure(), Statistic.totalTime).value(); Assertions.assertEquals(count, 1.0 / 60.0, 0.1e-12); Assertions.assertEquals(totalTime, 42e-3 / 60.0, 0.1e-12); } }
@Test public void readLatency() throws Exception { Registry r = new DefaultRegistry(new ManualClock()); List<JmxConfig> configs = configs(); JmxData data = timer("keyspace=test,name=ReadLatency,scope=foo,type=ColumnFamily", 0); List<Measurement> ms = measure(r, configs, data); Assertions.assertEquals(7, ms.size()); Assertions.assertEquals( 50.0e-4, Utils.first(ms, "statistic", "percentile_50").value(), 1e-12); data = timer("keyspace=test,name=ReadLatency,scope=foo,type=ColumnFamily", 1); ms = measure(r, configs, data); Assertions.assertEquals(7, ms.size()); Assertions.assertEquals( 50.01e-4, Utils.first(ms, "statistic", "percentile_50").value(), 1e-12); }
@Test public void testMeasure() { Registry r = new DefaultRegistry(clock); clock.setWallTime(61000L); Id id = r.createId("test"); Counter c = IntervalCounter.get(r, id); // all meters should have the correct timestamp r.stream().forEach(meter -> { for (Measurement m : meter.measure()) { Assertions.assertEquals(m.timestamp(), 61000L); } }); final List<Measurement> measurements = getAllMeasurements(r); final double initAge = Utils.first(measurements, Statistic.duration).value(); final double initCount = Utils.first(measurements, Statistic.count).value(); Assertions.assertEquals(61.0, initAge, EPSILON); Assertions.assertEquals(0.0, initCount, EPSILON); clock.setWallTime(120000L); c.increment(); final List<Measurement> afterMeasurements = getAllMeasurements(r); final double afterAge = Utils.first(afterMeasurements, Statistic.duration).value(); final double afterCount = Utils.first(afterMeasurements, Statistic.count).value(); Assertions.assertEquals(0.0, afterAge, EPSILON); Assertions.assertEquals(1.0, afterCount, EPSILON); }
@Test public void firstTag() { List<Measurement> ms = newList(10); Tag t = new BasicTag("i", "7"); Measurement m = Utils.first(ms, t); Assertions.assertEquals(m.id().tags(), ArrayTagSet.create(t)); }
@Test public void readLatencyNoActivity() throws Exception { Registry r = new DefaultRegistry(new ManualClock()); List<JmxConfig> configs = configs(); JmxData data = timer("keyspace=test,name=ReadLatency,scope=foo,type=ColumnFamily", 0); List<Measurement> ms = measure(r, configs, data); Assertions.assertEquals(7, ms.size()); Assertions.assertEquals( 50.0e-4, Utils.first(ms, "statistic", "percentile_50").value(), 1e-12); data = timer("keyspace=test,name=ReadLatency,scope=foo,type=ColumnFamily", 0); ms = measure(r, configs, data); Assertions.assertEquals(7, ms.size()); Assertions.assertEquals( 0.0, Utils.first(ms, "statistic", "percentile_50").value(), 1e-12); data = timer("keyspace=test,name=ReadLatency,scope=foo,type=ColumnFamily", 1); ms = measure(r, configs, data); Assertions.assertEquals(7, ms.size()); Assertions.assertEquals( 50.01e-4, Utils.first(ms, "statistic", "percentile_50").value(), 1e-12); }
@Test public void firstKV() { List<Measurement> ms = newList(10); Tag t = new BasicTag("i", "7"); Measurement m = Utils.first(ms, "i", "7"); Assertions.assertEquals(m.id().tags(), ArrayTagSet.create(t)); }
@Test public void firstPredicate() { List<Measurement> ms = newList(10); Tag t = new BasicTag("i", "7"); Measurement m = Utils.first(ms, v -> v.value() == 7.0); Assertions.assertEquals(m.id().tags(), ArrayTagSet.create(t)); }