-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(json): Improve structure performance
- Loading branch information
1 parent
1776ab7
commit 7c3d6e2
Showing
3 changed files
with
129 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
plugins { | ||
id("me.champeau.jmh") | ||
} | ||
|
||
apply(from = "$rootDir/gradle/java.gradle") | ||
|
||
jmh { | ||
version = "1.28" | ||
} |
106 changes: 106 additions & 0 deletions
106
components/json/src/jmh/java/datadog/json/JsonWriterBenchmark.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,106 @@ | ||
package datadog.json; | ||
|
||
import static java.util.concurrent.TimeUnit.MICROSECONDS; | ||
import static org.openjdk.jmh.annotations.Mode.AverageTime; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
@BenchmarkMode(AverageTime) | ||
@OutputTimeUnit(MICROSECONDS) | ||
@Fork(value = 1) | ||
@SuppressWarnings("unused") | ||
public class JsonWriterBenchmark { | ||
@Benchmark | ||
public void writeSimpleArray(Blackhole blackhole) { | ||
try (JsonWriter writer = new JsonWriter()) { | ||
writer | ||
.beginArray() | ||
.beginObject() | ||
.name("true") | ||
.value(true) | ||
.endObject() | ||
.beginObject() | ||
.name("false") | ||
.value(false) | ||
.endObject() | ||
.endArray(); | ||
blackhole.consume(writer.toString()); | ||
} | ||
} | ||
|
||
@Benchmark | ||
public void writeComplexArray(Blackhole blackhole) { | ||
try (JsonWriter writer = new JsonWriter()) { | ||
writer | ||
.beginArray() | ||
.value("first level") | ||
.beginArray() | ||
.value("second level") | ||
.beginArray() | ||
.value("third level") | ||
.beginObject() | ||
.name("key") | ||
.value("value") | ||
.endObject() | ||
.beginObject() | ||
.name("key") | ||
.value("value") | ||
.endObject() | ||
.endArray() // second level | ||
.beginObject() | ||
.name("key") | ||
.value("value") | ||
.endObject() | ||
.endArray() // first level | ||
.beginObject() | ||
.name("key") | ||
.value("value") | ||
.endObject() | ||
.value("last value") | ||
.endArray(); | ||
blackhole.consume(writer.toString()); | ||
} | ||
} | ||
|
||
@Benchmark | ||
public void writeComplexObject(Blackhole blackhole) { | ||
try (JsonWriter writer = new JsonWriter()) { | ||
writer | ||
.beginObject() | ||
.name("attrs") | ||
.beginObject() | ||
.name("attr1") | ||
.value("value1") | ||
.name("attr2") | ||
.value("value2") | ||
.endObject() | ||
.name("data") | ||
.beginArray() | ||
.beginObject() | ||
.name("x") | ||
.value(1) | ||
.name("y") | ||
.value(12.3) | ||
.endObject() | ||
.beginObject() | ||
.name("x") | ||
.value(2) | ||
.name("y") | ||
.value(4.56) | ||
.endObject() | ||
.beginObject() | ||
.name("x") | ||
.value(3) | ||
.name("y") | ||
.value(789) | ||
.endObject() | ||
.endArray() | ||
.endObject(); | ||
blackhole.consume(writer.toString()); | ||
} | ||
} | ||
} |
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