Skip to content

Commit eecfdae

Browse files
committed
Add basic infrastructure for benchmarking (JMH).
- Execute `./gradlew jmh` to run all benchmarks (i.e., all combinations of fixes and input files). - Place fixes to benchmark in `metafix/src/jmh/resources/org/metafacture/metafix/fixes/` and add to `MetafixBenchmark.fixDef` parameter. - Place input files to benchmark in `metafix/src/jmh/resources/org/metafacture/metafix/input/` and add to `MetafixBenchmark.input` parameter. https://github.com/openjdk/jmh
1 parent e144936 commit eecfdae

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

metafix/build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
id 'maven-publish'
3+
id 'me.champeau.jmh' version '0.6.6'
34
}
45

56
def passSystemProperties = {
@@ -45,6 +46,14 @@ dependencies {
4546
mwe2 "org.eclipse.xtext:org.eclipse.xtext.common.types:${versions.xtext}"
4647
mwe2 "org.eclipse.xtext:org.eclipse.xtext.xtext.generator:${versions.xtext}"
4748
mwe2 'org.eclipse.xtext:xtext-antlr-generator'
49+
50+
jmh "org.metafacture:metafacture-io:${versions.metafacture}"
51+
jmh "org.metafacture:metafacture-json:${versions.metafacture}"
52+
}
53+
54+
jmhJar {
55+
// SLF4J: Class path contains multiple SLF4J bindings.
56+
exclude '/org/slf4j/impl/StaticLoggerBinder.class'
4857
}
4958

5059
test {
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright 2022 hbz NRW
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.metafacture.metafix;
18+
19+
import org.metafacture.framework.helpers.DefaultStreamReceiver;
20+
import org.metafacture.io.FileOpener;
21+
import org.metafacture.io.ObjectStdoutWriter;
22+
import org.metafacture.io.RecordReader;
23+
import org.metafacture.json.JsonDecoder;
24+
import org.metafacture.json.JsonEncoder;
25+
26+
import org.openjdk.jmh.annotations.Benchmark;
27+
import org.openjdk.jmh.annotations.BenchmarkMode;
28+
import org.openjdk.jmh.annotations.Fork;
29+
import org.openjdk.jmh.annotations.Measurement;
30+
import org.openjdk.jmh.annotations.Mode;
31+
import org.openjdk.jmh.annotations.OutputTimeUnit;
32+
import org.openjdk.jmh.annotations.Param;
33+
import org.openjdk.jmh.annotations.Scope;
34+
import org.openjdk.jmh.annotations.Setup;
35+
import org.openjdk.jmh.annotations.State;
36+
import org.openjdk.jmh.annotations.Warmup;
37+
//import org.openjdk.jmh.profile.GCProfiler;
38+
//import org.openjdk.jmh.profile.StackProfiler;
39+
//import org.openjdk.jmh.runner.Runner;
40+
//import org.openjdk.jmh.runner.RunnerException;
41+
//import org.openjdk.jmh.runner.options.Options;
42+
//import org.openjdk.jmh.runner.options.OptionsBuilder;
43+
44+
import java.io.FileNotFoundException;
45+
import java.util.concurrent.TimeUnit;
46+
47+
@Fork(2)
48+
@Warmup(iterations = 2)
49+
@Measurement(iterations = 4) // checkstyle-disable-line MagicNumber
50+
@BenchmarkMode(Mode.Throughput) // AverageTime
51+
@OutputTimeUnit(TimeUnit.MILLISECONDS) // SECONDS
52+
@State(Scope.Thread)
53+
public class MetafixBenchmark {
54+
55+
// TODO: Need to inject system properties into JMHTask's JavaExec process.
56+
//private static final boolean DEBUG_OUTPUT = Boolean.parseBoolean(System.getProperty("org.metafacture.metafix.debugBenchmarkOutput"));
57+
private static final boolean DEBUG_OUTPUT = false;
58+
59+
private static final String BASE = "src/jmh/resources/org/metafacture/metafix";
60+
61+
private static final String FIXES = BASE + "/fixes/%s" + Metafix.FIX_EXTENSION;
62+
private static final String INPUT = BASE + "/input/%s.json";
63+
64+
@Param({ // checkstyle-disable-line AnnotationUseStyle
65+
"nothing"
66+
})
67+
private String fixDef;
68+
69+
@Param({ // checkstyle-disable-line AnnotationUseStyle
70+
"empty"
71+
})
72+
private String input;
73+
74+
private String fixFile;
75+
private String inputFile;
76+
77+
private FileOpener fileOpener;
78+
79+
public MetafixBenchmark() {
80+
}
81+
82+
@Setup
83+
public void setup() throws FileNotFoundException {
84+
fixFile = String.format(FIXES, fixDef);
85+
inputFile = String.format(INPUT, input);
86+
87+
final Metafix metafix = new Metafix(fixFile);
88+
89+
if (DEBUG_OUTPUT) {
90+
metafix
91+
.setReceiver(new JsonEncoder())
92+
.setReceiver(new ObjectStdoutWriter<String>());
93+
}
94+
else {
95+
metafix
96+
.setReceiver(new DefaultStreamReceiver());
97+
}
98+
99+
fileOpener = new FileOpener();
100+
fileOpener
101+
.setReceiver(new RecordReader())
102+
.setReceiver(new JsonDecoder())
103+
.setReceiver(metafix);
104+
}
105+
106+
@Benchmark
107+
public void baseline() {
108+
// this method was intentionally left blank.
109+
}
110+
111+
@Benchmark
112+
public void parse() {
113+
FixStandaloneSetup.parseFix(fixFile);
114+
}
115+
116+
@Benchmark
117+
public void process() {
118+
fileOpener.process(inputFile);
119+
}
120+
121+
/*
122+
public static void main(final String[] args) throws RunnerException {
123+
final Options opt = new OptionsBuilder()
124+
.include(MetafixBenchmark.class.getSimpleName())
125+
.addProfiler(StackProfiler.class)
126+
//.addProfiler(GCProfiler.class)
127+
.build();
128+
129+
new Runner(opt).run();
130+
}
131+
*/
132+
133+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nothing()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)