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 
  25 #include "precompiled.hpp"
  26 #include "gc/g1/g1Predictions.hpp"
  27 #include "unittest.hpp"
  28 
  29 static const double epsilon = 1e-6;
  30 
  31 // Some basic formula tests with confidence = 0.0
  32 TEST_VM(G1Predictions, basic_predictions) {
  33   G1Predictions predictor(0.0);
  34   TruncatedSeq s;
  35 
  36   double p0 = predictor.get_new_prediction(&s);
  37   ASSERT_LT(p0, epsilon) << "Initial prediction of empty sequence must be 0.0";
  38 
  39   s.add(5.0);
  40   double p1 = predictor.get_new_prediction(&s);
  41   ASSERT_NEAR(p1, 5.0, epsilon);
  42 
  43   for (int i = 0; i < 40; i++) {
  44     s.add(5.0);
  45   }
  46   double p2 = predictor.get_new_prediction(&s);
  47   ASSERT_NEAR(p2, 5.0, epsilon);
  48 }
  49 
  50 // The following tests checks that the initial predictions are based on
  51 // the average of the sequence and not on the stddev (which is 0).
  52 TEST_VM(G1Predictions, average_not_stdev_predictions) {
  53   G1Predictions predictor(0.5);
  54   TruncatedSeq s;
  55 
  56   s.add(1.0);
  57   double p1 = predictor.get_new_prediction(&s);
  58   ASSERT_GT(p1, s.davg()) << "First prediction must be greater than average";
  59 
  60   s.add(1.0);
  61   double p2 = predictor.get_new_prediction(&s);
  62   ASSERT_GT(p1, p2) << "First prediction must be greater than second";
  63 
  64   s.add(1.0);
  65   double p3 = predictor.get_new_prediction(&s);
  66   ASSERT_GT(p2, p3) << "Second prediction must be greater than third";
  67 
  68   s.add(1.0);
  69   s.add(1.0); // Five elements are now in the sequence.
  70   double p4 = predictor.get_new_prediction(&s);
  71   ASSERT_LT(p4, p3) << "Fourth prediction must be smaller than third";
  72   ASSERT_NEAR(p4, 1.0, epsilon);
  73 }
  74 
  75 // The following tests checks that initially prediction based on
  76 // the average is used, that gets overridden by the stddev prediction at
  77 // the end.
  78 TEST_VM(G1Predictions, average_stdev_predictions) {
  79   G1Predictions predictor(0.5);
  80   TruncatedSeq s;
  81 
  82   s.add(0.5);
  83   double p1 = predictor.get_new_prediction(&s);
  84   ASSERT_GT(p1, s.davg()) << "First prediction must be greater than average";
  85 
  86   s.add(0.2);
  87   double p2 = predictor.get_new_prediction(&s);
  88   ASSERT_GT(p1, p2) << "First prediction must be greater than second";
  89 
  90   s.add(0.5);
  91   double p3 = predictor.get_new_prediction(&s);
  92   ASSERT_GT(p2, p3) << "Second prediction must be greater than third";
  93 
  94   s.add(0.2);
  95   s.add(2.0);
  96   double p4 = predictor.get_new_prediction(&s);
  97   ASSERT_GT(p4, p3) << "Fourth prediction must be greater than third";
  98 }