Search in sources :

Example 1 with TimingCallback

use of com.linkedin.r2.message.timing.TimingCallback in project rest.li by linkedin.

the class TestTimingCallback method testOrdering.

/**
 * Ensures that timing keys are marked in the same order that they are added.
 */
@Test
public void testOrdering() {
    final RequestContext requestContext = new RequestContext();
    final Callback<Long> callback = new Callback<Long>() {

        @Override
        public void onSuccess(Long result) {
            Map<TimingKey, TimingContextUtil.TimingContext> timings = TimingContextUtil.getTimingsMap(requestContext);
            // Ensure all keys are present
            Assert.assertTrue(timings.containsKey(KEY_H));
            Assert.assertTrue(timings.containsKey(KEY_M));
            Assert.assertTrue(timings.containsKey(KEY_L));
            // Ensure timing start times/durations are consistent based on their ordering in the callback
            TimingContextUtil.TimingContext contextH = timings.get(KEY_H);
            TimingContextUtil.TimingContext contextM = timings.get(KEY_M);
            TimingContextUtil.TimingContext contextL = timings.get(KEY_L);
            Assert.assertTrue(contextM.getStartTimeNano() < contextL.getStartTimeNano());
            Assert.assertTrue(contextL.getStartTimeNano() < contextH.getStartTimeNano());
            Assert.assertTrue(contextL.getDurationNano() < contextM.getDurationNano());
            Assert.assertTrue(contextH.getDurationNano() < contextM.getDurationNano());
        }

        @Override
        public void onError(Throwable e) {
        }
    };
    final Callback<Long> timingCallback = new TimingCallback.Builder<>(callback, requestContext).addBeginTimingKey(KEY_M).addBeginTimingKey(KEY_L).addEndTimingKey(KEY_L).addBeginTimingKey(KEY_H).addEndTimingKey(KEY_H).addEndTimingKey(KEY_M).build();
    timingCallback.onSuccess(1L);
}
Also used : Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 2 with TimingCallback

use of com.linkedin.r2.message.timing.TimingCallback in project rest.li by linkedin.

the class TestTimingCallback method testBuilder.

/**
 * Ensures that the builder can correctly determine how to filter out timing keys based on the current timing
 * importance threshold, and that it can correctly determine when to return the original callback rather than wrapping
 * it with a new one.
 * @param timingImportanceThreshold timing importance threshold
 */
@Test(dataProvider = "timingImportanceThreshold")
public void testBuilder(TimingImportance timingImportanceThreshold) {
    final RequestContext requestContext = new RequestContext();
    if (timingImportanceThreshold != null) {
        requestContext.putLocalAttr(TimingContextUtil.TIMING_IMPORTANCE_THRESHOLD_KEY_NAME, timingImportanceThreshold);
    }
    final Callback<Long> callback = new Callback<Long>() {

        @Override
        public void onSuccess(Long result) {
            Map<TimingKey, TimingContextUtil.TimingContext> timings = TimingContextUtil.getTimingsMap(requestContext);
            // Ensure that keys have been filtered out correctly
            if (timingImportanceThreshold == null || TimingImportance.LOW.isAtLeast(timingImportanceThreshold)) {
                Assert.assertTrue(timings.containsKey(KEY_L));
                Assert.assertTrue(timings.containsKey(KEY_M));
            } else if (TimingImportance.MEDIUM.isAtLeast(timingImportanceThreshold)) {
                Assert.assertFalse(timings.containsKey(KEY_L));
                Assert.assertTrue(timings.containsKey(KEY_M));
            } else {
                Assert.assertFalse(timings.containsKey(KEY_L));
                Assert.assertFalse(timings.containsKey(KEY_M));
            }
        }

        @Override
        public void onError(Throwable e) {
        }
    };
    final Callback<Long> timingCallback = new TimingCallback.Builder<>(callback, requestContext).addBeginTimingKey(KEY_L).addBeginTimingKey(KEY_M).addEndTimingKey(KEY_L).addEndTimingKey(KEY_M).build();
    // Ensure that the builder can correctly determine when to return the original callback
    if (timingImportanceThreshold == null || !timingImportanceThreshold.equals(TimingImportance.HIGH)) {
        Assert.assertTrue(timingCallback instanceof TimingCallback);
    } else {
        Assert.assertFalse(timingCallback instanceof TimingCallback);
    }
    timingCallback.onSuccess(1L);
}
Also used : Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Aggregations

Callback (com.linkedin.common.callback.Callback)2 RequestContext (com.linkedin.r2.message.RequestContext)2 Test (org.testng.annotations.Test)2