-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new command to run benchmark of Schematron rules (TEDEFO-3637)
Use JMH to run the Schematron rules of the SDK being analyzed against a set of large XML notices, and write the results to a JSON file. This allows detecting when a change in the rules makes them slower, by comparing with results from another run. The notices used in the benchmark are included as resources, as we need them to be always the same, and to be large enough to make slowdowns noticeable. So we can't use the notice examples from the SDK being analyzed.
- Loading branch information
1 parent
ab8fa33
commit 027a060
Showing
6 changed files
with
115,872 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/main/java/eu/europa/ted/eforms/sdk/analysis/SchematronBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package eu.europa.ted.eforms.sdk.analysis; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.text.MessageFormat; | ||
|
||
import javax.xml.transform.Source; | ||
import javax.xml.transform.stream.StreamSource; | ||
|
||
import org.apache.commons.lang3.Validate; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
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.Warmup; | ||
import org.openjdk.jmh.results.format.ResultFormatType; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.helger.commons.io.resource.FileSystemResource; | ||
import com.helger.commons.io.resource.IReadableResource; | ||
import com.helger.schematron.ISchematronResource; | ||
import com.helger.schematron.pure.SchematronResourcePure; | ||
|
||
@State(Scope.Benchmark) | ||
public class SchematronBenchmark { | ||
private static final Logger logger = LoggerFactory.getLogger(SchematronBenchmark.class); | ||
|
||
// Path to the root of the eForms SDK, overriden with the path given on the command line | ||
@Param("../eforms-sdk") | ||
public String sdkRootPath; | ||
|
||
// Filenames of XML notices included as resources in this project | ||
@Param({"16.xml", "29.xml"}) | ||
public String noticeFilename; | ||
|
||
private ISchematronResource phSchematron; | ||
|
||
public static int run(final Path sdkRoot) throws Exception { | ||
logger.warn("Benchmarking Schematron rules from SDK under folder [{}]", sdkRoot); | ||
|
||
Validate.notNull(sdkRoot, "Undefined SDK root path"); | ||
if (!Files.isDirectory(sdkRoot)) { | ||
throw new FileNotFoundException(sdkRoot.toString()); | ||
} | ||
|
||
Options opt = new OptionsBuilder() | ||
.include(SchematronBenchmark.class.getSimpleName()) | ||
.param("sdkRootPath", sdkRoot.toString()) | ||
.resultFormat(ResultFormatType.JSON) | ||
.build(); | ||
|
||
new Runner(opt).run(); | ||
|
||
return 0; | ||
} | ||
|
||
@Setup | ||
public void setup() { | ||
SdkLoader sdkLoader = new SdkLoader(Path.of(sdkRootPath)); | ||
|
||
// Use the first set of Schematron rules (dynamic/static) | ||
// They are mostly identical, so it does not make a difference for the benchmark | ||
Path file = sdkLoader.getSchematronFilesPaths().get(0); | ||
IReadableResource schematron = new FileSystemResource(file); | ||
phSchematron = new SchematronResourcePure(schematron); | ||
} | ||
|
||
@Benchmark | ||
@Fork(1) | ||
@Warmup(iterations = 1) | ||
@Measurement(iterations = 3) | ||
@BenchmarkMode(Mode.AverageTime) | ||
public int executeValidation() throws Exception { | ||
|
||
String resourceName = MessageFormat.format("benchmark/notices/{0}", noticeFilename); | ||
InputStream stream = this.getClass().getClassLoader().getResourceAsStream(resourceName); | ||
Validate.notNull(stream, "Notice file not found in resources: " + resourceName); | ||
|
||
Source source = new StreamSource(stream); | ||
phSchematron.applySchematronValidation(source); | ||
|
||
return 0; | ||
} | ||
} |
Oops, something went wrong.