Skip to content

Commit

Permalink
GH-273 add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
hmottestad committed Aug 15, 2023
1 parent f0a54f6 commit 5c53ad2
Show file tree
Hide file tree
Showing 31 changed files with 1,837,852 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pom_parent.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<!-- test resources -->
<junit.version>5.10.0</junit.version>
<wiremock.version>2.35.0</wiremock.version>
<jmh.version>1.36</jmh.version>

<sonar.projectKey>filip26_titanium-json-ld</sonar.projectKey>
<sonar.organization>apicatalog</sonar.organization>
Expand Down Expand Up @@ -82,6 +83,18 @@
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.apicatalog.jsonld.benchmark;

import com.apicatalog.jsonld.JsonLd;
import com.apicatalog.jsonld.JsonLdError;
import com.apicatalog.jsonld.api.ExpansionApi;
import com.apicatalog.jsonld.api.FlatteningApi;
import com.apicatalog.jsonld.document.Document;
import com.apicatalog.jsonld.document.JsonDocument;
import com.apicatalog.jsonld.loader.DocumentLoaderOptions;
import com.apicatalog.jsonld.loader.FileLoader;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonStructure;
import jakarta.json.JsonValue;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
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.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
* This benchmark is used to compare the performance of the main processing algorithms of JSON-LD.
*/
@State(Scope.Benchmark)
@Warmup(iterations = 5)
@BenchmarkMode({Mode.AverageTime})
@Fork(value = 1, jvmArgs = {"-Xmx2048M", "-Xms2048M"})
@Measurement(iterations = 5)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class BasicProcessingAlgorithmsBenchmark {

private Document datagovbeDcat;
private Document datagovbeDcatContext;
private Document datagovbeDcatCompact;
private Document datagovbeDcatFlatten;

@Setup(Level.Invocation)
public void setUp() throws URISyntaxException, JsonLdError {
datagovbeDcat = loadDocument("benchmark/datagovbe/dcat.jsonld");
datagovbeDcatCompact = loadDocument("benchmark/datagovbe/dcat-compact.jsonld");
datagovbeDcatFlatten = loadDocument("benchmark/datagovbe/dcat-flatten.jsonld");
datagovbeDcatContext = loadDocument("benchmark/datagovbe/context/context.jsonld");
}

public static void main(String[] args) throws RunnerException {

// The classe(s) that are included may not get compiled by your IDE.
// Run `mvn clean verify -DskipTests` before running the benchmarks.

Options opt = new OptionsBuilder()
.include(BasicProcessingAlgorithmsBenchmark.class.getName()+".*")
.build();

new Runner(opt).run();
}

@Benchmark
public JsonObject compactDatagovbeDcat() throws JsonLdError {
return JsonLd.compact(datagovbeDcat, datagovbeDcatContext).get();
}

@Benchmark
public JsonObject compactDatagovbeDcatEmptyContext() throws JsonLdError {
return JsonLd.compact(datagovbeDcat, JsonDocument.of(JsonValue.EMPTY_JSON_OBJECT)).get();
}

@Benchmark
public JsonArray expandDatagovbeDcatFromCompact() throws JsonLdError {
return new ExpansionApi(datagovbeDcatCompact).context(datagovbeDcatContext.getJsonContent().get()).get();
}

@Benchmark
public JsonArray expandDatagovbeDcatFromFlatten() throws JsonLdError {
return JsonLd.expand(datagovbeDcatFlatten).get();
}

@Benchmark
public JsonStructure flattenDatagovbeDcat() throws JsonLdError {
return JsonLd.flatten(datagovbeDcat).get();
}

@Benchmark
public JsonStructure flattenDatagovbeDcatFromCompact() throws JsonLdError {
return new FlatteningApi(datagovbeDcatCompact).context(datagovbeDcatContext.getJsonContent().get()).get();
}

private Document loadDocument(String name) throws JsonLdError, URISyntaxException {
URL fileUrl = getClass().getClassLoader().getResource(name);
Document document = (new FileLoader()).loadDocument(fileUrl.toURI(), new DocumentLoaderOptions());
return document;
}

}
68 changes: 68 additions & 0 deletions src/test/java/com/apicatalog/jsonld/benchmark/OOMBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.apicatalog.jsonld.benchmark;

