rcp: Rename plugins to org.eclipse.tracecompass
[deliverable/tracecompass.git] / org.eclipse.tracecompass.pcap.core.tests / perf / org / eclipse / linuxtools / pcap / core / tests / perf / trace / PcapSeekBenchmark.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.pcap.core.tests.perf.trace;
14
15 import static org.junit.Assert.fail;
16 import static org.junit.Assume.assumeTrue;
17
18 import java.io.IOException;
19 import java.util.LinkedList;
20 import java.util.List;
21 import java.util.Random;
22
23 import org.eclipse.linuxtools.internal.pcap.core.packet.BadPacketException;
24 import org.eclipse.linuxtools.internal.pcap.core.trace.BadPcapFileException;
25 import org.eclipse.linuxtools.internal.pcap.core.trace.PcapFile;
26 import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
27 import org.eclipse.test.performance.Dimension;
28 import org.eclipse.test.performance.Performance;
29 import org.eclipse.test.performance.PerformanceMeter;
30 import org.junit.Test;
31
32 /**
33 * Tests for performance regressions of the pcap reader. It only tests the pcap
34 * reader, not tmf. <br>
35 * This test runs in 3 passes.
36 * <ul>
37 * <li>first it opens a trace</li>
38 * <li>then it reads the trace completely</li>
39 * <li>then it randomly (seeded) seeks NB_SEEKS locations in the trace and reads
40 * one event at each position.</li>
41 * </ul>
42 * <li>Note: We should make more seeks, since the current number is just too
43 * fast.</li>
44 *
45 * @author Vincent Perot
46 */
47 public class PcapSeekBenchmark {
48
49 private static final Random RND = new Random(1000);
50
51 private static final int LOOP_COUNT = 25;
52 private static final int NB_SEEKS = 1000000;
53 private static final String TEST_SUITE_NAME = "Pcap Read & Seek Benchmark (" + NB_SEEKS + " seeks)";
54 private static final String TEST_ID = "org.eclipse.linuxtools#" + TEST_SUITE_NAME;
55
56 /**
57 * Run the benchmark scenario for the pcap trace.
58 */
59 @Test
60 public void testPcapTrace() {
61 readAndSeekTrace(PcapTestTrace.BENCHMARK_TRACE, "trace-pcap", true);
62 }
63
64 private static void readAndSeekTrace(PcapTestTrace testTrace, String testName, boolean inGlobalSummary) {
65 assumeTrue(testTrace.exists());
66
67 Performance perf = Performance.getDefault();
68 PerformanceMeter pm = perf.createPerformanceMeter(TEST_ID + '#' + testName);
69 perf.tagAsSummary(pm, TEST_SUITE_NAME + ':' + testName, Dimension.CPU_TIME);
70
71 if (inGlobalSummary) {
72 perf.tagAsGlobalSummary(pm, TEST_SUITE_NAME + ':' + testName, Dimension.CPU_TIME);
73 }
74
75 for (int loop = 0; loop < LOOP_COUNT; loop++) {
76 try (PcapFile trace = testTrace.getTrace()) {
77 trace.seekPacket(0);
78
79 /* Read the whole trace to find out the number of packets */
80 long nbPackets = trace.getTotalNbPackets();
81
82 /* Generate the timestamps we will seek to */
83 List<Long> seekTimestamps = new LinkedList<>();
84 final long range = nbPackets;
85 for (int i = 0; i < NB_SEEKS; i++) {
86 long rank = (RND.nextLong() % range);
87 if (rank < 0) {
88 // This is needed since modulus can return a negative
89 // number.
90 rank += range;
91 }
92 seekTimestamps.add(rank);
93 }
94
95 /* Benchmark seeking to the generated timestamps */
96 pm.start();
97 for (Long rank : seekTimestamps) {
98 trace.seekPacket(rank);
99 trace.parseNextPacket();
100 }
101 pm.stop();
102
103 } catch (IOException | BadPcapFileException | BadPacketException e) {
104 /* Should not happen if assumeTrue() passed above */
105 fail("Test failed at iteration " + loop + ':' + e.getMessage());
106 }
107 }
108 pm.commit();
109 }
110 }
This page took 0.032426 seconds and 5 git commands to generate.