Search in sources :

Example 1 with Duration

use of com.newrelic.agent.Duration in project newrelic-java-agent by newrelic.

the class KeyTransactionProfileTest method testKeyTransactionsAcrossMultipleThreads.

@Test
public void testKeyTransactionsAcrossMultipleThreads() {
    doReturn(new NoOpSet(Sets.newHashSet(1L, 2L))).when(keyTransactionProfile).getActiveThreadIds();
    // Thread 1: |--- Key Tx (TxA1) ---|---  Non-Key Tx   ---|--- Key Tx (TxA3) ---|
    // Thread 2:                       |--- Key Tx (TxA2) ---|
    long key1StartTime = System.nanoTime();
    StackTraceElement[] keyStackTrace1 = generateStackTraceElements(3, "fileName1");
    keyTransactionProfile.addStackTrace(1, true, ThreadType.BasicThreadType.OTHER, keyStackTrace1);
    long key1EndTime = System.nanoTime();
    long nonKeyStartTime = System.nanoTime();
    StackTraceElement[] nonKeyStackTrace = generateStackTraceElements(3, "fileNameNonKey");
    keyTransactionProfile.addStackTrace(1, true, ThreadType.BasicThreadType.OTHER, nonKeyStackTrace);
    long nonKeyEndTime = System.nanoTime();
    keyTransactionProfile.dispatcherTransactionFinished(generateTransactionData(ImmutableMultimap.of(1L, new Duration(nonKeyStartTime, nonKeyEndTime)), nonKeyStartTime, nonKeyEndTime, "nonKeyTransaction", "App"), new TransactionStats());
    long key2StartTime = System.nanoTime();
    StackTraceElement[] keyStackTrace2 = generateStackTraceElements(3, "fileName2");
    keyTransactionProfile.addStackTrace(2, true, ThreadType.BasicThreadType.OTHER, keyStackTrace2);
    long key2EndTime = System.nanoTime();
    long key3StartTime = System.nanoTime();
    StackTraceElement[] keyStackTrace3 = generateStackTraceElements(3, "fileName3");
    keyTransactionProfile.addStackTrace(1, true, ThreadType.BasicThreadType.OTHER, keyStackTrace3);
    long key3EndTime = System.nanoTime();
    keyTransactionProfile.dispatcherTransactionFinished(generateTransactionData(ImmutableMultimap.of(1L, new Duration(key1StartTime, key1EndTime), 2L, new Duration(key2StartTime, key2EndTime), 1L, new Duration(key3StartTime, key3EndTime)), key1StartTime, key3EndTime, "keyTransaction", "App"), new TransactionStats());
    // This would get called by the sampler thread
    keyTransactionProfile.beforeSampling();
    verify(profile, times(1)).addStackTrace(eq(1L), eq(true), eq(ThreadType.BasicThreadType.OTHER), eq(keyStackTrace1[0]), eq(keyStackTrace1[1]), eq(keyStackTrace1[2]));
    verify(profile, times(1)).addStackTrace(eq(2L), eq(true), eq(ThreadType.BasicThreadType.OTHER), eq(keyStackTrace2[0]), eq(keyStackTrace2[1]), eq(keyStackTrace2[2]));
    verify(profile, times(1)).addStackTrace(eq(1L), eq(true), eq(ThreadType.BasicThreadType.OTHER), eq(keyStackTrace3[0]), eq(keyStackTrace3[1]), eq(keyStackTrace3[2]));
    verify(profile, times(0)).addStackTrace(eq(1L), eq(true), eq(ThreadType.BasicThreadType.OTHER), eq(nonKeyStackTrace[0]), eq(nonKeyStackTrace[1]), eq(nonKeyStackTrace[2]));
    assertEquals(2, keyTransactionProfile.getPendingThreadQueueSizes().size());
    assertTrue(keyTransactionProfile.getPendingThreadQueueSizes().containsKey(1L));
    assertTrue(keyTransactionProfile.getPendingThreadQueueSizes().containsKey(2L));
    assertEquals(0, (long) keyTransactionProfile.getPendingThreadQueueSizes().get(1L));
    assertEquals(0, (long) keyTransactionProfile.getPendingThreadQueueSizes().get(2L));
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) Duration(com.newrelic.agent.Duration) Test(org.junit.Test)

