|
Java example source code file (CaliperRunner.java)
This example Java source code file (CaliperRunner.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Learn more about this Java project at its project page.
The CaliperRunner.java Java example source code
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you 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 io.netty.test.udt.util;
import com.google.caliper.ConfiguredBenchmark;
import com.google.caliper.Environment;
import com.google.caliper.EnvironmentGetter;
import com.google.caliper.Json;
import com.google.caliper.Result;
import com.google.caliper.Run;
import com.google.caliper.Runner;
import com.google.caliper.Scenario;
import com.google.caliper.ScenarioResult;
import com.google.caliper.SimpleBenchmark;
import com.yammer.metrics.core.TimerContext;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* Custom caliper runner for {@link CaliperBench}.
*/
public final class CaliperRunner {
private static final InternalLogger log = InternalLoggerFactory.getInstance(CaliperRunner.class);
private CaliperRunner() {
}
/**
* Parse bench parameters.
*/
public static List<String> valueList(final String valueText) {
return Arrays.asList(valueText.split(","));
}
/**
* Execute full cycle: warm up, execute and publish benchmark.
*/
public static void execute(final Class<? extends CaliperBench> klaz)
throws Exception {
execute("WARMUP", klaz);
Run run = execute("REPORT", klaz);
publish(newResult(run));
}
/**
* Execute benchmark for all parameter combinations.
*/
public static Run execute(final String name,
final Class<? extends CaliperBench> klaz) throws Exception {
final CaliperBench booter = klaz.newInstance();
final List<Map varsSet = product(booter);
final Run run = newRun(klaz.getName());
int index = 0;
for (final Map<String, String> vars : varsSet) {
final int done = 100 * index++ / varsSet.size();
log.info("{} {}% {}", name, done, vars);
/** call setUp() */
final ConfiguredBenchmark runner = booter.createBenchmark(vars);
final CaliperBench bench = (CaliperBench) runner.getBenchmark();
final CaliperMeasure measure = bench.measure();
measure.variables().putAll(vars);
/** call timeXXX() */
runner.run(0);
/** call tearDown() */
runner.close();
measure.appendTo(run);
}
return run;
}
/**
* Convert caliper result into JSON string.
*/
public static String json(final Result result) {
return Json.getGsonInstance().toJson(result);
}
/**
* Map signature based on map values.
*/
public static String signature(final Map<String, String> map) {
final StringBuilder text = new StringBuilder();
for (final String item : map.values()) {
text.append(String.format("%20s", item));
}
return text.toString();
}
/**
* Generate all parameter combinations for {@link SimpleBenchmark}.
*/
public static List<Map product(final SimpleBenchmark bench) {
final Set<Map collect = new HashSet
Other Java examples (source code examples)
Here is a short list of links related to this Java CaliperRunner.java source code file:
|