Skip to content

Commit 672c718

Browse files
authored
Merge pull request #1 from HASMAC-AS/GH-273-add-benchmark
GH-273 add benchmarks
2 parents 1a2ed30 + 887ed47 commit 672c718

31 files changed

+1837873
-21
lines changed

pom_parent.xml

+34-21
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@
1212
<packaging>pom</packaging>
1313

1414
<name>Titanium JSON-LD 1.1</name>
15-
15+
1616
<description>
1717
A JSON-LD 1.1 Processor &amp; API
1818
</description>
19-
19+
2020
<licenses>
2121
<license>
2222
<name>Apache License, Version 2.0</name>
2323
<url>http://apache.org/licenses/LICENSE-2.0</url>
2424
</license>
25-
</licenses>
26-
25+
</licenses>
26+
2727
<issueManagement>
2828
<system>github</system>
2929
<url>https://github.com/filip26/titanium-json-ld/issues</url>
3030
</issueManagement>
31-
31+
3232
<developers>
3333
<developer>
3434
<id>filip26</id>
@@ -37,20 +37,21 @@
3737
<roles><role>author</role></roles>
3838
</developer>
3939
</developers>
40-
40+
4141
<inceptionYear>2020</inceptionYear>
42-
42+
4343
<properties>
4444
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4545
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
46-
47-
<jakarta.json.version>2.0.1</jakarta.json.version>
46+
47+
<jakarta.json.version>2.0.1</jakarta.json.version>
4848

4949
<argLine>-Dfile.encoding=UTF-8</argLine>
5050

5151
<!-- test resources -->
5252
<junit.version>5.10.0</junit.version>
5353
<wiremock.version>2.35.1</wiremock.version>
54+
<jmh.version>1.36</jmh.version>
5455