Example 2 with Duration

use of com.newrelic.agent.Duration in project newrelic-java-agent by newrelic.

the class KeyTransactionProfileTest method testKeyTransaction.

@Test
public void testKeyTransaction() {
    doReturn(new NoOpSet(Sets.newHashSet(1L))).when(keyTransactionProfile).getActiveThreadIds();
    // Create non-key transactions (followed by a key transaction on the same thread)
    keyTransactionProfile.addStackTrace(1, true, ThreadType.BasicThreadType.OTHER, generateStackTraceElements(3, "fileName"));
    long keyTransactionStartTime = System.nanoTime();
    StackTraceElement[] keyStackTrace = generateStackTraceElements(3, "fileName");
    keyTransactionProfile.addStackTrace(1, true, ThreadType.BasicThreadType.OTHER, keyStackTrace);
    long keyTransactionEndTime = System.nanoTime();
    // This should match the key transaction and leave the non-key transaction in the queue
    keyTransactionProfile.dispatcherTransactionFinished(generateTransactionData(ImmutableMultimap.of(1L, new Duration(keyTransactionStartTime, keyTransactionEndTime)), keyTransactionStartTime, keyTransactionEndTime, "keyTransaction", "App"), new TransactionStats());
    // This would get called by the sampler thread
    keyTransactionProfile.beforeSampling();
    verify(profile, times(1)).addStackTrace(eq(1L), eq(true), eq(ThreadType.BasicThreadType.OTHER), eq(keyStackTrace[0]), eq(keyStackTrace[1]), eq(keyStackTrace[2]));
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) Duration(com.newrelic.agent.Duration) Test(org.junit.Test)

Example 3 with Duration

use of com.newrelic.agent.Duration in project newrelic-java-agent by newrelic.

the class KeyTransactionProfileTest method testUnusedThreadCleanup.

@Test
public void testUnusedThreadCleanup() {
    doReturn(new NoOpSet(Sets.newHashSet(1L))).when(keyTransactionProfile).getActiveThreadIds();
    // Create key transaction on thread 1 and the rest will be non-key transactions
    long keyTransactionStartTime = System.nanoTime();
    StackTraceElement[] keyStackTrace = generateStackTraceElements(3, "fileName");
    keyTransactionProfile.addStackTrace(1, true, ThreadType.BasicThreadType.OTHER, keyStackTrace);
    long keyTransactionEndTime = System.nanoTime();
    keyTransactionProfile.addStackTrace(2, true, ThreadType.BasicThreadType.OTHER, generateStackTraceElements(3, "fileName2"));
    keyTransactionProfile.addStackTrace(3, true, ThreadType.BasicThreadType.OTHER, generateStackTraceElements(3, "fileName3"));
    keyTransactionProfile.addStackTrace(4, true, ThreadType.BasicThreadType.OTHER, generateStackTraceElements(3, "fileName4"));
    keyTransactionProfile.addStackTrace(5, true, ThreadType.BasicThreadType.OTHER, generateStackTraceElements(3, "fileName5"));
    // This should match the key transaction and leave the non-key transactions in the queue
    keyTransactionProfile.dispatcherTransactionFinished(generateTransactionData(ImmutableMultimap.of(1L, new Duration(keyTransactionStartTime, keyTransactionEndTime)), keyTransactionStartTime, keyTransactionEndTime, "keyTransaction", "App"), new TransactionStats());
    // This would get called by the sampler thread (and should clear out the non-key transaction queues)
    keyTransactionProfile.beforeSampling();
    verify(profile, times(1)).addStackTrace(eq(1L), eq(true), eq(ThreadType.BasicThreadType.OTHER), eq(keyStackTrace[0]), eq(keyStackTrace[1]), eq(keyStackTrace[2]));
    assertEquals(1, keyTransactionProfile.getPendingThreadQueueSizes().size());
    assertTrue(keyTransactionProfile.getPendingThreadQueueSizes().containsKey(1L));
    assertEquals(0, (long) keyTransactionProfile.getPendingThreadQueueSizes().get(1L));
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) Duration(com.newrelic.agent.Duration) Test(org.junit.Test)

