Search in sources :

Example 1 with TimingImportance

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

the class TestLatencyInstrumentation method checkTimingKeyCompleteness.

/**
 * Ensures that the recorded timing keys are "complete", meaning that all keys which are expected to be present
 * are present. Also ensures that no unexpected keys are present.
 * @param forceException parameter from the test method
 * @param timingImportanceThreshold parameter from the test method
 */
private void checkTimingKeyCompleteness(boolean forceException, TimingImportance timingImportanceThreshold, boolean useScatterGather) {
    // Form the set of expected timing keys using the current test parameters
    Set<TimingKey> expectedKeys = Arrays.stream(FrameworkTimingKeys.values()).map(FrameworkTimingKeys::key).filter(timingKey -> !(useScatterGather && timingKey.getName().startsWith(FrameworkTimingKeys.KEY_PREFIX + "client"))).filter(timingKey -> {
        if (forceException) {
            return !TIMING_KEYS_MISSING_ON_ERROR.contains(timingKey);
        } else {
            return !TIMING_KEYS_MISSING_ON_SUCCESS.contains(timingKey);
        }
    }).filter(timingKey -> !TIMING_KEYS_MISSING_ON_PROTOCOL_2_0_0.contains(timingKey)).filter(timingKey -> timingImportanceThreshold == null || TIMING_KEYS_ALWAYS_PRESENT.contains(timingKey) || timingKey.getTimingImportance().isAtLeast(timingImportanceThreshold)).collect(Collectors.toSet());
    // Check that all keys have complete timings (not -1) and that there are no unexpected keys
    for (TimingKey timingKey : _resultMap.keySet()) {
        if (expectedKeys.contains(timingKey)) {
            expectedKeys.remove(timingKey);
            Assert.assertNotEquals(_resultMap.get(timingKey).getDurationNano(), -1, timingKey.getName() + " is -1");
        } else if (timingKey.getName().contains(FrameworkTimingKeys.KEY_PREFIX)) {
            Assert.fail("Encountered unexpected key: " + timingKey);
        }
    }
    // Check that the set of recorded timing keys is "complete"
    Assert.assertTrue(expectedKeys.isEmpty(), "Missing keys: " + expectedKeys.stream().map(key -> String.format("\"%s\"", key.getName())).collect(Collectors.joining(", ")));
}
Also used : LatencyInstrumentationResource(com.linkedin.restli.examples.instrumentation.server.LatencyInstrumentationResource) Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) RestLiIntegrationTest(com.linkedin.restli.examples.RestLiIntegrationTest) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) AfterMethod(org.testng.annotations.AfterMethod) RestResponse(com.linkedin.r2.message.rest.RestResponse) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Request(com.linkedin.r2.message.Request) Assert(org.testng.Assert) RequestFinalizer(com.linkedin.r2.util.finalizer.RequestFinalizer) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ResponseFuture(com.linkedin.restli.client.ResponseFuture) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) Map(java.util.Map) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) RequestContextUtil(com.linkedin.r2.util.RequestContextUtil) FilterChain(com.linkedin.r2.filter.FilterChain) FrameworkTimingKeys(com.linkedin.r2.message.timing.FrameworkTimingKeys) IdEntityResponse(com.linkedin.restli.common.IdEntityResponse) RestRequest(com.linkedin.r2.message.rest.RestRequest) SingleRetry(com.linkedin.test.util.retry.SingleRetry) StreamFilter(com.linkedin.r2.filter.message.stream.StreamFilter) FilterChains(com.linkedin.r2.filter.FilterChains) LatencyInstrumentationBuilders(com.linkedin.restli.examples.instrumentation.client.LatencyInstrumentationBuilders) Response(com.linkedin.r2.message.Response) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) Collection(java.util.Collection) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) InstrumentationControl(com.linkedin.restli.examples.instrumentation.api.InstrumentationControl) TimingKey(com.linkedin.r2.message.timing.TimingKey) RequestFinalizerManager(com.linkedin.r2.util.finalizer.RequestFinalizerManager) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) NextFilter(com.linkedin.r2.filter.NextFilter) RequestContext(com.linkedin.r2.message.RequestContext) CreateIdEntityRequest(com.linkedin.restli.client.CreateIdEntityRequest) RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) TimingContextUtil(com.linkedin.r2.message.timing.TimingContextUtil) TimingImportance(com.linkedin.r2.message.timing.TimingImportance) Collections(java.util.Collections) TimingKey(com.linkedin.r2.message.timing.TimingKey)

Example 2 with TimingImportance

use of com.linkedin.r2.message.timing.TimingImportance 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)

Example 3 with TimingImportance

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

the class TestLatencyInstrumentation method provideLatencyInstrumentationData.

@DataProvider(name = "latencyInstrumentation")
private Object[][] provideLatencyInstrumentationData() {
    List<Object[]> data = new ArrayList<>();
    boolean[] booleanOptions = new boolean[] { true, false };
    Collection<TimingImportance> timingImportanceOptions = new ArrayList<>(Arrays.asList(TimingImportance.values()));
    timingImportanceOptions.add(null);
    for (boolean useStreaming : booleanOptions) {
        for (boolean forceException : booleanOptions) {
            for (boolean useScatterGather : booleanOptions) {
                for (TimingImportance timingImportanceThreshold : timingImportanceOptions) {
                    data.add(new Object[] { useStreaming, forceException, useScatterGather, timingImportanceThreshold });
                }
            }
        }
    }
    return data.toArray(new Object[data.size()][4]);
}
Also used : ArrayList(java.util.ArrayList) TimingImportance(com.linkedin.r2.message.timing.TimingImportance) DataProvider(org.testng.annotations.DataProvider)

Example 4 with TimingImportance

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

the class TestLatencyInstrumentation method beforeMethod.

@BeforeMethod
public void beforeMethod(Object[] args) throws Exception {
    _countDownLatch = new CountDownLatch(1);
    _useStreaming = (boolean) args[0];
    _timingImportanceThreshold = (TimingImportance) args[3];
    final InstrumentationTrackingFilter instrumentationTrackingFilter = new InstrumentationTrackingFilter();
    final FilterChain filterChain = FilterChains.empty().addFirst(instrumentationTrackingFilter).addFirstRest(instrumentationTrackingFilter);
    // System.setProperty("test.useStreamCodecServer", "true"); // Uncomment this to test with server streaming codec in IDEA
    super.init(Collections.emptyList(), filterChain, false);
}
Also used : FilterChain(com.linkedin.r2.filter.FilterChain) CountDownLatch(java.util.concurrent.CountDownLatch) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

FilterChain (com.linkedin.r2.filter.FilterChain)2 RequestContext (com.linkedin.r2.message.RequestContext)2 TimingImportance (com.linkedin.r2.message.timing.TimingImportance)2 ArrayList (java.util.ArrayList)2 Test (org.testng.annotations.Test)2 Callback (com.linkedin.common.callback.Callback)1 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)1 FilterChains (com.linkedin.r2.filter.FilterChains)1 NextFilter (com.linkedin.r2.filter.NextFilter)1 RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)1 StreamFilter (com.linkedin.r2.filter.message.stream.StreamFilter)1 Request (com.linkedin.r2.message.Request)1 Response (com.linkedin.r2.message.Response)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 RestResponse (com.linkedin.r2.message.rest.RestResponse)1 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)1 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)1 FrameworkTimingKeys (com.linkedin.r2.message.timing.FrameworkTimingKeys)1 TimingContextUtil (com.linkedin.r2.message.timing.TimingContextUtil)1 TimingKey (com.linkedin.r2.message.timing.TimingKey)1