Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ListBenchmark |
|
| 1.75;1.75 |
1 | /** | |
2 | * Copyright (c) 2012, University of Konstanz, Distributed Systems Group | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * * Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * * Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * * Neither the name of the University of Konstanz nor the | |
13 | * names of its contributors may be used to endorse or promote products | |
14 | * derived from this software without specific prior written permission. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
19 | * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY | |
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 | */ | |
27 | /** | |
28 | * | |
29 | */ | |
30 | package org.perfidix.example.list; | |
31 | ||
32 | import java.util.ArrayList; | |
33 | import java.util.Random; | |
34 | import java.util.Vector; | |
35 | ||
36 | import org.perfidix.Benchmark; | |
37 | import org.perfidix.annotation.BeforeBenchClass; | |
38 | import org.perfidix.annotation.Bench; | |
39 | import org.perfidix.example.Config; | |
40 | import org.perfidix.ouput.TabularSummaryOutput; | |
41 | import org.perfidix.result.BenchmarkResult; | |
42 | ||
43 | /** | |
44 | * This benchmark benches | |
45 | * [@link IntArrayList], | |
46 | * [@link java.util.ArrayList] and | |
47 | * [@link java.util.Vector] | |
48 | * | |
49 | * @author Nuray Guerler, University of Konstanz | |
50 | * | |
51 | */ | |
52 | 0 | public class ListBenchmark extends ElementList { |
53 | ||
54 | /** | |
55 | * Number of runs. | |
56 | */ | |
57 | private static final int RUNS = 100; | |
58 | ||
59 | /** | |
60 | * Size of array to be tested. | |
61 | */ | |
62 | private static final int ARRAYSIZE = 100; | |
63 | ||
64 | /** Data to be written and read. */ | |
65 | private transient int[] intData; | |
66 | ||
67 | /** IntArrayList instance. */ | |
68 | private transient IntArrayList list; | |
69 | ||
70 | /** ArrayList instance */ | |
71 | private transient ArrayList<Integer> arrayList; | |
72 | ||
73 | /** vector instance */ | |
74 | private transient Vector<Integer> vector; | |
75 | ||
76 | /** | |
77 | * Generating the data, just once per runtime. | |
78 | */ | |
79 | @BeforeBenchClass | |
80 | public void generateData() { | |
81 | 0 | final Random ran = new Random(); |
82 | 0 | intData = new int[ARRAYSIZE]; |
83 | 0 | int counter = 0; |
84 | 0 | while (counter < ARRAYSIZE) { |
85 | 0 | intData[counter] = ran.nextInt(); |
86 | 0 | counter++; |
87 | } | |
88 | 0 | } |
89 | ||
90 | /** | |
91 | * Bench for adding the data to the {@link IntArrayList}. | |
92 | */ | |
93 | @Bench(runs = RUNS) | |
94 | public void intArrayListAdd() { | |
95 | 0 | list = new IntArrayList(); |
96 | 0 | list.add(intData); |
97 | 0 | } |
98 | ||
99 | /** | |
100 | * bench for retrieving an element at a specified index | |
101 | */ | |
102 | @Bench(runs = RUNS, beforeEachRun = "intArrayListAdd") | |
103 | public void intArrayListGet() { | |
104 | 0 | for (int i = 0; i < list.size(); i++) { |
105 | 0 | list.get(i); |
106 | } | |
107 | 0 | } |
108 | ||
109 | /** bench for adding data to the [@link ArrayList] */ | |
110 | @Bench(runs = RUNS) | |
111 | public void arrayListAdd() { | |
112 | 0 | arrayList = new ArrayList<Integer>(); |
113 | 0 | for (final int i : intData) { |
114 | 0 | arrayList.add(i); |
115 | } | |
116 | 0 | } |
117 | ||
118 | /** benchmark for retrieving an element at a specified index */ | |
119 | @Bench(runs = RUNS, beforeEachRun = "arrayListAdd") | |
120 | public void arrayListGet() { | |
121 | 0 | for (int i = 0; i < list.size(); i++) { |
122 | 0 | arrayList.get(i); |
123 | } | |
124 | 0 | } |
125 | ||
126 | /** benchmark for adding data to [@link java.util.Vector] */ | |
127 | @Bench(runs = RUNS) | |
128 | public void vectorAdd() { | |
129 | 0 | vector = new Vector<Integer>(); |
130 | 0 | for (final int i : intData) { |
131 | 0 | vector.add(i); |
132 | } | |
133 | 0 | } |
134 | ||
135 | /** benchmark for retrieving an element at a specified index */ | |
136 | @Bench(runs = RUNS, beforeEachRun = "vectorAdd") | |
137 | public void vectorGet() { | |
138 | 0 | for (int i = 0; i < vector.size(); i++) { |
139 | 0 | vector.get(i); |
140 | } | |
141 | 0 | } |
142 | ||
143 | /** | |
144 | * Simple setUp of a benchmark. The {@link Benchmark} is initialized with | |
145 | * two Meters (<code>TimeMeter</code> and <code>MemMeter</code>). Afterwards | |
146 | * the benchmark is running with a TabularOutput as a listener registered. | |
147 | * The result of the benchmark is displayed in a complete table at the end. | |
148 | * | |
149 | * @param args | |
150 | * not used here | |
151 | */ | |
152 | public static void main(String[] args) { | |
153 | 0 | final Benchmark bench = new Benchmark(new Config()); |
154 | 0 | bench.add(ListBenchmark.class); |
155 | ||
156 | 0 | final BenchmarkResult res = bench.run(); |
157 | 0 | new TabularSummaryOutput().visitBenchmark(res); |
158 | 0 | } |
159 | ||
160 | } |