import com.apicatalog.jsonld.JsonLdError;
import com.apicatalog.jsonld.api.ToRdfApi;
import com.apicatalog.jsonld.document.Document;
import com.apicatalog.jsonld.loader.DocumentLoaderOptions;
import com.apicatalog.jsonld.loader.FileLoader;
import com.apicatalog.rdf.RdfDataset;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
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.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
@Warmup(iterations = 0)
@BenchmarkMode({Mode.AverageTime})
@Fork(value = 1, jvmArgs = {"-Xms16G", "-Xmx16G", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseEpsilonGC",
"-XX:+AlwaysPreTouch"})
@Measurement(iterations = 99999999, time = 1, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
/*
* This benchmark is used to test how many iterations we can run before running out of memory.
*/
public class OOMBenchmark {

private Document datagovbeDcat;

@Setup(Level.Invocation)
public void setUp() throws URISyntaxException, JsonLdError {
datagovbeDcat = null;
URL fileUrl = getClass().getClassLoader().getResource("benchmark/datagovbe/dcat.jsonld");
datagovbeDcat = (new FileLoader()).loadDocument(fileUrl.toURI(), new DocumentLoaderOptions());
}

public static void main(String[] args) throws RunnerException {

// The classe(s) that are included may not get compiled by your IDE.
// Run `mvn clean verify -DskipTests` before running the benchmarks.

Options opt = new OptionsBuilder()
.include(OOMBenchmark.class.getName()+".*")
.build();

new Runner(opt).run();
}

@Benchmark
public int datagovbeDcatToRdf() throws JsonLdError {
RdfDataset rdfDataset = new ToRdfApi(datagovbeDcat).get();
return rdfDataset.size();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.apicatalog.jsonld.benchmark;

import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

public class RunAllBenchmarks {

public static void main(String[] args) throws RunnerException {

// The classe(s) that are included may not get compiled by your IDE.
// Run `mvn clean verify -DskipTests` before running the benchmarks.

Options opt = new OptionsBuilder()
.include("com.apicatalog.jsonld.benchmark.*.*")
.build();

new Runner(opt).run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.apicatalog.jsonld.benchmark;

import com.apicatalog.jsonld.JsonLdError;
import com.apicatalog.jsonld.api.ToRdfApi;
import com.apicatalog.jsonld.document.Document;
import com.apicatalog.jsonld.loader.DocumentLoaderOptions;
import com.apicatalog.jsonld.loader.FileLoader;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
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.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
@Warmup(iterations = 5)
@BenchmarkMode({Mode.AverageTime})
@Fork(value = 1, jvmArgs = {"-Xmx1024M", "-Xms1024M"})
@Measurement(iterations = 5)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class ToRdfLargeFilesBenchmark {

private Document datagovbeDcat;

@Setup(Level.Invocation)
public void setUp() throws URISyntaxException, JsonLdError {
URL fileUrl = getClass().getClassLoader().getResource("benchmark/datagovbe/dcat.jsonld");

datagovbeDcat = (new FileLoader()).loadDocument(fileUrl.toURI(), new DocumentLoaderOptions());
}

public static void main(String[] args) throws RunnerException {

// The classe(s) that are included may not get compiled by your IDE.
// Run `mvn clean verify -DskipTests` before running the benchmarks.

Options opt = new OptionsBuilder()
.include(ToRdfLargeFilesBenchmark.class.getName()+".*")
.build();

new Runner(opt).run();
}

@Benchmark
public Object datagovbeDcat() throws JsonLdError {
return new ToRdfApi(datagovbeDcat).get();
}

}
Loading

0 comments on commit 5c53ad2

Please sign in to comment.