23 |
23 |
24 package MyPackage; |
24 package MyPackage; |
25 |
25 |
26 /** |
26 /** |
27 * @test |
27 * @test |
28 * @summary Verifies the JVMTI Heap Monitor sampling rate average. |
28 * @summary Verifies the JVMTI Heap Monitor sampling interval average. |
29 * @build Frame HeapMonitor |
29 * @build Frame HeapMonitor |
30 * @compile HeapMonitorStatRateTest.java |
30 * @compile HeapMonitorStatIntervalTest.java |
31 * @requires vm.compMode != "Xcomp" |
31 * @requires vm.compMode != "Xcomp" |
32 * @run main/othervm/native -agentlib:HeapMonitorTest MyPackage.HeapMonitorStatRateTest |
32 * @run main/othervm/native -agentlib:HeapMonitorTest MyPackage.HeapMonitorStatIntervalTest |
33 */ |
33 */ |
34 |
34 |
35 public class HeapMonitorStatRateTest { |
35 public class HeapMonitorStatIntervalTest { |
36 |
36 |
37 private native static double getAverageRate(); |
37 private native static double getAverageInterval(); |
38 |
38 |
39 private static boolean testRateOnce(int rate, boolean throwIfFailure) { |
39 private static boolean testIntervalOnce(int interval, boolean throwIfFailure) { |
40 HeapMonitor.resetEventStorage(); |
40 HeapMonitor.resetEventStorage(); |
41 HeapMonitor.setSamplingRate(rate); |
41 HeapMonitor.setSamplingInterval(interval); |
42 |
42 |
43 HeapMonitor.enableSamplingEvents(); |
43 HeapMonitor.enableSamplingEvents(); |
44 |
44 |
45 int allocationTotal = 10 * 1024 * 1024; |
45 int allocationTotal = 10 * 1024 * 1024; |
46 int allocationIterations = 10; |
46 int allocationIterations = 10; |
52 actualCount += HeapMonitor.getEventStorageElementCount(); |
52 actualCount += HeapMonitor.getEventStorageElementCount(); |
53 } |
53 } |
54 |
54 |
55 HeapMonitor.disableSamplingEvents(); |
55 HeapMonitor.disableSamplingEvents(); |
56 |
56 |
57 double expectedCount = allocationTotal * allocationIterations / rate; |
57 double expectedCount = allocationTotal * allocationIterations / interval; |
58 |
58 |
59 double error = Math.abs(actualCount - expectedCount); |
59 double error = Math.abs(actualCount - expectedCount); |
60 double errorPercentage = error / expectedCount * 100; |
60 double errorPercentage = error / expectedCount * 100; |
61 |
61 |
62 boolean success = (errorPercentage < 10.0); |
62 boolean success = (errorPercentage < 10.0); |
63 |
63 |
64 if (!success && throwIfFailure) { |
64 if (!success && throwIfFailure) { |
65 throw new RuntimeException("Rate average over 10% for rate " + rate + " -> " + actualCount |
65 throw new RuntimeException("Interval average over 10% for interval " + interval + " -> " |
66 + ", " + expectedCount); |
66 + actualCount + ", " + expectedCount); |
67 } |
67 } |
68 |
68 |
69 return success; |
69 return success; |
70 } |
70 } |
71 |
71 |
72 |
72 |
73 private static void testRate(int rate) { |
73 private static void testInterval(int interval) { |
74 // Test the rate twice, it can happen that the test is "unlucky" and the rate just goes above |
74 // Test the interval twice, it can happen that the test is "unlucky" and the interval just goes above |
75 // the 10% mark. So try again to squash flakiness. |
75 // the 10% mark. So try again to squash flakiness. |
76 // Flakiness is due to the fact that this test is dependent on the sampling rate, which is a |
76 // Flakiness is due to the fact that this test is dependent on the sampling interval, which is a |
77 // statistical geometric variable around the sampling rate. This means that the test could be |
77 // statistical geometric variable around the sampling interval. This means that the test could be |
78 // unlucky and not achieve the mean average fast enough for the test case. |
78 // unlucky and not achieve the mean average fast enough for the test case. |
79 if (!testRateOnce(rate, false)) { |
79 if (!testIntervalOnce(interval, false)) { |
80 testRateOnce(rate, true); |
80 testIntervalOnce(interval, true); |
81 } |
81 } |
82 } |
82 } |
83 |
83 |
84 public static void main(String[] args) { |
84 public static void main(String[] args) { |
85 int[] tab = {1024, 8192}; |
85 int[] tab = {1024, 8192}; |
86 |
86 |
87 for (int rateIdx = 0; rateIdx < tab.length; rateIdx++) { |
87 for (int intervalIdx = 0; intervalIdx < tab.length; intervalIdx++) { |
88 testRate(tab[rateIdx]); |
88 testInterval(tab[intervalIdx]); |
89 } |
89 } |
90 } |
90 } |
91 } |
91 } |