aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test/src/org/jacoco/core/test/perf/PerfOutputWriter.java
blob: c569f850a0bff5bcbc571e513c43aa1bc462d4e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*******************************************************************************
 * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.core.test.perf;

import static java.lang.String.format;

import java.io.PrintWriter;

import org.jacoco.core.JaCoCo;

/**
 * Formatted text output.
 */
public class PerfOutputWriter implements IPerfOutput {

	private final PrintWriter writer;

	public PerfOutputWriter(final PrintWriter writer) {
		this.writer = writer;
		writeHeader();
	}

	private void writeHeader() {
		writer.printf("JaCoCo Performance Data%n%n");
		writer.printf("JaCoCo Version:  %s%n", JaCoCo.VERSION);
		writer.printf("JVM Vendor:      %s%n",
				System.getProperty("java.vm.vendor"));
		writer.printf("JVM Version:     %s%n%n",
				System.getProperty("java.vm.version"));
		writer.println(
				"scenario                         instr     ref    overhead");
		writer.println(
				"----------------------------------------------------------");
	}

	public void writeTimeResult(final String description, final long duration,
			final long reference) {
		final double dms = (double) duration / 1000000;
		if (reference == NO_REFERENCE) {
			writeResult(description, dms, "%.2f", "ms");
		} else {
			final double rms = (double) reference / 1000000;
			writeResult(description, dms, rms, "%.2f", "ms");
		}
	}

	public void writeByteResult(String description, long size, long reference) {
		if (size == 0) {
			return;
		}
		if (reference == NO_REFERENCE) {
			writeResult(description, size, "%.0f", "bytes");
		} else {
			writeResult(description, size, reference, "%.0f", "bytes");
		}
	}

	private void writeResult(final String description, final double subject,
			String fmt, String unit) {
		writer.printf("%-30s%8s         %-6s%n", description,
				format(fmt, Double.valueOf(subject)), unit);
	}

	private void writeResult(final String description, final double subject,
			final double reference, String fmt, String unit) {
		double overhead = 100 * (subject - reference) / reference;
		writer.printf("%-30s%8s%8s %-6s%4.0f%%%n", description,
				format(fmt, Double.valueOf(subject)),
				format(fmt, Double.valueOf(reference)), unit,
				Double.valueOf(overhead));
	}

}