1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.io.XmlWriter;
|
---|
5 |
|
---|
6 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Timer utilities for performance tests.
|
---|
10 | * @author Michael Zangl
|
---|
11 | */
|
---|
12 | public final class PerformanceTestUtils {
|
---|
13 | /**
|
---|
14 | * A timer that measures the time from it's creation to the {@link #done()} call.
|
---|
15 | * @author Michael Zangl
|
---|
16 | */
|
---|
17 | public static class PerformanceTestTimer {
|
---|
18 | private final String name;
|
---|
19 | private final long time;
|
---|
20 | private boolean measurementPlotsPlugin = false;
|
---|
21 |
|
---|
22 | protected PerformanceTestTimer(String name) {
|
---|
23 | this.name = name;
|
---|
24 | time = System.nanoTime();
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Activate output for the Jenkins Measurement Plots Plugin.
|
---|
29 | * @param active true if it should be activated
|
---|
30 | */
|
---|
31 | public void setMeasurementPlotsPluginOutput(boolean active) {
|
---|
32 | measurementPlotsPlugin = active;
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Prints the time since this timer was created.
|
---|
37 | */
|
---|
38 | public void done() {
|
---|
39 | long dTime = (System.nanoTime() - time) / 1000000;
|
---|
40 | if (measurementPlotsPlugin) {
|
---|
41 | measurementPlotsPluginOutput(name + "(ms)", dTime);
|
---|
42 | } else {
|
---|
43 | System.out.println("TIMER " + name + ": " + dTime + "ms");
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | private PerformanceTestUtils() {
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Starts a new performance timer.
|
---|
53 | * @param name The name/description of the timer.
|
---|
54 | * @return A {@link PerformanceTestTimer} object of which you can call {@link PerformanceTestTimer#done()} when done.
|
---|
55 | */
|
---|
56 | @SuppressFBWarnings(value = "DM_GC", justification = "Performance test code")
|
---|
57 | public static PerformanceTestTimer startTimer(String name) {
|
---|
58 | System.gc();
|
---|
59 | System.runFinalization();
|
---|
60 | return new PerformanceTestTimer(name);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Emit one data value for the Jenkins Measurement Plots Plugin.
|
---|
65 | *
|
---|
66 | * The plugin collects the values over multiple builds and plots them in a diagram.
|
---|
67 | *
|
---|
68 | * @param name the name / title of the measurement
|
---|
69 | * @param value the value
|
---|
70 | * @see https://wiki.jenkins-ci.org/display/JENKINS/Measurement+Plots+Plugin
|
---|
71 | */
|
---|
72 | public static void measurementPlotsPluginOutput(String name, double value) {
|
---|
73 | System.err.println("<measurement><name>"+XmlWriter.encode(name)+"</name><value>"+value+"</value></measurement>");
|
---|
74 | }
|
---|
75 | }
|
---|