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 package org.perfidix.example.stack; 28 29 import java.util.ArrayDeque; 30 import java.util.Random; 31 import java.util.Stack; 32 33 import org.perfidix.Benchmark; 34 import org.perfidix.annotation.BeforeBenchClass; 35 import org.perfidix.annotation.Bench; 36 import org.perfidix.example.Config; 37 import org.perfidix.ouput.TabularSummaryOutput; 38 import org.perfidix.result.BenchmarkResult; 39 40 /** 41 * Benching {@link FastIntStack} against {@link Stack}. Just a simple example of 42 * Perfidix. 43 * 44 * @author Sebastian Graf, University of Konstanz 45 */ 46 public final class StackBenchmark { 47 48 /** 49 * Number of runs. 50 */ 51 private static final int RUNS = 100; 52 53 /** 54 * Size of array to be tested. 55 */ 56 private static final int ARRAYSIZE = 100; 57 58 /** Data to be written and read. */ 59 private transient int[] intData; 60 61 /** FastStack instance. */ 62 private transient FastIntStack fastInt; 63 64 /** Stack instance. */ 65 private transient Stack<Integer> normalInt; 66 67 private transient ArrayDeque<Integer> arrayDeque; 68 69 /** 70 * Generating the data, just once per runtime. 71 */ 72 @BeforeBenchClass 73 public void generateData() { 74 final Random ran = new Random(); 75 intData = new int[ARRAYSIZE]; 76 int counter = 0; 77 while (counter < ARRAYSIZE) { 78 intData[counter] = ran.nextInt(); 79 counter++; 80 } 81 } 82 83 /** 84 * Bench for pushing the data to the {@link FastIntStack}. 85 */ 86 @Bench(runs = RUNS) 87 public void benchFastIntPush() { 88 fastInt = new FastIntStack(); 89 for (final int i : intData) { 90 fastInt.push(i); 91 } 92 } 93 94 /** 95 * Bench for popping the data from the {@link FastIntStack}. 96 */ 97 @Bench(runs = RUNS, beforeEachRun = "benchFastIntPush") 98 public void benchFastIntStackPop() { 99 100 while (fastInt.size() > 0) { 101 fastInt.pop(); 102 103 } 104 } 105 106 /** 107 * Bench for pushing the data to the {@link Stack}. 108 */ 109 @Bench(runs = RUNS) 110 public void benchNormalIntPush() { 111 normalInt = new Stack<Integer>(); 112 for (final int i : intData) { 113 normalInt.push(i); 114 } 115 } 116 117 /** 118 * Bench for popping the data from the {@link Stack}. 119 */ 120 @Bench(runs = RUNS, beforeEachRun = "benchNormalIntPush") 121 public void benchNormalIntPop() { 122 while (normalInt.size() > 0) { 123 normalInt.pop(); 124 } 125 } 126 127 @Bench(runs = RUNS) 128 /** 129 * Bench for pushing the data to the {@link ArrayDeque}. 130 */ 131 public void benchArrayDequePush() { 132 arrayDeque = new ArrayDeque<Integer>(); 133 for (final int i : intData) { 134 arrayDeque.push(i); 135 } 136 } 137 138 /** 139 * Bench for popping the data from the {@link ArrayDeque}. 140 */ 141 @Bench(runs = RUNS, beforeEachRun = "benchArrayDequePush") 142 public void benchArrayDequeStackPop() { 143 144 while (arrayDeque.size() > 0) { 145 arrayDeque.pop(); 146 } 147 } 148 149 /** 150 * Simple setUp of a benchmark. The {@link Benchmark} is initialized with 151 * two Meters (<code>TimeMeter</code> and <code>MemMeter</code>). Afterwards 152 * the benchmark is running with a TabularOutput as a listener registered. 153 * The result of the benchmark is displayed in a complete table at the end. 154 * 155 * @param args 156 * not used here 157 */ 158 public static void main(final String[] args) { 159 160 final Benchmark bench = new Benchmark(new Config()); 161 bench.add(StackBenchmark.class); 162 163 final BenchmarkResult res = bench.run(); 164 new TabularSummaryOutput().visitBenchmark(res); 165 } 166 167 }