| 1 | package de.uka.ipd.sdq.prototype.framework.tests; |
| 2 | |
| 3 | import java.io.BufferedWriter; |
| 4 | import java.io.FileWriter; |
| 5 | import java.io.IOException; |
| 6 | |
| 7 | import junit.framework.Assert; |
| 8 | |
| 9 | import org.apache.log4j.Logger; |
| 10 | import org.junit.Before; |
| 11 | import org.junit.Test; |
| 12 | |
| 13 | import de.uka.ipd.sdq.measurement.strategies.activeresource.DegreeOfAccuracyEnum; |
| 14 | import de.uka.ipd.sdq.measurement.strategies.activeresource.IDemandStrategy; |
| 15 | import de.uka.ipd.sdq.measurement.strategies.activeresource.ResourceTypeEnum; |
| 16 | import de.uka.ipd.sdq.measurement.strategies.activeresource.cpu.FibonacciDemand; |
| 17 | import de.uka.ipd.sdq.measurement.strategies.activeresource.hdd.ReadLargeChunksDemand; |
| 18 | import de.uka.ipd.sdq.prototype.framework.strategies.DemandConsumerStrategiesRegistry; |
| 19 | |
| 20 | public class PrototypePlatformTests { |
| 21 | |
| 22 | private static final double CPU_PROCESSING_RATE = 1000.0; |
| 23 | private static final double HDD_PROCESSING_RATE = 1000.0; |
| 24 | private static Logger logger = Logger |
| 25 | .getLogger(PrototypePlatformTests.class.getName()); |
| 26 | |
| 27 | @Before |
| 28 | public void initialise() { |
| 29 | /* |
| 30 | * This is done by the Strategy Register itself at the moment, but will |
| 31 | * be needed later. |
| 32 | */ |
| 33 | System.out.println("Pls pin processor! Press a key when ready."); |
| 34 | /*try { |
| 35 | System.in.read(); |
| 36 | } catch (IOException e) { |
| 37 | // TODO Auto-generated catch block |
| 38 | e.printStackTrace(); |
| 39 | }*/ |
| 40 | logger.debug("Initialising Testbed"); |
| 41 | IDemandStrategy cpuStrategy = new FibonacciDemand(); |
| 42 | cpuStrategy.initializeStrategy(DegreeOfAccuracyEnum.HIGH,CPU_PROCESSING_RATE); |
| 43 | DemandConsumerStrategiesRegistry.singleton().registerStrategyFor( |
| 44 | ResourceTypeEnum.CPU, cpuStrategy); |
| 45 | |
| 46 | IDemandStrategy hddStrategy = new ReadLargeChunksDemand(); |
| 47 | hddStrategy.initializeStrategy(DegreeOfAccuracyEnum.MEDIUM,HDD_PROCESSING_RATE); |
| 48 | DemandConsumerStrategiesRegistry.singleton().registerStrategyFor( |
| 49 | ResourceTypeEnum.HDD, hddStrategy); |
| 50 | logger.debug("Testbed inialised"); |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void testConsumeCPU() { |
| 55 | // long unitsToConsume = 5000; |
| 56 | final double ERROR_LEVEL = 0.1; // Total error level for a single |
| 57 | // measurement |
| 58 | |
| 59 | final int TEST_ITERATIONS = 1; |
| 60 | final double OUTLIER_RATIO = 0.1; // How many measurements may be |
| 61 | // outside bounds |
| 62 | final int START_UNIT = 512; // Lower units cause larger relative overhead |
| 63 | |
| 64 | for (long unitsToConsume = START_UNIT; unitsToConsume <= 2048; unitsToConsume = unitsToConsume * 2) { |
| 65 | testConsumeCPUUnits(ERROR_LEVEL, TEST_ITERATIONS, OUTLIER_RATIO, |
| 66 | unitsToConsume); |
| 67 | |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private void testConsumeCPUUnits(final double ERROR_LEVEL, |
| 72 | final int TEST_ITERATIONS, final double OUTLIER_RATIO, |
| 73 | long unitsToConsume) { |
| 74 | |
| 75 | double lowerAcceptanceBound = (unitsToConsume - (unitsToConsume |
| 76 | * ERROR_LEVEL / 2)) |
| 77 | / CPU_PROCESSING_RATE; |
| 78 | double upperAcceptanceBound = (unitsToConsume + (unitsToConsume |
| 79 | * ERROR_LEVEL / 2)) |
| 80 | / CPU_PROCESSING_RATE; |
| 81 | |
| 82 | IDemandStrategy cpuStrategy = DemandConsumerStrategiesRegistry |
| 83 | .singleton().getStrategyFor(ResourceTypeEnum.CPU); |
| 84 | |
| 85 | int countOutliers = 0; |
| 86 | for (int i = 0; i < TEST_ITERATIONS; i++) { |
| 87 | |
| 88 | long start = System.nanoTime(); |
| 89 | cpuStrategy.consume(unitsToConsume); |
| 90 | long end = System.nanoTime(); |
| 91 | |
| 92 | double timeConsumptionInSeconds = (end - start) / 1.0E9; |
| 93 | |
| 94 | if (timeConsumptionInSeconds < lowerAcceptanceBound |
| 95 | || timeConsumptionInSeconds > upperAcceptanceBound) { |
| 96 | countOutliers++; |
| 97 | if (timeConsumptionInSeconds < lowerAcceptanceBound) |
| 98 | logger.info("Lower acceptance level not reached in run " |
| 99 | + i + ": Time is " + timeConsumptionInSeconds |
| 100 | + " and must be higher than " |
| 101 | + lowerAcceptanceBound); |
| 102 | if (timeConsumptionInSeconds > upperAcceptanceBound) |
| 103 | logger. |
| 104 | info("Upper acceptance level not reached in run " |
| 105 | + i + ": Time is " |
| 106 | + timeConsumptionInSeconds |
| 107 | + " and must be lower than " |
| 108 | + upperAcceptanceBound); |
| 109 | } |
| 110 | } |
| 111 | logger.info("There have been " + countOutliers + " outliers out of " |
| 112 | + TEST_ITERATIONS + " values for " + unitsToConsume |
| 113 | + " workunits."); |
| 114 | Assert.assertTrue("There have been more than " + TEST_ITERATIONS |
| 115 | * OUTLIER_RATIO + " outliers for " + unitsToConsume |
| 116 | + " work units: " + countOutliers, |
| 117 | countOutliers <= TEST_ITERATIONS * OUTLIER_RATIO); |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void testConsumeHDD() throws IOException { |
| 122 | |
| 123 | ReadLargeChunksDemand hddStrategy = (ReadLargeChunksDemand) DemandConsumerStrategiesRegistry |
| 124 | .singleton().getStrategyFor(ResourceTypeEnum.HDD); |
| 125 | |
| 126 | Assert |
| 127 | .assertEquals(hddStrategy.getClass(), |
| 128 | ReadLargeChunksDemand.class); |
| 129 | |
| 130 | BufferedWriter bw = new BufferedWriter(new FileWriter( |
| 131 | "testConsumeHDDResults.csv")); |
| 132 | bw.write("SizeRead;Time"); |
| 133 | bw.newLine(); |
| 134 | |
| 135 | hddStrategy.initializeStrategy(DegreeOfAccuracyEnum.MEDIUM,0.0); |
| 136 | |
| 137 | boolean random = true; |
| 138 | |
| 139 | int iterations = 100; |
| 140 | |
| 141 | double demand = 1000000; |
| 142 | // warmup |
| 143 | for (int i = 0; i < 100; i++) { |
| 144 | hddStrategy.consume(demand); |
| 145 | } |
| 146 | |
| 147 | if (!random) { |
| 148 | consumeDecreasingHDDDemand(hddStrategy, bw, iterations); |
| 149 | } else { |
| 150 | consumeRandomHDDDemand(hddStrategy, bw, iterations); |
| 151 | } |
| 152 | |
| 153 | // TODO: Noch mehr Tests. |
| 154 | } |
| 155 | |
| 156 | @Test |
| 157 | |
| 158 | private void consumeRandomHDDDemand(ReadLargeChunksDemand hddStrategy, |
| 159 | BufferedWriter bw, int iterations) throws IOException { |
| 160 | double[] demand = new double[iterations]; |
| 161 | long[] startTime = new long[iterations]; |
| 162 | long[] endTime = new long[iterations]; |
| 163 | for (int i = 0; i < iterations; i++) { |
| 164 | demand[i] = Math.random() * hddStrategy.getMaxFileSize(); |
| 165 | startTime[i] = System.nanoTime(); |
| 166 | hddStrategy.consume(demand[i]); |
| 167 | endTime[i] = System.nanoTime(); |
| 168 | } |
| 169 | for (int i = 0; i < iterations; i++) { |
| 170 | writeHDDResultToFile(bw, startTime[i], endTime[i], demand[i], i); |
| 171 | } |
| 172 | |
| 173 | } |
| 174 | |
| 175 | private void consumeDecreasingHDDDemand(ReadLargeChunksDemand hddStrategy, |
| 176 | BufferedWriter bw, int iterations) throws IOException { |
| 177 | |
| 178 | long sum; |
| 179 | double demand; |
| 180 | |
| 181 | long[] startTimes = new long[iterations]; |
| 182 | long[] endTimes = new long[iterations]; |
| 183 | |
| 184 | for (int j = hddStrategy.getMaxFileSize(); j > 0; j -= 1000000) { |
| 185 | demand = j; |
| 186 | |
| 187 | for (int i = 0; i < iterations; i++) { |
| 188 | startTimes[i] = System.nanoTime(); |
| 189 | hddStrategy.consume(demand); |
| 190 | endTimes[i] = System.nanoTime(); |
| 191 | } |
| 192 | sum = 0; |
| 193 | for (int i = 0; i < iterations; i++) { |
| 194 | |
| 195 | sum += endTimes[i] - startTimes[i]; |
| 196 | writeHDDResultToFile(bw, startTimes[i], endTimes[i], demand, i); |
| 197 | } |
| 198 | double mean = sum / (double) iterations; |
| 199 | System.out.println("Mean is " + mean + " nanoseconds, that is " |
| 200 | + (mean / 1000000000) + " seconds."); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | private void writeHDDResultToFile(BufferedWriter bw, long startTime, |
| 205 | long endTime, double demand, int i) throws IOException { |
| 206 | System.out.println(i + ": Reading " + demand + " B took " |
| 207 | + (endTime - startTime) + " ns."); |
| 208 | bw.write(demand + ";" + (endTime - startTime)); |
| 209 | bw.newLine(); |
| 210 | bw.flush(); |
| 211 | } |
| 212 | |
| 213 | } |