test/hotspot/jtreg/compiler/graalunit/com.oracle.mxtool.junit/com/oracle/mxtool/junit/TimingDecorator.java
changeset 50908 7c51db95ccb6
child 58523 fb3d408c7a7e
equal deleted inserted replaced
50907:39d27210c627 50908:7c51db95ccb6
       
     1 /*
       
     2  * Copyright (c) 2014, 2017, 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 package com.oracle.mxtool.junit;
       
    24 
       
    25 import java.util.HashMap;
       
    26 import java.util.Map;
       
    27 
       
    28 import org.junit.runner.Description;
       
    29 
       
    30 /**
       
    31  * Timing support for JUnit test runs.
       
    32  */
       
    33 class TimingDecorator extends MxRunListenerDecorator {
       
    34 
       
    35     private long startTime;
       
    36     private long classStartTime;
       
    37     private Description currentTest;
       
    38     final Map<Class<?>, Long> classTimes;
       
    39     final Map<Description, Long> testTimes;
       
    40 
       
    41     TimingDecorator(MxRunListener l) {
       
    42         super(l);
       
    43         this.classTimes = new HashMap<>();
       
    44         this.testTimes = new HashMap<>();
       
    45     }
       
    46 
       
    47     @Override
       
    48     public void testClassStarted(Class<?> clazz) {
       
    49         classStartTime = System.nanoTime();
       
    50         super.testClassStarted(clazz);
       
    51     }
       
    52 
       
    53     @Override
       
    54     public void testClassFinished(Class<?> clazz, int numPassed, int numFailed) {
       
    55         long totalTime = System.nanoTime() - classStartTime;
       
    56         super.testClassFinished(clazz, numPassed, numFailed);
       
    57         if (beVerbose()) {
       
    58             getWriter().print(' ' + valueToString(totalTime));
       
    59         }
       
    60         classTimes.put(clazz, totalTime / 1_000_000);
       
    61     }
       
    62 
       
    63     @Override
       
    64     public void testStarted(Description description) {
       
    65         currentTest = description;
       
    66         startTime = System.nanoTime();
       
    67         super.testStarted(description);
       
    68     }
       
    69 
       
    70     @Override
       
    71     public void testFinished(Description description) {
       
    72         long totalTime = System.nanoTime() - startTime;
       
    73         super.testFinished(description);
       
    74         if (beVerbose()) {
       
    75             getWriter().print(" " + valueToString(totalTime));
       
    76         }
       
    77         currentTest = null;
       
    78         testTimes.put(description, totalTime / 1_000_000);
       
    79     }
       
    80 
       
    81     private static String valueToString(long valueNS) {
       
    82         long timeWholeMS = valueNS / 1_000_000;
       
    83         long timeFractionMS = (valueNS / 100_000) % 10;
       
    84         return String.format("%d.%d ms", timeWholeMS, timeFractionMS);
       
    85     }
       
    86 
       
    87     /**
       
    88      * Gets the test currently starting but not yet completed along with the number of milliseconds
       
    89      * it has been executing.
       
    90      *
       
    91      * @return {@code null} if there is no test currently executing
       
    92      */
       
    93     public Object[] getCurrentTestDuration() {
       
    94         Description current = currentTest;
       
    95         if (current != null) {
       
    96             long timeMS = (System.nanoTime() - startTime) / 1_000_000;
       
    97             return new Object[]{current, timeMS};
       
    98         }
       
    99         return null;
       
   100     }
       
   101 }