| 1 | package de.uka.ipd.sdq.measurement.strategies.activeresource.cpu; |
| 2 | |
| 3 | import java.util.Arrays; |
| 4 | import java.util.Random; |
| 5 | |
| 6 | import de.uka.ipd.sdq.measurement.strategies.activeresource.AbstractDemandStrategy; |
| 7 | import de.uka.ipd.sdq.measurement.strategies.activeresource.CalibrationTable; |
| 8 | import de.uka.ipd.sdq.measurement.strategies.activeresource.ResourceTypeEnum; |
| 9 | |
| 10 | public class SortArrayDemand extends AbstractDemandStrategy { |
| 11 | |
| 12 | private double[] values = null; |
| 13 | |
| 14 | private int ARRAY_SIZE = 1024 * 1024; // Default: 1M Values |
| 15 | |
| 16 | /** |
| 17 | * always use the same seed for the random values to reduce the variability |
| 18 | * of the sorting algorithms (the same seed generates the same sequence of |
| 19 | * values) |
| 20 | */ |
| 21 | private static final int SEED = 1234; |
| 22 | |
| 23 | public SortArrayDemand(int arraySize) { |
| 24 | super(-3,0,3,10000,50); |
| 25 | ARRAY_SIZE = arraySize; |
| 26 | values = new double[ARRAY_SIZE]; |
| 27 | Random r = new Random(SEED); |
| 28 | for (int i = 0; i < values.length; i++) { |
| 29 | values[i] = r.nextDouble(); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | public SortArrayDemand() { |
| 34 | super(-3,0,3,10000,50); |
| 35 | values = new double[ARRAY_SIZE]; |
| 36 | Random r = new Random(SEED); |
| 37 | for (int i = 0; i < values.length; i++) { |
| 38 | values[i] = r.nextDouble(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | private void sortArray(int amountOfNumbers) { |
| 43 | int iterations = amountOfNumbers / ARRAY_SIZE; |
| 44 | int rest = amountOfNumbers % ARRAY_SIZE; |
| 45 | for (int i=0; i<iterations; i++){ |
| 46 | double[] lotsOfDoubles = getArray(ARRAY_SIZE); |
| 47 | Arrays.sort(lotsOfDoubles); |
| 48 | } |
| 49 | double[] lotsOfDoubles = getArray(rest); |
| 50 | Arrays.sort(lotsOfDoubles); |
| 51 | } |
| 52 | |
| 53 | private double[] getArray(int amountOfNumbers) { |
| 54 | return Arrays.copyOf(values, amountOfNumbers); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void run(long initial) { |
| 59 | sortArray((int) initial); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public ResourceTypeEnum getStrategysResource() { |
| 64 | return ResourceTypeEnum.CPU; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public String getName() { |
| 69 | return "SortArray"; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected String getCalibrationFileName() { |
| 74 | return getCalibrationPath() + getName() + "_" |
| 75 | + CalibrationTable.DEFAULT_CALIBRATION_TABLE_SIZE + "_" + ARRAY_SIZE + "_" + this.degreeOfAccuracy.name() + ".ser"; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void cleanup() { |
| 80 | // Do nothing. |
| 81 | } |
| 82 | } |