com.google.caliper.report.console.ConsoleReport.java Source code

Java tutorial

Introduction

Here is the source code for com.google.caliper.report.console.ConsoleReport.java

Source

/**
 * Copyright (C) 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.caliper.report.console;

import com.google.caliper.plugin.AbstractSettingsReport;
import com.google.caliper.report.BenchmarkReport;
import com.google.caliper.report.ReportHeader;
import com.google.caliper.report.ReportRow;
import com.google.common.base.Strings;
import java.util.List;

/**
 * Prints a report containing the tested values and the corresponding
 * measurements. Measurements are grouped by variable using indentation.
 * Alongside numeric values, quick-glance ASCII art bar charts are printed.
 * Sample output (this may not represent the exact form that is produced):
 * <pre>
 *              benchmark          d     ns logarithmic runtime
 * ConcatenationBenchmark 3.14159265   4397 ========================
 * ConcatenationBenchmark       -0.0    223 ===============
 *     FormatterBenchmark 3.14159265  33999 ==============================
 *     FormatterBenchmark       -0.0  26399 =============================
 * </pre>
 *
 * @author Renat.Hilmanov
 */
public final class ConsoleReport extends AbstractSettingsReport {

    /**
     * Report name.
     */
    public static final String NAME = "console-report";
    public static final String VERSION = "1.0";

    private static final int NAME_COLUMN_WIDTH = 15;
    private static final int VALUE_COLUMN_WIDTH = 7;
    private static final int GRAPH_COLUMN_WIDTH = 30;
    private static final int BAR_GRAPH_WIDTH = 24;

    /**
     * Constructs console report.
     */
    public ConsoleReport() {
        super(NAME, VERSION);
    }

    /**
     * Generates textual report.
     *
     * @param report benchmark report
     */
    public void renderReport(BenchmarkReport report) {
        printValues(report);
    }

    /**
     * Prints a table of values.
     * @param report
     */
    private void printValues(BenchmarkReport report) {
        ReportHeader header = report.getHeader();
        String benchmarkTitle = String.format("%-" + NAME_COLUMN_WIDTH + "s", header.getBenchmarkTitle());
        String valueTitle = String.format("%-" + VALUE_COLUMN_WIDTH + "s", header.getValueTitle());
        String scaledTitle = String.format("%-" + GRAPH_COLUMN_WIDTH + "s", header.getScaledTitle());

        System.out.print(benchmarkTitle);
        System.out.print(valueTitle);
        System.out.print(scaledTitle);
        System.out.println();

        List<ReportRow> reportRows = report.getRows();

        for (ReportRow row : reportRows) {
            String variableName = String.format("%-" + NAME_COLUMN_WIDTH + "s", row.getVariableName());
            String variableValue = String.format("%-" + VALUE_COLUMN_WIDTH + "s", row.getValue());
            String valueGraph = String.format("%-" + GRAPH_COLUMN_WIDTH + "s", barGraph(row.getScaledValue()));

            System.out.print(variableName);
            System.out.print(variableValue);
            System.out.print(valueGraph);
            System.out.println();
        }
    }

    /**
     * Returns a string containing a bar of proportional width to the specified
     * value.
     *
     * @param graphLength length of the textual graph element
     * @return textual graph representation
     */
    private String barGraph(int graphLength) {
        graphLength = Math.max(1, graphLength);
        graphLength = Math.min(BAR_GRAPH_WIDTH, graphLength);
        return Strings.repeat("=", graphLength);
    }
}