Package com.netflix.servo.util

Examples of com.netflix.servo.util.ManualClock


        assertEquals(activeTasks.getValue().longValue(), 0L);
    }

    @Test
    public void testTimer() throws Exception {
        ManualClock clock = new ManualClock(0);
        DurationTimer timer = new DurationTimer(MonitorConfig.builder("test").build(), clock);
        Stopwatch s = timer.start();
        clock.set(10 * 1000L);
        assertEquals(10, s.getDuration());

        Monitor<Long> duration = getDuration(timer.getMonitors());
        Monitor<Long> activeTasks = getActiveTasks(timer.getMonitors());
        assertEquals(10L, duration.getValue().longValue());
        assertEquals(1L, activeTasks.getValue().longValue());
        clock.set(20 * 1000L);

        assertEquals(20L, duration.getValue().longValue());
        assertEquals(1L, activeTasks.getValue().longValue());

        Stopwatch anotherTask = timer.start();
        assertEquals(20L, duration.getValue().longValue());
        assertEquals(2L, activeTasks.getValue().longValue());

        clock.set(30 * 1000L);
        assertEquals(40L, duration.getValue().longValue()); // 30s for the first, 10s for the second
        assertEquals(2L, activeTasks.getValue().longValue());

        s.stop();
        assertEquals(10L, duration.getValue().longValue()); // 30s for the first, 10s for the second
View Full Code Here


        assertEquals(0L, activeTasks.getValue().longValue());
    }

    @Test
    public void testValue() throws Exception {
        ManualClock clock = new ManualClock(0L);
        DurationTimer timer = new DurationTimer(MonitorConfig.builder("test").build(), clock);
        assertEquals(0L, timer.getValue().longValue());
        Stopwatch s = timer.start();
        clock.set(10 * 1000L);
        assertEquals(10L, timer.getValue().longValue());
        s.stop();
        assertEquals(0L, timer.getValue().longValue());
    }
View Full Code Here

        assertEquals(0L, timer.getValue().longValue());
    }

    @Test
    public void testReset() throws Exception {
        ManualClock clock = new ManualClock(0L);
        DurationTimer timer = new DurationTimer(MonitorConfig.builder("test").build(), clock);
        Stopwatch s = timer.start();
        clock.set(10 * 1000L);
        assertEquals(10L, timer.getValue().longValue());
        s.reset();
        assertEquals(0L, timer.getValue().longValue());
        clock.set(20 * 1000L);
        assertEquals(10L, timer.getValue().longValue());
    }
View Full Code Here

        assertEquals(c.getValue(1).doubleValue(), 0.2);
    }

    @Test
    public void testGetValueTwice() {
        ManualClock manualClock = new ManualClock(0L);

        StepCounter c = new StepCounter(MonitorConfig.builder("test").build(), manualClock);
        c.increment();
        for (int i = 1; i < 10; ++i) {
            manualClock.set(i * 60000L);
            c.increment();
            c.getValue(0);
            assertEquals(c.getValue(0).doubleValue(), 1 / 60.0);
        }
    }
