36
37 private static double calculateErrorPercentage(double expected, double actual) {
38 double error = expected - actual;
39 error = error < 0 ? -error : error;
40 return error / expected * 100;
41 }
42
43 private static void runner(int max, int depth) {
44 for (int j = 0; j < max; j++) {
45 HeapMonitor.allocate(depth);
46 }
47 }
48
49 public static void main(String[] args) {
50 int[] depths = {10, 100, 500};
51
52 for (int depthIdx = 0; depthIdx < depths.length; depthIdx++) {
53 int depth = depths[depthIdx];
54
55 HeapMonitor.enableSampling();
56 // Do the runner 10 times to ensure the stack is really sampled.
57 runner(10, depth);
58
59 // baseDepth represents the helper method depth: main, runner, HeapMonitor.allocate,
60 // and HeapMonitor.actuallyAllocate.
61 // To get the requested depth, remove this from the count.
62 final int baseDepth = 4;
63 double averageDepth = getAverageStackDepth() - baseDepth;
64 double errorPercentage = calculateErrorPercentage(depth, averageDepth);
65
66 // 3% error should be close enough.
67 if (errorPercentage > 3) {
68 throw new RuntimeException("Stack depth average over 5% for depth " + depth + " : " + averageDepth + " , error: " + errorPercentage);
69 }
70
71 HeapMonitor.disableSampling();
72 }
73
74
75 // Last test is 1024, which is the current maximum.
76 HeapMonitor.enableSampling();
77 final int maximumDepth = 1024;
78 // Do the runner 10 times to ensure the stack is really sampled.
79 runner(10, maximumDepth);
80 // Because of the extra frames, we should be at (maximumDepth + a few frames). Due to the
81 // maximum depth allowed, we hit it and so should still be at an average of 1024.
82 double averageDepth = getAverageStackDepth();
83 double errorPercentage = calculateErrorPercentage(maximumDepth, averageDepth);
84 HeapMonitor.disableSampling();
85
86 // 3% error should be close enough.
87 if (errorPercentage > 3) {
88 throw new RuntimeException("Stack depth average over 5% for depth 1024 : " + averageDepth + " , error: " + errorPercentage);
89 }
90 }
91 }
|
36
37 private static double calculateErrorPercentage(double expected, double actual) {
38 double error = expected - actual;
39 error = error < 0 ? -error : error;
40 return error / expected * 100;
41 }
42
43 private static void runner(int max, int depth) {
44 for (int j = 0; j < max; j++) {
45 HeapMonitor.allocate(depth);
46 }
47 }
48
49 public static void main(String[] args) {
50 int[] depths = {10, 100, 500};
51
52 for (int depthIdx = 0; depthIdx < depths.length; depthIdx++) {
53 int depth = depths[depthIdx];
54
55 HeapMonitor.enableSampling();
56 // Do the runner 3 times to ensure the stack is really sampled.
57 runner(3, depth);
58
59 // baseDepth represents the helper method depth: main, runner, HeapMonitor.allocate,
60 // and HeapMonitor.actuallyAllocate.
61 // To get the requested depth, remove this from the count.
62 final int baseDepth = 4;
63 double averageDepth = getAverageStackDepth() - baseDepth;
64 double errorPercentage = calculateErrorPercentage(depth, averageDepth);
65
66 // 3% error should be close enough.
67 if (errorPercentage > 3) {
68 throw new RuntimeException("Stack depth average over 3% for depth " + depth + " : " + averageDepth + " , error: " + errorPercentage);
69 }
70
71 HeapMonitor.disableSampling();
72 }
73
74
75 // Last test is 1024, which is the current maximum.
76 HeapMonitor.enableSampling();
77 final int maximumDepth = 1024;
78 // Do the runner 3 times to ensure the stack is really sampled.
79 runner(3, maximumDepth);
80 // Because of the extra frames, we should be at (maximumDepth + a few frames). Due to the
81 // maximum depth allowed, we hit it and so should still be at an average of 1024.
82 double averageDepth = getAverageStackDepth();
83 double errorPercentage = calculateErrorPercentage(maximumDepth, averageDepth);
84 HeapMonitor.disableSampling();
85
86 // 3% error should be close enough.
87 if (errorPercentage > 3) {
88 throw new RuntimeException("Stack depth average over 3% for depth 1024 : " + averageDepth + " , error: " + errorPercentage);
89 }
90 }
91 }
|