test/hotspot/jtreg/compiler/graalunit/com.oracle.mxtool.junit/com/oracle/mxtool/junit/TimingDecorator.java
author epavlova
Wed, 09 Oct 2019 10:59:45 -0700
changeset 58523 fb3d408c7a7e
parent 50908 7c51db95ccb6
permissions -rw-r--r--
8231903: [Graal] Update com.oracle.mxtool.junit classes Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50908
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
     1
/*
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
     2
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
     4
 *
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
     8
 *
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    13
 * accompanied this code).
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    14
 *
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    18
 *
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    21
 * questions.
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    22
 */
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    23
package com.oracle.mxtool.junit;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    24
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    25
import java.util.Map;
58523
fb3d408c7a7e 8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents: 50908
diff changeset
    26
import java.util.concurrent.ConcurrentHashMap;
50908
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    27
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    28
import org.junit.runner.Description;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    29
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    30
/**
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    31
 * Timing support for JUnit test runs.
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    32
 */
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    33
class TimingDecorator extends MxRunListenerDecorator {
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    34
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    35
    private long startTime;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    36
    private long classStartTime;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    37
    private Description currentTest;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    38
    final Map<Class<?>, Long> classTimes;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    39
    final Map<Description, Long> testTimes;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    40
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    41
    TimingDecorator(MxRunListener l) {
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    42
        super(l);
58523
fb3d408c7a7e 8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents: 50908
diff changeset
    43
        this.classTimes = new ConcurrentHashMap<>();
fb3d408c7a7e 8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents: 50908
diff changeset
    44
        this.testTimes = new ConcurrentHashMap<>();
50908
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    45
    }
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    46
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    47
    @Override
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    48
    public void testClassStarted(Class<?> clazz) {
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    49
        classStartTime = System.nanoTime();
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    50
        super.testClassStarted(clazz);
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    51
    }
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    52
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    53
    @Override
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    54
    public void testClassFinished(Class<?> clazz, int numPassed, int numFailed) {
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    55
        long totalTime = System.nanoTime() - classStartTime;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    56
        super.testClassFinished(clazz, numPassed, numFailed);
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    57
        if (beVerbose()) {
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    58
            getWriter().print(' ' + valueToString(totalTime));
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    59
        }
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    60
        classTimes.put(clazz, totalTime / 1_000_000);
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    61
    }
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    62
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    63
    @Override
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    64
    public void testStarted(Description description) {
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    65
        currentTest = description;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    66
        startTime = System.nanoTime();
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    67
        super.testStarted(description);
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    68
    }
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    69
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    70
    @Override
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    71
    public void testFinished(Description description) {
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    72
        long totalTime = System.nanoTime() - startTime;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    73
        super.testFinished(description);
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    74
        if (beVerbose()) {
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    75
            getWriter().print(" " + valueToString(totalTime));
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    76
        }
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    77
        currentTest = null;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    78
        testTimes.put(description, totalTime / 1_000_000);
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    79
    }
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    80
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    81
    private static String valueToString(long valueNS) {
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    82
        long timeWholeMS = valueNS / 1_000_000;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    83
        long timeFractionMS = (valueNS / 100_000) % 10;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    84
        return String.format("%d.%d ms", timeWholeMS, timeFractionMS);
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    85
    }
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    86
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    87
    /**
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    88
     * Gets the test currently starting but not yet completed along with the number of milliseconds
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    89
     * it has been executing.
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    90
     *
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    91
     * @return {@code null} if there is no test currently executing
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    92
     */
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    93
    public Object[] getCurrentTestDuration() {
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    94
        Description current = currentTest;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    95
        if (current != null) {
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    96
            long timeMS = (System.nanoTime() - startTime) / 1_000_000;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    97
            return new Object[]{current, timeMS};
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    98
        }
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
    99
        return null;
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
   100
    }
7c51db95ccb6 8205207: Port Graal unit tests under jtreg
epavlova
parents:
diff changeset
   101
}