Skip to content

Commit a7526fe

Browse files
committed
Refactor benchmark parameters and adjust time units. (#198)
1 parent eecfdae commit a7526fe

File tree

4 files changed

+166
-64
lines changed

4 files changed

+166
-64
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.openjdk.jmh.annotations.Benchmark;
20+
import org.openjdk.jmh.annotations.BenchmarkMode;
21+
import org.openjdk.jmh.annotations.Fork;
22+
import org.openjdk.jmh.annotations.Measurement;
23+
import org.openjdk.jmh.annotations.Mode;
24+
import org.openjdk.jmh.annotations.OutputTimeUnit;
25+
import org.openjdk.jmh.annotations.Scope;
26+
import org.openjdk.jmh.annotations.State;
27+
import org.openjdk.jmh.annotations.Warmup;
28+
//import org.openjdk.jmh.profile.GCProfiler;
29+
//import org.openjdk.jmh.profile.StackProfiler;
30+
//import org.openjdk.jmh.runner.Runner;
31+
//import org.openjdk.jmh.runner.RunnerException;
32+
//import org.openjdk.jmh.runner.options.Options;
33+
//import org.openjdk.jmh.runner.options.OptionsBuilder;
34+
35+
import java.util.concurrent.TimeUnit;
36+
37+
@Fork(2)
38+
@Warmup(iterations = 2)
39+
@Measurement(iterations = 4) // checkstyle-disable-line MagicNumber
40+
@BenchmarkMode(Mode.Throughput) // AverageTime
41+
@OutputTimeUnit(TimeUnit.SECONDS)
42+
@State(Scope.Thread)
43+
public abstract class AbstractBenchmark {
44+
45+
public AbstractBenchmark() {
46+
}
47+
48+
@Benchmark
49+
public void benchmark() {
50+
workload();
51+
}
52+
53+
protected abstract void workload();
54+
55+
/*
56+
public static void main(final String[] args) throws RunnerException {
57+
final Options opt = new OptionsBuilder()
58+
.include(MetafixBenchmark.class.getSimpleName())
59+
.addProfiler(StackProfiler.class)
60+
//.addProfiler(GCProfiler.class)
61+
.build();
62+
63+
new Runner(opt).run();
64+
}
65+
*/
66+
67+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.openjdk.jmh.annotations.OutputTimeUnit;
20+
21+
import java.util.concurrent.TimeUnit;
22+
23+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
24+
public class BaselineBenchmark extends AbstractBenchmark {
25+
26+
public BaselineBenchmark() {
27+
}
28+
29+
@Override
30+
protected void workload() {
31+
// this method was intentionally left blank.
32+
}
33+
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.openjdk.jmh.annotations.Param;
20+
import org.openjdk.jmh.annotations.Setup;
21+
22+
public class FixParseBenchmark extends AbstractBenchmark {
23+
24+
protected static final String BASE = "src/jmh/resources/org/metafacture/metafix";
25+
26+
private static final String FIXES = BASE + "/fixes/%s" + Metafix.FIX_EXTENSION;
27+
28+
protected String fixFile; // checkstyle-disable-line VisibilityModifier
29+
30+
@Param({ // checkstyle-disable-line AnnotationUseStyle
31+
"nothing"
32+
})
33+
private String fixDef;
34+
35+
public FixParseBenchmark() {
36+
}
37+
38+
@Setup
39+
public void setup() {
40+
fixFile = String.format(FIXES, fixDef);
41+
}
42+
43+
@Override
44+
protected void workload() {
45+
FixStandaloneSetup.parseFix(fixFile);
46+
}
47+
48+
}

metafix/src/jmh/java/org/metafacture/metafix/MetafixBenchmark.java

Lines changed: 17 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -23,68 +23,43 @@
2323
import org.metafacture.json.JsonDecoder;
2424
import org.metafacture.json.JsonEncoder;
2525

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;
3226
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;
4327

4428
import java.io.FileNotFoundException;
45-
import java.util.concurrent.TimeUnit;
29+
import java.io.UncheckedIOException;
4630

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 {
31+
public class MetafixBenchmark extends FixParseBenchmark { // checkstyle-disable-line ClassDataAbstractionCoupling
5432

5533
// TODO: Need to inject system properties into JMHTask's JavaExec process.
5634
//private static final boolean DEBUG_OUTPUT = Boolean.parseBoolean(System.getProperty("org.metafacture.metafix.debugBenchmarkOutput"));
5735
private static final boolean DEBUG_OUTPUT = false;
5836

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;
6237
private static final String INPUT = BASE + "/input/%s.json";
6338

64-
@Param({ // checkstyle-disable-line AnnotationUseStyle
65-
"nothing"
66-
})
67-
private String fixDef;
39+
private FileOpener fileOpener;
40+
private String inputFile;
6841

6942
@Param({ // checkstyle-disable-line AnnotationUseStyle
7043
"empty"
7144
})
7245
private String input;
7346

74-
private String fixFile;
75-
private String inputFile;
76-
77-
private FileOpener fileOpener;
78-
7947
public MetafixBenchmark() {
8048
}
8149

82-
@Setup
83-
public void setup() throws FileNotFoundException {
84-
fixFile = String.format(FIXES, fixDef);
50+
@Override
51+
public void setup() {
52+
super.setup();
53+
8554
inputFile = String.format(INPUT, input);
8655

87-
final Metafix metafix = new Metafix(fixFile);
56+
final Metafix metafix;
57+
try {
58+
metafix = new Metafix(fixFile);
59+
}
60+
catch (final FileNotFoundException e) {
61+
throw new UncheckedIOException(e);
62+
}
8863

8964
if (DEBUG_OUTPUT) {
9065
metafix
@@ -103,31 +78,9 @@ public void setup() throws FileNotFoundException {
10378
.setReceiver(metafix);
10479
}
10580

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() {
81+
@Override
82+
protected void workload() {
11883
fileOpener.process(inputFile);
11984
}
12085

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-
13386
}

0 commit comments

Comments
 (0)