/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.jmh.runner;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.infra.BenchmarkParams;
import org.openjdk.jmh.infra.IterationParams;
import org.openjdk.jmh.profile.ExternalProfiler;
import org.openjdk.jmh.profile.ProfilerException;
import org.openjdk.jmh.profile.ProfilerFactory;
import org.openjdk.jmh.results.*;
import org.openjdk.jmh.results.format.ResultFormatFactory;
import org.openjdk.jmh.runner.format.OutputFormat;
import org.openjdk.jmh.runner.format.OutputFormatFactory;
import org.openjdk.jmh.runner.link.BinaryLinkServer;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.ProfilerConfig;
import org.openjdk.jmh.runner.options.TimeValue;
import org.openjdk.jmh.runner.options.VerboseMode;
import org.openjdk.jmh.util.*;
import org.openjdk.jmh.util.Optional;
import java.io.*;
import java.lang.management.ManagementFactory;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* Runner executes JMH benchmarks.
*
*
This is the entry point for JMH Java API.
*
* {@link Runner} is not usually reusable. After you execute any method on the {@link Runner}, you should digest
* the results, give up on current {@link Runner}, and instantiate another one. This class may be turned into
* static class in future releases.
*/
public class Runner extends BaseRunner {
private static final int TAIL_LINES_ON_ERROR = Integer.getInteger("jmh.tailLines", 20);
private static final String JMH_LOCK_FILE = System.getProperty("java.io.tmpdir") + "/jmh.lock";
private static final Boolean JMH_LOCK_IGNORE = Boolean.getBoolean("jmh.ignoreLock");
private final BenchmarkList list;
private int cpuCount;
/**
* Create runner with the custom OutputFormat.
*
* @param options options to use
* @param format OutputFormat to use
*/
public Runner(Options options, OutputFormat format) {
super(options, format);
this.list = BenchmarkList.defaultList();
}
/**
* Create Runner with the given options.
* This method sets up the {@link org.openjdk.jmh.runner.format.OutputFormat} as
* mandated by options.
* @param options options to use.
*/
public Runner(Options options) {
this(options, createOutputFormat(options));
}
private static OutputFormat createOutputFormat(Options options) {
// sadly required here as the check cannot be made before calling this method in constructor
if (options == null) {
throw new IllegalArgumentException("Options not allowed to be null.");
}
PrintStream out;
if (options.getOutput().hasValue()) {
try {
out = new PrintStream(options.getOutput().get());
} catch (FileNotFoundException ex) {
throw new IllegalStateException(ex);
}
} else {
// Protect the System.out from accidental closing
try {
out = new UnCloseablePrintStream(System.out, Utils.guessConsoleEncoding());
} catch (UnsupportedEncodingException ex) {
throw new IllegalStateException(ex);
}
}
return OutputFormatFactory.createFormatInstance(out, options.verbosity().orElse(Defaults.VERBOSITY));
}
/**
* Print matching benchmarks into output.
*/
public void list() {
Set benchmarks = list.find(out, options.getIncludes(), options.getExcludes());
out.println("Benchmarks: ");
for (BenchmarkListEntry benchmark : benchmarks) {
out.println(benchmark.getUsername());
}
}
/**
* Print matching benchmarks with parameters into output.
*/
public void listWithParams() {
Set benchmarks = list.find(out, options.getIncludes(), options.getExcludes());
out.println("Benchmarks: ");
for (BenchmarkListEntry benchmark : benchmarks) {
out.println(benchmark.getUsername());
Optional