Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c8439b2f12f04e5b578835ae4b2f5f82c5aa3616 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*******************************************************************************
 * Copyright (c) 2014 Ericsson
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Vincent Perot - Initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.pcap.core.tests.perf.trace;

import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import org.eclipse.linuxtools.pcap.core.packet.BadPacketException;
import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace;
import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException;
import org.eclipse.linuxtools.pcap.core.trace.PcapFile;
import org.eclipse.test.performance.Dimension;
import org.eclipse.test.performance.Performance;
import org.eclipse.test.performance.PerformanceMeter;
import org.junit.Test;

/**
 * Tests for performance regressions of the pcap reader. It only tests the pcap
 * reader, not tmf. <br>
 * This test runs in 3 passes.
 * <ul>
 * <li>first it opens a trace</li>
 * <li>then it reads the trace completely</li>
 * <li>then it randomly (seeded) seeks NB_SEEKS locations in the trace and reads
 * one event at each position.</li>
 * </ul>
 * <li>Note: We should make more seeks, since the current number is just too
 * fast.</li>
 *
 * @author Vincent Perot
 */
public class PcapSeekBenchmark {

    private static final Random RND = new Random(1000);

    private static final int LOOP_COUNT = 25;
    private static final int NB_SEEKS = 1000000;
    private static final String TEST_SUITE_NAME = "Pcap Read & Seek Benchmark (" + NB_SEEKS + " seeks)";
    private static final String TEST_ID = "org.eclipse.linuxtools#" + TEST_SUITE_NAME;

    /**
     * Run the benchmark scenario for the pcap trace.
     */
    @Test
    public void testPcapTrace() {
        readAndSeekTrace(PcapTestTrace.BENCHMARK_TRACE, "trace-pcap", true);
    }

    private static void readAndSeekTrace(PcapTestTrace testTrace, String testName, boolean inGlobalSummary) {
        assumeTrue(testTrace.exists());

        Performance perf = Performance.getDefault();
        PerformanceMeter pm = perf.createPerformanceMeter(TEST_ID + '#' + testName);
        perf.tagAsSummary(pm, TEST_SUITE_NAME + ':' + testName, Dimension.CPU_TIME);

        if (inGlobalSummary) {
            perf.tagAsGlobalSummary(pm, TEST_SUITE_NAME + ':' + testName, Dimension.CPU_TIME);
        }

        for (int loop = 0; loop < LOOP_COUNT; loop++) {
            try (PcapFile trace = testTrace.getTrace()) {
                trace.seekPacket(0);

                /* Read the whole trace to find out the number of packets */
                long nbPackets = trace.getTotalNbPackets();

                /* Generate the timestamps we will seek to */
                List<Long> seekTimestamps = new LinkedList<>();
                final long range = nbPackets;
                for (int i = 0; i < NB_SEEKS; i++) {
                    long rank = (RND.nextLong() % range);
                    if (rank < 0) {
                        // This is needed since modulus can return a negative
                        // number.
                        rank += range;
                    }
                    seekTimestamps.add(rank);
                }

                /* Benchmark seeking to the generated timestamps */
                pm.start();
                for (Long rank : seekTimestamps) {
                    trace.seekPacket(rank);
                    trace.parseNextPacket();
                }
                pm.stop();

            } catch (IOException | BadPcapFileException | BadPacketException e) {
                /* Should not happen if assumeTrue() passed above */
                fail("Test failed at iteration " + loop + ':' + e.getMessage());
            }
        }
        pm.commit();
    }
}

Back to the top