Skip to content

Commit

Permalink
Merge pull request #179 from sebastian-toepfer/remove_static_json_calls
Browse files Browse the repository at this point in the history
remove calls to static method from Json
  • Loading branch information
sebastian-toepfer authored Dec 20, 2023
2 parents 41e09bc + 08cd849 commit ae8b241
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 35 deletions.
120 changes: 120 additions & 0 deletions benchmark/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.github.sebastian-toepfer.ddd</groupId>
<artifactId>domain-driven-desgin</artifactId>
<version>0.6.0-SNAPSHOT</version>
</parent>

<artifactId>media-benchmark</artifactId>
<name>Domain Driven Desgin :: benchmark</name>

<properties>
<sonar.skip>true</sonar.skip>
</properties>

<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>media-json-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.parsson</groupId>
<artifactId>parsson</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths combine.children="append">
<path>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
<executions>
<execution>
<id>default-testCompile</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>

<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>benchmarks</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jmh.Main</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
<filters>
<filter>
<!--
Shading signed JARs will fail without this.
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
-->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.ddd.benchmark;

import io.github.sebastiantoepfer.ddd.media.json.JsonObjectMedia;
import jakarta.json.spi.JsonProvider;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Warmup;

@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 7, timeUnit = TimeUnit.SECONDS)
@BenchmarkMode(Mode.AverageTime)
public class JsonObjectMediaBenchmarks {

private static final JsonProvider JSONP = JsonProvider.provider();

@Benchmark
public void printAsJsonObject() {
var unused = IntStream
.range(1, 100)
.boxed()
.reduce(
new JsonObjectMedia(JSONP),
(JsonObjectMedia media, Integer value) -> media.withValue(String.valueOf(value), value),
(left, right) -> null
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import io.github.sebastiantoepfer.ddd.common.Printable;
import io.github.sebastiantoepfer.ddd.media.core.BaseMedia;
import io.github.sebastiantoepfer.ddd.media.json.util.CollectionToJsonValueMapper;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonNumber;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import jakarta.json.spi.JsonProvider;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.AbstractMap;
Expand All @@ -18,49 +18,56 @@

public class JsonObjectMedia extends AbstractMap<String, JsonValue> implements BaseMedia<JsonObjectMedia>, JsonObject {

private static final JsonProvider JSONP = JsonProvider.provider();
private final JsonProvider jsonProvider;
private final JsonObject json;

public JsonObjectMedia() {
this(Json.createObjectBuilder());
this(JSONP);
}

private JsonObjectMedia(final JsonObjectBuilder builder) {
public JsonObjectMedia(final JsonProvider jsonProvider) {
this(jsonProvider, jsonProvider.createObjectBuilder());
}

private JsonObjectMedia(final JsonProvider jsonProvider, final JsonObjectBuilder builder) {
this.jsonProvider = jsonProvider;
this.json = builder.build();
}

@Override
public JsonObjectMedia withValue(final String name, final String value) {
return new JsonObjectMedia(Json.createObjectBuilder(json).add(name, value));
return new JsonObjectMedia(jsonProvider, asJsonBuilder().add(name, value));
}

@Override
public JsonObjectMedia withValue(final String name, final int value) {
return new JsonObjectMedia(Json.createObjectBuilder(json).add(name, value));
return new JsonObjectMedia(jsonProvider, asJsonBuilder().add(name, value));
}

@Override
public JsonObjectMedia withValue(final String name, final long value) {
return new JsonObjectMedia(Json.createObjectBuilder(json).add(name, value));
return new JsonObjectMedia(jsonProvider, asJsonBuilder().add(name, value));
}

@Override
public JsonObjectMedia withValue(final String name, final double value) {
return new JsonObjectMedia(Json.createObjectBuilder(json).add(name, value));
return new JsonObjectMedia(jsonProvider, asJsonBuilder().add(name, value));
}

@Override
public JsonObjectMedia withValue(final String name, final BigDecimal value) {
return new JsonObjectMedia(Json.createObjectBuilder(json).add(name, value));
return new JsonObjectMedia(jsonProvider, asJsonBuilder().add(name, value));
}

@Override
public JsonObjectMedia withValue(final String name, final BigInteger value) {
return new JsonObjectMedia(Json.createObjectBuilder(json).add(name, value));
return new JsonObjectMedia(jsonProvider, asJsonBuilder().add(name, value));
}

@Override
public JsonObjectMedia withValue(final String name, final boolean value) {
return new JsonObjectMedia(Json.createObjectBuilder(json).add(name, value));
return new JsonObjectMedia(jsonProvider, asJsonBuilder().add(name, value));
}

@Override
Expand All @@ -71,20 +78,25 @@ public JsonObjectMedia withValue(final String name, final Printable value) {
@Override
public JsonObjectMedia withValue(final String name, final Collection<?> values) {
return new JsonObjectMedia(
Json.createObjectBuilder(json).add(name, new CollectionToJsonValueMapper(values).asJsonValue())
jsonProvider,
asJsonBuilder().add(name, new CollectionToJsonValueMapper(jsonProvider, values).asJsonValue())
);
}

@Override
public JsonObjectMedia withValue(final String name, final JsonObjectMedia value) {
return new JsonObjectMedia(Json.createObjectBuilder(json).add(name, value));
return new JsonObjectMedia(jsonProvider, asJsonBuilder().add(name, value));
}

@Override
public String toString() {
return json.toString();
}

private JsonObjectBuilder asJsonBuilder() {
return jsonProvider.createObjectBuilder(json);
}

@Override
public JsonArray getJsonArray(final String string) {
return json.getJsonArray(string);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package io.github.sebastiantoepfer.ddd.media.json.stream;

import io.github.sebastiantoepfer.ddd.common.Printable;
import jakarta.json.Json;
import jakarta.json.spi.JsonProvider;
import jakarta.json.stream.JsonGenerator;
import java.io.Flushable;
import java.io.IOException;
Expand All @@ -34,6 +34,8 @@

public final class JsonArrayStreamMediaPrintableAdapter implements Flushable, AutoCloseable {

private static final JsonProvider JSONP = JsonProvider.provider();
private final JsonProvider jsonProvider;
private final JsonGenerator generator;
private final Function<JsonObjectStreamMedia, TerminableMedia<?>> mediaDecorator;

Expand All @@ -45,13 +47,23 @@ public JsonArrayStreamMediaPrintableAdapter(
final OutputStream os,
final Function<JsonObjectStreamMedia, TerminableMedia<?>> mediaDecorator
) {
this(Json.createGenerator(os), mediaDecorator);
this(JSONP, os, mediaDecorator);
}

public JsonArrayStreamMediaPrintableAdapter(
final JsonProvider provider,
final OutputStream os,
final Function<JsonObjectStreamMedia, TerminableMedia<?>> mediaDecorator
) {
this(provider, provider.createGenerator(os), mediaDecorator);
}

private JsonArrayStreamMediaPrintableAdapter(
final JsonProvider jsonProvider,
final JsonGenerator generator,
final Function<JsonObjectStreamMedia, TerminableMedia<?>> mediaDecorator
) {
this.jsonProvider = Objects.requireNonNull(jsonProvider);
this.generator = Objects.requireNonNull(generator).writeStartArray();
this.mediaDecorator = Objects.requireNonNull(mediaDecorator);
}
Expand All @@ -73,7 +85,9 @@ public JsonArrayStreamMediaPrintableAdapter end() {
}

public JsonArrayStreamMediaPrintableAdapter print(final Printable printable) {
printable.printOn((TerminableMedia) mediaDecorator.apply(new JsonObjectStreamMedia(generator))).end();
printable
.printOn((TerminableMedia) mediaDecorator.apply(new JsonObjectStreamMedia(jsonProvider, generator)))
.end();
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import io.github.sebastiantoepfer.ddd.common.Printable;
import io.github.sebastiantoepfer.ddd.media.json.util.ObjectToJsonValueMapper;
import io.github.sebastiantoepfer.ddd.media.json.util.ToJsonValueMapper;
import jakarta.json.Json;
import jakarta.json.JsonValue;
import jakarta.json.spi.JsonProvider;
import jakarta.json.stream.JsonGenerator;
import java.io.Flushable;
import java.io.IOException;
Expand All @@ -41,13 +41,15 @@

public final class JsonObjectStreamMedia implements TerminableMedia<JsonObjectStreamMedia>, AutoCloseable, Flushable {

private final JsonProvider provider;
private final JsonGenerator generator;

public JsonObjectStreamMedia(final OutputStream os) {
this(Json.createGenerator(os));
public JsonObjectStreamMedia(final JsonProvider provider, final OutputStream os) {
this(provider, provider.createGenerator(os));
}

JsonObjectStreamMedia(final JsonGenerator generator) {
JsonObjectStreamMedia(final JsonProvider provider, final JsonGenerator generator) {
this.provider = Objects.requireNonNull(provider);
this.generator = Objects.requireNonNull(generator).writeStartObject();
}

Expand Down Expand Up @@ -123,7 +125,7 @@ public JsonObjectStreamMedia withValue(final String name, final Collection<?> va
generator.writeStartArray(name);
values
.stream()
.map(ObjectToJsonValueMapper::new)
.map(o -> new ObjectToJsonValueMapper(provider, o))
.map(ToJsonValueMapper::asJsonValue)
.filter(not(value -> value.getValueType() == JsonValue.ValueType.NULL))
.forEach(generator::write);
Expand Down
Loading

0 comments on commit ae8b241

Please sign in to comment.