Skip to content

Commit

Permalink
Messagelist benchmark (#119)
Browse files Browse the repository at this point in the history
Load test messages from csv files or generate 25000 static ones if the benchmark requests that, otherwise fall back on a single message.
  • Loading branch information
kroepke authored and bernd committed Oct 21, 2016
1 parent 68165ed commit 95944cb
Showing 1 changed file with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@
*/
package org.graylog.benchmarks.pipeline;

import au.com.bytecode.opencsv.CSVParser;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;
import com.eaio.uuid.UUID;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.MapMaker;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
import com.google.common.io.LineProcessor;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
Expand Down Expand Up @@ -66,13 +71,16 @@
import org.graylog2.streams.StreamImpl;
import org.graylog2.streams.StreamService;
import org.joda.time.DateTime;
import org.jooq.lambda.Seq;
import org.jooq.lambda.tuple.Tuple2;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.profile.GCProfiler;
import org.openjdk.jmh.results.RunResult;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
Expand All @@ -83,6 +91,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
Expand All @@ -91,8 +100,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -102,9 +113,8 @@

public class PipelinePerformanceBenchmarks {
private static final Logger LOG = LoggerFactory.getLogger(PipelinePerformanceBenchmarks.class);
public static final Message MESSAGE = new Message("hallo welt", "127.0.0.1", Tools.nowUTC());

private static final String BENCHMARKS_RESOURCE_DIRECTORY = "/benchmarks";
private static final Message MESSAGE = new Message("hallo welt", "127.0.0.1", Tools.nowUTC());
private static String benchmarkDir = System.getProperty("benchmarkDir", "benchmarks");

@State(Scope.Benchmark)
Expand All @@ -116,6 +126,7 @@ public static class PipelineConfig {
private PipelineInterpreter interpreter;
private BenchmarkConfig config;
private Injector injector;
private Iterator<Message> messageCycler;
// enable when using yourkit for single runs
// private Controller controller;

Expand Down Expand Up @@ -159,6 +170,8 @@ protected void configure() {
configFiles.put(Type.PIPELINE, inputFile);
} else if (name.equals("benchmark.toml")) {
configFiles.put(Type.CONFIG, inputFile);
} else if (name.endsWith(".csv")) {
configFiles.put(Type.MESSAGES, inputFile);
} else {
LOG.warn("unrecognized file {} found, it will be ignored.", inputFile);
}
Expand Down Expand Up @@ -232,6 +245,31 @@ protected void configure() {
}
}

final List<Message> loadedMessages = Lists.newArrayList();
configFiles.get(Type.MESSAGES).forEach(file -> {
try {
loadedMessages.addAll(com.google.common.io.Files.readLines(file,
StandardCharsets.UTF_8,
new CsvMessageFileProcessor()));
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(-3);
}
});
if (!loadedMessages.isEmpty()) {
messageCycler = Iterators.cycle(loadedMessages);
}

if (!configFiles.containsKey(Type.MESSAGES)) {
if ("generate".equalsIgnoreCase(config.messages)) {
final ArrayList<Message> objects = Lists.newArrayList();
Seq.range(0, 25000).forEach(i -> objects.add(new Message("hallo welt", "127.0.0.1", Tools.nowUTC())));
messageCycler = Iterators.cycle(objects);
} else {
messageCycler = Iterators.cycle(MESSAGE);
}
}

interpreter = injector.getInstance(PipelineInterpreter.class);
}

Expand Down Expand Up @@ -426,12 +464,47 @@ public Map<String, List<ValidationResult>> validate(Map<String, Validator> valid
}
}

private static class CsvMessageFileProcessor implements LineProcessor<List<Message>> {
String[] fieldNames;
private CSVParser csvParser = new CSVParser();
private List<Message> messages = Lists.newArrayList();
boolean firstLine = true;

@Override
public boolean processLine(@Nonnull String line) throws IOException {
final String[] strings = csvParser.parseLine(line);
if (strings == null) {
return false;
}
if (firstLine) {
fieldNames = strings;
firstLine = false;
return true;
}

final Map<String, Object> fields = Seq.of(fieldNames)
.zipWithIndex()
.map(nameAndIndex -> nameAndIndex.map2(index -> strings[Math.toIntExact(index)]))
.collect(Collectors.toMap(Tuple2::v1, Tuple2::v2));
fields.put(Message.FIELD_ID, new UUID().toString());
messages.add(new Message(fields));
return true;
}

@Override
public List<Message> getResult() {
return messages;
}
}

@SuppressWarnings({"unused", "MismatchedQueryAndUpdateOfCollection"})
private class BenchmarkConfig {
private String name;

private List<StreamDescription> streams;

private String messages;

private class StreamDescription {
private String name;
private String description;
Expand All @@ -445,13 +518,15 @@ private class StreamDescription {
private enum Type {
CONFIG,
RULE,
MESSAGES,
PIPELINE
}
}

@Benchmark
public void runPipeline(PipelineConfig config, Blackhole bh) {
bh.consume(config.interpreter.process(MESSAGE));
// forever loop over the messages
bh.consume(config.interpreter.process(config.messageCycler.next()));
}

public static void main(String[] args) throws RunnerException, URISyntaxException, IOException {
Expand All @@ -465,7 +540,7 @@ public static void main(String[] args) throws RunnerException, URISyntaxExceptio
.desc("Benchmark directory (default: 'benchmarks')")
.required(false)
.build());
options.addOption(new Option("f", "Number of forks (default 1). Set to 0 to allow attaching a debugger/profiler"));
options.addOption("f", true, "Number of forks (default 1). Set to 0 to allow attaching a debugger/profiler");
options.addOption(Option.builder("n")
.longOpt("name")
.desc("Only run benchmark with the given name")
Expand Down Expand Up @@ -526,6 +601,7 @@ public static void main(String[] args) throws RunnerException, URISyntaxExceptio
.param("directoryName", benchmarkParams)
.jvmArgsAppend("-DbenchmarkDir="+benchmarkDir)
.resultFormat(ResultFormatType.CSV)
.addProfiler(GCProfiler.class)
.build();

final Runner runner = new Runner(opt);
Expand Down

0 comments on commit 95944cb

Please sign in to comment.