View Full Code Here

            return Objects.hashCode(t, v);
        }
    }

    void assertMetrics(long step, long heartbeat, List<Metric> input, List<TimeVal> expected) {
        ManualClock clock = new ManualClock(0);
        MemoryMetricObserver mmo = new MemoryMetricObserver("m", 1);
        MetricObserver transform = new NormalizationTransform(mmo, step, heartbeat,
                TimeUnit.MILLISECONDS, clock);

        int i = 0;
View Full Code Here

    private static final double DELTA = 1e-6;

    @Test
    public void testUpdate() throws Exception {
        BasicCounter basicCounter = new BasicCounter(MonitorConfig.builder("basicCounter").build());
        ManualClock manualClock = new ManualClock(0);
        StepCounter stepCounter = new StepCounter(MonitorConfig.builder("stepCounter").build(),
                manualClock);
        LongGauge gauge = new LongGauge(MonitorConfig.builder("longGauge").build());

        ImmutableList<? extends AbstractMonitor<Number>> monitors = ImmutableList.of(basicCounter,
                stepCounter, gauge);

        MemoryMetricObserver observer = new MemoryMetricObserver("normalization-test", 1);
        NormalizationTransform normalizationTransform = new NormalizationTransform(observer, 60,
                120, TimeUnit.SECONDS, manualClock);
        CounterToRateMetricTransform toRateMetricTransform =
                new CounterToRateMetricTransform(normalizationTransform, 60,
                        120, TimeUnit.SECONDS, manualClock);

        double[] rates = {0.5 / 60.0, 2 / 60.0, 3 / 60.0, 4 / 60.0};
        double[] expectedNormalized = {
                rates[0] * (2.0 / 3.0), // 20000L over stepBoundary
                rates[0] * (1.0 / 3.0) + rates[1] * (2.0 / 3.0),
                rates[1] * (1.0 / 3.0) + rates[2] * (2.0 / 3.0),
                rates[2] * (1.0 / 3.0) + rates[3] * (2.0 / 3.0)};

        for (int i = 1; i < 5; ++i) {
            long now = 20000L + i * 60000L;
            long stepBoundary = i * 60000L;
            manualClock.set(now);
            basicCounter.increment(i);
            stepCounter.increment(i);
            gauge.set((long) i);
            List<Metric> metrics = getValue(monitors, manualClock);
            toRateMetricTransform.update(metrics);

            List<Metric> o = observer.getObservations().get(0);
            assertEquals(o.size(), 3);
            double basicCounterVal = o.get(0).getNumberValue().doubleValue();
            double stepCounterVal = o.get(1).getNumberValue().doubleValue();
            double gaugeVal = o.get(2).getNumberValue().doubleValue();
            assertEquals(gaugeVal, (double) i, DELTA);
            // rate per second for the prev interval
            assertEquals(stepCounterVal, (i - 1) / 60.0, DELTA);
            assertEquals(basicCounterVal, expectedNormalized[i - 1], DELTA);

            for (Metric m : o) {
                assertEquals(m.getTimestamp(), stepBoundary);
            }
        }

        // no updates to anything, just clock forward
        int i = 5;
        manualClock.set(i * 60000L + 20000L);
        List<Metric> metrics = getValue(monitors, manualClock);
        toRateMetricTransform.update(metrics);
        List<Metric> o = observer.getObservations().get(0);
        assertEquals(o.size(), 3);

View Full Code Here

    @Test
    public void testExpiration() {
        BasicCounter c1 = new BasicCounter(MonitorConfig.builder("c1").build());
        BasicCounter c2 = new BasicCounter(MonitorConfig.builder("c2").build());
        BasicCounter c3 = new BasicCounter(MonitorConfig.builder("c3").build());
        ManualClock manualClock = new ManualClock(0);

        MemoryMetricObserver observer = new MemoryMetricObserver("normalization-test", 1);
        NormalizationTransform normalizationTransform = new NormalizationTransform(observer, 60,
                120, TimeUnit.SECONDS, manualClock);
        CounterToRateMetricTransform toRateMetricTransform =
                new CounterToRateMetricTransform(normalizationTransform, 60,
                        120, TimeUnit.SECONDS, manualClock);

        manualClock.set(30000L);
        c1.increment();
        Metric m1 = new Metric(c1.getConfig(), manualClock.now(), c1.getValue(0));

        toRateMetricTransform.update(ImmutableList.of(m1));
        assertEquals(NormalizationTransform.HEARTBEAT_EXCEEDED.getValue(0).longValue(), 0);
        List<Metric> o = observer.getObservations().get(0);
        assertEquals(o.size(), 1);

        manualClock.set(100000L);
        Metric m2 = new Metric(c2.getConfig(), manualClock.now(), c2.getValue());
        toRateMetricTransform.update(ImmutableList.of(m2));
        assertEquals(NormalizationTransform.HEARTBEAT_EXCEEDED.getValue(0).longValue(), 0);

        manualClock.set(160000L);
        Metric m3 = new Metric(c3.getConfig(), manualClock.now(), c3.getValue());
        toRateMetricTransform.update(ImmutableList.of(m3));
        assertEquals(NormalizationTransform.HEARTBEAT_EXCEEDED.getValue(0).longValue(), 1);

    }
View Full Code Here

TOP

Related Classes of com.netflix.servo.util.ManualClock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.