34 public class HeapMonitorStatArrayCorrectnessTest {
35
36 // Do 100000 iterations and expect maxIteration / multiplier samples.
37 private static final int maxIteration = 100000;
38 private static int array[];
39
40 private static void allocate(int size) {
41 for (int j = 0; j < maxIteration; j++) {
42 array = new int[size];
43 }
44 }
45
46 public static void main(String[] args) {
47 int sizes[] = {1000, 10000, 100000};
48
49 HeapMonitor.enableSamplingEvents();
50
51 for (int currentSize : sizes) {
52 System.out.println("Testing size " + currentSize);
53
54 if (!HeapMonitor.eventStorageIsEmpty()) {
55 throw new RuntimeException("Should not have any events stored yet.");
56 }
57
58 // 111 is as good a number as any.
59 final int samplingMultiplier = 111;
60 HeapMonitor.setSamplingRate(samplingMultiplier * currentSize);
61
62 allocate(currentSize);
63
64 // For simplifications, we ignore the array memory usage for array internals (with the array
65 // sizes requested, it should be a negligible oversight).
66 //
67 // That means that with maxIterations, the loop in the method allocate requests:
68 // maxIterations * currentSize * 4 bytes (4 for integers)
69 //
70 // Via the enable sampling, the code requests a sample every samplingMultiplier * currentSize bytes.
71 //
72 // Therefore, the expected sample number is:
73 // (maxIterations * currentSize * 4) / (samplingMultiplier * currentSize);
74 double expected = maxIteration;
75 expected *= 4;
76 expected /= samplingMultiplier;
77
78 // 10% error ensures a sanity test without becoming flaky.
79 if (!HeapMonitor.statsHaveExpectedNumberSamples((int) expected, 10)) {
80 throw new RuntimeException("Statistics should show about " + expected + " samples.");
81 }
82
83 HeapMonitor.resetEventStorage();
84 }
85 }
86 }
|
34 public class HeapMonitorStatArrayCorrectnessTest {
35
36 // Do 100000 iterations and expect maxIteration / multiplier samples.
37 private static final int maxIteration = 100000;
38 private static int array[];
39
40 private static void allocate(int size) {
41 for (int j = 0; j < maxIteration; j++) {
42 array = new int[size];
43 }
44 }
45
46 public static void main(String[] args) {
47 int sizes[] = {1000, 10000, 100000};
48
49 HeapMonitor.enableSamplingEvents();
50
51 for (int currentSize : sizes) {
52 System.out.println("Testing size " + currentSize);
53
54 HeapMonitor.resetEventStorage();
55 if (!HeapMonitor.eventStorageIsEmpty()) {
56 throw new RuntimeException("Should not have any events stored yet.");
57 }
58
59 // 111 is as good a number as any.
60 final int samplingMultiplier = 111;
61 HeapMonitor.setSamplingRate(samplingMultiplier * currentSize);
62
63 allocate(currentSize);
64
65 // For simplifications, we ignore the array memory usage for array internals (with the array
66 // sizes requested, it should be a negligible oversight).
67 //
68 // That means that with maxIterations, the loop in the method allocate requests:
69 // maxIterations * currentSize * 4 bytes (4 for integers)
70 //
71 // Via the enable sampling, the code requests a sample every samplingMultiplier * currentSize bytes.
72 //
73 // Therefore, the expected sample number is:
74 // (maxIterations * currentSize * 4) / (samplingMultiplier * currentSize);
75 double expected = maxIteration;
76 expected *= 4;
77 expected /= samplingMultiplier;
78
79 // 10% error ensures a sanity test without becoming flaky.
80 if (!HeapMonitor.statsHaveExpectedNumberSamples((int) expected, 10)) {
81 throw new RuntimeException("Statistics should show about " + expected + " samples.");
82 }
83 }
84 }
85 }
|