Example 4 with Duration

use of com.newrelic.agent.Duration in project newrelic-java-agent by newrelic.

the class ProfileTest method testAsyncStackTracesMultipleThreads.

@Test
public void testAsyncStackTracesMultipleThreads() throws Exception {
    ExecutorService singleThreadExecutor1 = Executors.newSingleThreadExecutor();
    ExecutorService singleThreadExecutor2 = Executors.newSingleThreadExecutor();
    Future<Long> getThreadId1 = singleThreadExecutor1.submit(new Callable<Long>() {

        @Override
        public Long call() throws Exception {
            return Thread.currentThread().getId();
        }
    });
    Future<Long> getThreadId2 = singleThreadExecutor2.submit(new Callable<Long>() {

        @Override
        public Long call() throws Exception {
            return Thread.currentThread().getId();
        }
    });
    final Long threadId1 = getThreadId1.get(10, TimeUnit.SECONDS);
    final Long threadId2 = getThreadId2.get(10, TimeUnit.SECONDS);
    ProfilerParameters parameters = new ProfilerParameters(-1L, 0L, 0L, false, false, false, "keyTransaction", "asyncStackTraces");
    final KeyTransactionProfile profile = new KeyTransactionProfile(new Profile(parameters));
    profile.start();
    StackTraceElement[] keyTransactionTrace1 = new StackTraceElement[] { createSimpleStackTrace("keyTransaction1"), createSimpleStackTrace("keyTransaction1"), createSimpleStackTrace("keyTransaction1") };
    final long startTime1 = System.nanoTime();
    profile.addStackTrace(threadId1, true, ThreadType.BasicThreadType.OTHER, keyTransactionTrace1);
    final long endTime1 = System.nanoTime();
    StackTraceElement[] keyTransactionTrace2 = new StackTraceElement[] { createSimpleStackTrace("keyTransaction2"), createSimpleStackTrace("keyTransaction2"), createSimpleStackTrace("keyTransaction2") };
    final long startTime2 = System.nanoTime();
    profile.addStackTrace(threadId2, true, ThreadType.BasicThreadType.OTHER, keyTransactionTrace2);
    final long endTime2 = System.nanoTime();
    Future<?> transactionFinished = singleThreadExecutor2.submit(new Runnable() {

        @Override
        public void run() {
            Multimap<Long, Duration> threadIdToDuration = ArrayListMultimap.create();
            threadIdToDuration.put(threadId1, new Duration(startTime1, endTime1));
            threadIdToDuration.put(threadId2, new Duration(startTime2, endTime2));
            profile.dispatcherTransactionFinished(generateTransactionData(threadIdToDuration, startTime1, endTime2, "asyncStackTraces"), new TransactionStats());
        }
    });
    transactionFinished.get(10, TimeUnit.SECONDS);
    profile.end();
    ProfileTree requestProfileTree = profile.getProfileTree(ThreadType.BasicThreadType.OTHER);
    Collection<ProfileSegment> rootSegments = requestProfileTree.getRootSegments();
    assertNotNull(rootSegments);
    assertEquals(2, rootSegments.size());
}
Also used : Duration(com.newrelic.agent.Duration) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Multimap(com.google.common.collect.Multimap) TransactionStats(com.newrelic.agent.stats.TransactionStats) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Aggregations

Duration (com.newrelic.agent.Duration)4 TransactionStats (com.newrelic.agent.stats.TransactionStats)4 Test (org.junit.Test)4 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 Multimap (com.google.common.collect.Multimap)1 IOException (java.io.IOException)1 ExecutorService (java.util.concurrent.ExecutorService)1 ParseException (org.json.simple.parser.ParseException)1