Commit | Line | Data |
---|---|---|
a1d21447 VP |
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 | ||
20 | import org.eclipse.linuxtools.pcap.core.packet.BadPacketException; | |
21 | import org.eclipse.linuxtools.pcap.core.packet.Packet; | |
22 | import org.eclipse.linuxtools.pcap.core.tests.shared.PcapTestTrace; | |
23 | import org.eclipse.linuxtools.pcap.core.trace.BadPcapFileException; | |
24 | import org.eclipse.linuxtools.pcap.core.trace.PcapFile; | |
25 | import org.eclipse.test.performance.Dimension; | |
26 | import org.eclipse.test.performance.Performance; | |
27 | import org.eclipse.test.performance.PerformanceMeter; | |
28 | import org.junit.Test; | |
29 | ||
30 | ||
31 | /** | |
32 | * Benchmark of the Pcap parser for reading a trace. Note: We should get a bigger trace. One | |
33 | * that has WAYYYY more events since this current trace is just parsed too fast. | |
34 | * | |
35 | * @author Vincent Perot | |
36 | */ | |
37 | public class PcapReadBenchmark { | |
38 | ||
39 | private static final String TEST_SUITE_NAME = "Pcap Read Benchmark"; | |
40 | private static final String TEST_ID = "org.eclipse.linuxtools#" + TEST_SUITE_NAME; | |
41 | private static final int LOOP_COUNT = 25; | |
42 | ||
43 | /** | |
44 | * Benchmark reading the pcap trace | |
45 | */ | |
46 | @Test | |
47 | public void testPcapTrace() { | |
48 | readTrace(PcapTestTrace.MOSTLY_UDP, "trace-pcap", true); | |
49 | } | |
50 | ||
51 | private static void readTrace(PcapTestTrace testTrace, String testName, boolean inGlobalSummary) { | |
52 | assumeTrue(testTrace.exists()); | |
53 | ||
54 | Performance perf = Performance.getDefault(); | |
55 | PerformanceMeter pm = perf.createPerformanceMeter(TEST_ID + '#' + testName); | |
56 | perf.tagAsSummary(pm, TEST_SUITE_NAME + ':' + testName, Dimension.CPU_TIME); | |
57 | ||
58 | if (inGlobalSummary) { | |
59 | perf.tagAsGlobalSummary(pm, TEST_SUITE_NAME + ':' + testName, Dimension.CPU_TIME); | |
60 | } | |
61 | ||
62 | for (int loop = 0; loop < LOOP_COUNT; loop++) { | |
63 | pm.start(); | |
64 | try (PcapFile trace = testTrace.getTrace();) { | |
65 | trace.seekPacket(0); | |
66 | while (trace.hasNextPacket()) { | |
67 | Packet packet = trace.parseNextPacket(); | |
68 | if (packet == null) { | |
69 | fail("Test failed at iteration " + loop + " packet " + trace.getCurrentRank()); | |
70 | return; | |
71 | } | |
72 | /* Do something with the packet because we are awesome */ | |
73 | packet.getPayload(); | |
74 | } | |
75 | ||
76 | } catch (IOException | BadPcapFileException | BadPacketException e) { | |
77 | fail("Test failed at iteration " + loop + ':' + e.getMessage()); | |
78 | } | |
79 | pm.stop(); | |
80 | } | |
81 | pm.commit(); | |
82 | } | |
83 | } |