5556
<sonar.projectKey>filip26_titanium-json-ld</sonar.projectKey>
5657
<sonar.organization>apicatalog</sonar.organization>
@@ -75,15 +76,27 @@
7576
<artifactId>junit-jupiter</artifactId>
7677
<version>${junit.version}</version>
7778
<scope>test</scope>
78-
</dependency>
79+
</dependency>
7980
<dependency>
8081
<groupId>com.github.tomakehurst</groupId>
8182
<artifactId>wiremock-jre8</artifactId>
8283
<version>${wiremock.version}</version>
8384
<scope>test</scope>
8485
</dependency>
86+
<dependency>
87+
<groupId>org.openjdk.jmh</groupId>
88+
<artifactId>jmh-core</artifactId>
89+
<version>${jmh.version}</version>
90+
<scope>test</scope>
91+
</dependency>
92+
<dependency>
93+
<groupId>org.openjdk.jmh</groupId>
94+
<artifactId>jmh-generator-annprocess</artifactId>
95+
<version>${jmh.version}</version>
96+
<scope>test</scope>
97+
</dependency>
8598
</dependencies>
86-
<build>
99+
<build>
87100
<plugins>
88101
<plugin>
89102
<artifactId>maven-jar-plugin</artifactId>
@@ -109,7 +122,7 @@
109122
</configuration>
110123
</execution>
111124
</executions>
112-
</plugin>
125+
</plugin>
113126
<plugin>
114127
<groupId>org.apache.maven.plugins</groupId>
115128
<artifactId>maven-source-plugin</artifactId>
@@ -122,7 +135,7 @@
122135
</goals>
123136
</execution>
124137
</executions>
125-
</plugin>
138+
</plugin>
126139
<plugin>
127140
<groupId>org.apache.maven.plugins</groupId>
128141
<artifactId>maven-javadoc-plugin</artifactId>
@@ -135,7 +148,7 @@
135148
</goals>
136149
</execution>
137150
</executions>
138-
</plugin>
151+
</plugin>
139152
<plugin>
140153
<groupId>org.jacoco</groupId>
141154
<artifactId>jacoco-maven-plugin</artifactId>
@@ -155,7 +168,7 @@
155168
</goals>
156169
</execution>
157170
</executions>
158-
</plugin>
171+
</plugin>
159172
<plugin>
160173
<groupId>org.codehaus.mojo</groupId>
161174
<artifactId>flatten-maven-plugin</artifactId>
@@ -189,7 +202,7 @@
189202
<plugin>
190203
<artifactId>maven-failsafe-plugin</artifactId>
191204
<version>3.1.2</version>
192-
</plugin>
205+
</plugin>
193206
</plugins>
194207
</build>
195208
<profiles>
@@ -199,7 +212,7 @@
199212
<activeByDefault>false</activeByDefault>
200213
</activation>
201214
<build>
202-
<plugins>
215+
<plugins>
203216
<plugin>
204217
<groupId>org.apache.maven.plugins</groupId>
205218
<artifactId>maven-gpg-plugin</artifactId>
@@ -213,7 +226,7 @@
213226
</goals>
214227
</execution>
215228
</executions>
216-
</plugin>
229+
</plugin>
217230
<plugin>
218231
<groupId>org.sonatype.plugins</groupId>
219232
<artifactId>nexus-staging-maven-plugin</artifactId>
@@ -224,15 +237,15 @@
224237
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
225238
<autoReleaseAfterClose>true</autoReleaseAfterClose>
226239
</configuration>
227-
</plugin>
240+
</plugin>
228241
</plugins>
229242
</build>
230243
<distributionManagement>
231244
<snapshotRepository>
232245
<id>ossrh</id>
233246
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
234247
</snapshotRepository>
235-
</distributionManagement>
236-
</profile>
248+
</distributionManagement>
249+
</profile>
237250
</profiles>
238251
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.apicatalog.jsonld.benchmark;
2+
3+
import com.apicatalog.jsonld.JsonLd;
4+
import com.apicatalog.jsonld.JsonLdError;
5+
import com.apicatalog.jsonld.api.ExpansionApi;
6+
import com.apicatalog.jsonld.api.FlatteningApi;
7+
import com.apicatalog.jsonld.document.Document;
8+
import com.apicatalog.jsonld.document.JsonDocument;
9+
import com.apicatalog.jsonld.loader.DocumentLoaderOptions;
10+
import com.apicatalog.jsonld.loader.FileLoader;
11+
import jakarta.json.JsonArray;
12+
import jakarta.json.JsonObject;
13+
import jakarta.json.JsonStructure;
14+
import jakarta.json.JsonValue;
15+
import org.openjdk.jmh.annotations.Benchmark;
16+
import org.openjdk.jmh.annotations.BenchmarkMode;
17+
import org.openjdk.jmh.annotations.Fork;
18+
import org.openjdk.jmh.annotations.Level;
19+
import org.openjdk.jmh.annotations.Measurement;
20+
import org.openjdk.jmh.annotations.Mode;
21+
import org.openjdk.jmh.annotations.OutputTimeUnit;
22+
import org.openjdk.jmh.annotations.Scope;
23+
import org.openjdk.jmh.annotations.Setup;
24+
import org.openjdk.jmh.annotations.State;
25+
import org.openjdk.jmh.annotations.Warmup;
26+
import org.openjdk.jmh.runner.Runner;
27+
import org.openjdk.jmh.runner.RunnerException;
28+
import org.openjdk.jmh.runner.options.Options;
29+
import org.openjdk.jmh.runner.options.OptionsBuilder;
30+
31+
import java.net.URISyntaxException;
32+
import java.net.URL;
33+
import java.util.concurrent.TimeUnit;
34+
35+
/**
36+
* This benchmark is used to compare the performance of the main processing algorithms of JSON-LD.
37+
*/
38+
@State(Scope.Benchmark)
39+
@Warmup(iterations = 5)
40+
@BenchmarkMode({Mode.AverageTime})
41+
@Fork(value = 1, jvmArgs = {"-Xmx2048M", "-Xms2048M"})
42+
@Measurement(iterations = 5)
43+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
44+
public class BasicProcessingAlgorithmsBenchmark {
45+
46+
private Document datagovbeDcat;
47+
private Document datagovbeDcatContext;
48+
private Document datagovbeDcatCompact;
49+
private Document datagovbeDcatFlatten;
50+
51+
@Setup(Level.Invocation)
52+
public void setUp() throws URISyntaxException, JsonLdError {
53+
datagovbeDcat = loadDocument("benchmark/datagovbe/dcat.jsonld");
54+
datagovbeDcatCompact = loadDocument("benchmark/datagovbe/dcat-compact.jsonld");
55+
datagovbeDcatFlatten = loadDocument("benchmark/datagovbe/dcat-flatten.jsonld");
56+
datagovbeDcatContext = loadDocument("benchmark/datagovbe/context/context.jsonld");
57+
}
58+
59+
public static void main(String[] args) throws RunnerException {
60+
61+
// The classe(s) that are included may not get compiled by your IDE.
62+
// Run `mvn clean verify -DskipTests` before running the benchmarks.
63+
64+
Options opt = new OptionsBuilder()
65+
.include(BasicProcessingAlgorithmsBenchmark.class.getName()+".*")
66+
.build();
67+
68+
new Runner(opt).run();
69+
}
70+
71+
@Benchmark
72+
public JsonObject compactDatagovbeDcat() throws JsonLdError {
73+
return JsonLd.compact(datagovbeDcat, datagovbeDcatContext).get();
74+
}
75+
76+
@Benchmark
77+
public JsonObject compactDatagovbeDcatEmptyContext() throws JsonLdError {
78+
return JsonLd.compact(datagovbeDcat, JsonDocument.of(JsonValue.EMPTY_JSON_OBJECT)).get();
79+
}
80+
81+
@Benchmark
82+
public JsonArray expandDatagovbeDcatFromCompact() throws JsonLdError {
83+
return new ExpansionApi(datagovbeDcatCompact).context(datagovbeDcatContext.getJsonContent().get()).get();
84+
}
85+
86+
@Benchmark
87+
public JsonArray expandDatagovbeDcatFromFlatten() throws JsonLdError {
88+
return JsonLd.expand(datagovbeDcatFlatten).get();
89+
}
90+
91+
@Benchmark
92+
public JsonStructure flattenDatagovbeDcat() throws JsonLdError {
93+
return JsonLd.flatten(datagovbeDcat).get();
94+
}
95+
96+
@Benchmark
97+
public JsonStructure flattenDatagovbeDcatFromCompact() throws JsonLdError {
98+
return new FlatteningApi(datagovbeDcatCompact).context(datagovbeDcatContext.getJsonContent().get()).get();
99+
}
100+
101+
private Document loadDocument(String name) throws JsonLdError, URISyntaxException {
102+
URL fileUrl = getClass().getClassLoader().getResource(name);
103+
Document document = (new FileLoader()).loadDocument(fileUrl.toURI(), new DocumentLoaderOptions());
104+
return document;
105+
}
106+
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.apicatalog.jsonld.benchmark;
2+
3+
import com.apicatalog.jsonld.JsonLdError;
4+
import com.apicatalog.jsonld.api.ToRdfApi;
5+
import com.apicatalog.jsonld.document.Document;
6+
import com.apicatalog.jsonld.loader.DocumentLoaderOptions;
7+
import com.apicatalog.jsonld.loader.FileLoader;
8+
import com.apicatalog.rdf.RdfDataset;
9+
import org.openjdk.jmh.annotations.Benchmark;
10+
import org.openjdk.jmh.annotations.BenchmarkMode;
11+
import org.openjdk.jmh.annotations.Fork;
12+
import org.openjdk.jmh.annotations.Level;
13+
import org.openjdk.jmh.annotations.Measurement;
14+
import org.openjdk.jmh.annotations.Mode;
15+
import org.openjdk.jmh.annotations.OutputTimeUnit;
16+
import org.openjdk.jmh.annotations.Scope;
17+
import org.openjdk.jmh.annotations.Setup;
18+
import org.openjdk.jmh.annotations.State;
19+
import org.openjdk.jmh.annotations.Warmup;
20+
import org.openjdk.jmh.runner.Runner;
21+
import org.openjdk.jmh.runner.RunnerException;
22+
import org.openjdk.jmh.runner.options.Options;
23+
import org.openjdk.jmh.runner.options.OptionsBuilder;
24+
25+
import java.net.URISyntaxException;
26+
import java.net.URL;
27+
import java.util.concurrent.TimeUnit;
28+
29+
@State(Scope.Benchmark)
30+
@Warmup(iterations = 0)
31+
@BenchmarkMode({Mode.AverageTime})
32+
@Fork(value = 1, jvmArgs = {"-Xms16G", "-Xmx16G", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseEpsilonGC",
33+
"-XX:+AlwaysPreTouch"})
34+
@Measurement(iterations = 99999999, time = 1, timeUnit = TimeUnit.MILLISECONDS)
35+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
36+
/*
37+
* This benchmark is used to test how many iterations we can run before running out of memory.
38+
*/
39+
public class OOMBenchmark {
40+
41+
private Document datagovbeDcat;
42+
43+
@Setup(Level.Invocation)
44+
public void setUp() throws URISyntaxException, JsonLdError {
45+
datagovbeDcat = null;
46+
URL fileUrl = getClass().getClassLoader().getResource("benchmark/datagovbe/dcat.jsonld");
47+
datagovbeDcat = (new FileLoader()).loadDocument(fileUrl.toURI(), new DocumentLoaderOptions());
48+
}
49+
50+
public static void main(String[] args) throws RunnerException {
51+
52+
// The classe(s) that are included may not get compiled by your IDE.
53+
// Run `mvn clean verify -DskipTests` before running the benchmarks.
54+
55+
Options opt = new OptionsBuilder()
56+
.include(OOMBenchmark.class.getName()+".*")
57+
.build();
58+
59+
new Runner(opt).run();
60+
}
61+
62+
@Benchmark
63+
public int datagovbeDcatToRdf() throws JsonLdError {
64+
RdfDataset rdfDataset = new ToRdfApi(datagovbeDcat).get();
65+
return rdfDataset.size();
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.apicatalog.jsonld.benchmark;
2+
3+
import org.openjdk.jmh.runner.Runner;
4+
import org.openjdk.jmh.runner.RunnerException;
5+
import org.openjdk.jmh.runner.options.Options;
6+
import org.openjdk.jmh.runner.options.OptionsBuilder;
7+
8+
public class RunAllBenchmarks {
9+
10+
public static void main(String[] args) throws RunnerException {
11+
12+
// The classe(s) that are included may not get compiled by your IDE.
13+
// Run `mvn clean verify -DskipTests` before running the benchmarks.
14+
15+
Options opt = new OptionsBuilder()
16+
.include("com.apicatalog.jsonld.benchmark.*.*")
17+
.build();
18+
19+
new Runner(opt).run();
20+
}
21+
}

0 commit comments

Comments
 (0)