1 /*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "gc/g1/g1Predictions.hpp"
27
28 #ifndef PRODUCT
29
30 void G1Predictions::test() {
31 double const epsilon = 1e-6;
32 {
33 // Some basic formula tests with confidence = 0.0
34 G1Predictions predictor(0.0);
35 TruncatedSeq s;
36
37 double p0 = predictor.get_new_prediction(&s);
38 assert(p0 < epsilon, "Initial prediction of empty sequence must be 0.0 but is %f", p0);
39
40 s.add(5.0);
41 double p1 = predictor.get_new_prediction(&s);
42 assert(fabs(p1 - 5.0) < epsilon, "Prediction should be 5.0 but is %f", p1);
43 for (int i = 0; i < 40; i++) {
44 s.add(5.0);
45 }
46 double p2 = predictor.get_new_prediction(&s);
47 assert(fabs(p2 - 5.0) < epsilon, "Prediction should be 5.0 but is %f", p1);
48 }
49
50 {
51 // The following tests checks that the initial predictions are based on the
52 // average of the sequence and not on the stddev (which is 0).
53 G1Predictions predictor(0.5);
54 TruncatedSeq s;
55
56 s.add(1.0);
57 double p1 = predictor.get_new_prediction(&s);
58 assert(p1 > 1.0, "First prediction must be larger than average, but avg is %f and prediction %f", s.davg(), p1);
59 s.add(1.0);
60 double p2 = predictor.get_new_prediction(&s);
61 assert(p2 < p1, "First prediction must be larger than second, but they are %f %f", p1, p2);
62 s.add(1.0);
63 double p3 = predictor.get_new_prediction(&s);
64 assert(p3 < p2, "Second prediction must be larger than third, but they are %f %f", p2, p3);
65 s.add(1.0);
66 s.add(1.0); // Five elements are now in the sequence.
67 double p5 = predictor.get_new_prediction(&s);
68 assert(p5 < p3, "Fifth prediction must be smaller than third, but they are %f %f", p3, p5);
69 assert(fabs(p5 - 1.0) < epsilon, "Prediction must be 1.0+epsilon, but is %f", p5);
70 }
71
72 {
73 // The following tests checks that initially prediction based on the average is
74 // used, that gets overridden by the stddev prediction at the end.
75 G1Predictions predictor(0.5);
76 TruncatedSeq s;
77
78 s.add(0.5);
79 double p1 = predictor.get_new_prediction(&s);
80 assert(p1 > 0.5, "First prediction must be larger than average, but avg is %f and prediction %f", s.davg(), p1);
81 s.add(0.2);
82 double p2 = predictor.get_new_prediction(&s);
83 assert(p2 < p1, "First prediction must be larger than second, but they are %f %f", p1, p2);
84 s.add(0.5);
85 double p3 = predictor.get_new_prediction(&s);
86 assert(p3 < p2, "Second prediction must be larger than third, but they are %f %f", p2, p3);
87 s.add(0.2);
88 s.add(2.0);
89 double p5 = predictor.get_new_prediction(&s);
90 assert(p5 > p3, "Fifth prediction must be bigger than third, but they are %f %f", p3, p5);
91 }
92 }
93
94 void TestPredictions_test() {
95 G1Predictions::test();
96 }
97
98 #endif
|
1 /*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 #include "precompiled.hpp"
25 #include "gc/g1/g1Predictions.hpp"
26 #include "unittest.hpp"
27
28 static const double epsilon = 1e-6;
29
30 // Some basic formula tests with confidence = 0.0
31 TEST_VM(G1Predictions, basic_predictions) {
32 G1Predictions predictor(0.0);
33 TruncatedSeq s;
34
35 double p0 = predictor.get_new_prediction(&s);
36 ASSERT_LT(p0, epsilon) << "Initial prediction of empty sequence must be 0.0";
37
38 s.add(5.0);
39 double p1 = predictor.get_new_prediction(&s);
40 ASSERT_NEAR(p1, 5.0, epsilon);
41
42 for (int i = 0; i < 40; i++) {
43 s.add(5.0);
44 }
45 double p2 = predictor.get_new_prediction(&s);
46 ASSERT_NEAR(p2, 5.0, epsilon);
47 }
48
49 // The following tests checks that the initial predictions are based on
50 // the average of the sequence and not on the stddev (which is 0).
51 TEST_VM(G1Predictions, average_not_stdev_predictions) {
52 G1Predictions predictor(0.5);
53 TruncatedSeq s;
54
55 s.add(1.0);
56 double p1 = predictor.get_new_prediction(&s);
57 ASSERT_GT(p1, s.davg()) << "First prediction must be larger than average";
58
59 s.add(1.0);
60 double p2 = predictor.get_new_prediction(&s);
61 ASSERT_GT(p1, p2) << "First prediction must be larger than second";
62
63 s.add(1.0);
64 double p3 = predictor.get_new_prediction(&s);
65 ASSERT_GT(p2, p3) << "Second prediction must be larger than third";
66
67 s.add(1.0);
68 s.add(1.0); // Five elements are now in the sequence.
69 double p4 = predictor.get_new_prediction(&s);
70 ASSERT_LT(p4, p3) << "Fourth prediction must be smaller than third";
71 ASSERT_NEAR(p4, 1.0, epsilon);
72 }
73
74 // The following tests checks that initially prediction based on
75 // the average is used, that gets overridden by the stddev prediction at
76 // the end.
77 TEST_VM(G1Predictions, average_stdev_predictions) {
78 G1Predictions predictor(0.5);
79 TruncatedSeq s;
80
81 s.add(0.5);
82 double p1 = predictor.get_new_prediction(&s);
83 ASSERT_GT(p1, s.davg()) << "First prediction must be larger than average";
84
85 s.add(0.2);
86 double p2 = predictor.get_new_prediction(&s);
87 ASSERT_GT(p1, p2) << "First prediction must be larger than second";
88
89 s.add(0.5);
90 double p3 = predictor.get_new_prediction(&s);
91 ASSERT_GT(p2, p3) << "Second prediction must be larger than third";
92
93 s.add(0.2);
94 s.add(2.0);
95 double p4 = predictor.get_new_prediction(&s);
96 ASSERT_GT(p4, p3) << "Fourth prediction must be larger than third";
97 }
|