1 | package de.uka.ipd.sdq.statistics; |
2 | |
3 | import java.util.LinkedList; |
4 | import java.util.List; |
5 | |
6 | import org.apache.log4j.Logger; |
7 | |
8 | /** |
9 | * Batch means algorithm with manually configured batch size. |
10 | * Specify the size of each batch and the minimum number of batches. |
11 | * It will then provide the mean values for the defined batches to the simulation. |
12 | * @author martens |
13 | * |
14 | */ |
15 | public class StaticBatchAlgorithm extends ABatchAlgorithm { |
16 | |
17 | private int batchSize; |
18 | private int minNumberOfBatches; |
19 | |
20 | private List<Double> buffer; |
21 | |
22 | private Logger logger = Logger.getLogger("de.uka.ipd.sdq.statistics.StaticBatchAlgorithm.log"); |
23 | |
24 | |
25 | public StaticBatchAlgorithm(int batchSize, int minNumberOfBatches){ |
26 | this.batchSize = batchSize; |
27 | this.minNumberOfBatches = minNumberOfBatches; |
28 | if (minNumberOfBatches > 0){ |
29 | this.setValid(false); |
30 | } else { |
31 | this.setValid(true); |
32 | } |
33 | |
34 | if (batchSize > 100000){ |
35 | logger.warn("Batch size is larger than 100 000, thus more than 700KB are needed to store intermediate values. Decrease batch size of you have memory problems, or ask the developers to implement a sliding mean calculation for the batchs."); |
36 | } |
37 | this.buffer = new LinkedList<Double>(); |
38 | } |
39 | |
40 | /** |
41 | * Store all values of the current batch to avoid numerical errors with |
42 | * sliding mean calculation. |
43 | * Should not be too many values in one batch. |
44 | */ |
45 | @Override |
46 | public synchronized void offerSample(double value) { |
47 | |
48 | buffer.add(value); |
49 | |
50 | if (buffer.size() > batchSize){ |
51 | // calculate a new batch mean |
52 | int batchSize = buffer.size(); |
53 | double batchSum = 0; |
54 | for (Double sample : buffer) { |
55 | batchSum += sample; |
56 | } |
57 | |
58 | this.batches.add(new Batch(batchSum, batchSize)); |
59 | |
60 | if (!this.hasValidBatches()){ |
61 | if (this.minNumberOfBatches <= this.batches.size()){ |
62 | this.setValid(true); |
63 | } |
64 | } |
65 | |
66 | // empty buffer |
67 | buffer.clear(); |
68 | } |
69 | |
70 | } |
71 | |
72 | } |