Skip to content

Commit

Permalink
add benchmark for issue #609
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 6, 2022
1 parent b21fe4a commit 7ff203d
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
Expand Down Expand Up @@ -37,9 +38,10 @@ public class Issue609 {
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(Issue609.class.getName())
.warmupIterations(5)
.warmupIterations(3)
.measurementIterations(5)
.forks(1)
.jvmArgsAppend("-Xms128m", "-Xmx128m")
.build();

new Runner(opt).run();
Expand All @@ -48,129 +50,147 @@ public static void main(String[] args) throws RunnerException {
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void fastJSON1ObjSeThroughput() {
public void fastJSON1ObjSeThroughput(Blackhole bh) {
for (Student student : objList) {
JSON.toJSONString(student);
bh.consume(JSON.toJSONString(student));
}
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void fastJSON1ObjDeThroughput() {
public void fastJSON1ObjDeThroughput(Blackhole bh) {
for (String student : strList) {
JSON.parseObject(student, Student.class);
bh.consume(JSON.parseObject(student, Student.class));
}
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void fastJSON2ObjSeThroughput() {
public void fastJSON2ObjSeThroughput(Blackhole bh) {
for (Student student : objList) {
com.alibaba.fastjson2.JSON.toJSONString(student);
bh.consume(
com.alibaba.fastjson2.JSON.toJSONString(student)
);
}
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void fastJSON2ObjDeThroughput() {
public void fastJSON2ObjDeThroughput(Blackhole bh) {
for (String student : strList) {
com.alibaba.fastjson2.JSON.parseObject(student, Student.class);
bh.consume(com.alibaba.fastjson2.JSON.parseObject(student, Student.class));
}
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void fastJSON1ArraySeThroughput() {
JSON.toJSONString(objList);
public void fastJSON1ArraySeThroughput(Blackhole bh) {
bh.consume(
JSON.toJSONString(objList)
);
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void fastJSON1ArrayDeThroughput() {
JSON.parseArray(source, Student.class);
public void fastJSON1ArrayDeThroughput(Blackhole bh) {
bh.consume(
JSON.parseArray(source, Student.class)
);
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void fastJSON2ArraySeThroughput() {
com.alibaba.fastjson2.JSON.toJSONString(objList, JSONWriter.Feature.ReferenceDetection);
public void fastJSON2ArraySeThroughput(Blackhole bh) {
bh.consume(
com.alibaba.fastjson2.JSON.toJSONString(objList, JSONWriter.Feature.ReferenceDetection)
);
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void fastJSON2ArrayDeThroughput() {
com.alibaba.fastjson2.JSON.parseArray(source, Student.class);
public void fastJSON2ArrayDeThroughput(Blackhole bh) {
bh.consume(
com.alibaba.fastjson2.JSON.parseArray(source, Student.class)
);
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void fastJSON1ObjSeTime() {
public void fastJSON1ObjSeTime(Blackhole bh) {
for (Student student : objList) {
JSON.toJSONString(student);
bh.consume(
JSON.toJSONString(student)
);
}
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void fastJSON1ObjDeTime() {
public void fastJSON1ObjDeTime(Blackhole bh) {
for (String student : strList) {
JSON.parseObject(student, Student.class);
bh.consume(
JSON.parseObject(student, Student.class)
);
}
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void fastJSON2ObjSeTime() {
public void fastJSON2ObjSeTime(Blackhole bh) {
for (Student student : objList) {
com.alibaba.fastjson2.JSON.toJSONString(student);
bh.consume(
com.alibaba.fastjson2.JSON.toJSONString(student)
);
}
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void fastJSON2ObjDeTime() {
public void fastJSON2ObjDeTime(Blackhole bh) {
for (String student : strList) {
com.alibaba.fastjson2.JSON.parseObject(student, Student.class);
bh.consume(com.alibaba.fastjson2.JSON.parseObject(student, Student.class));
}
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void fastJSON1ArraySeTime() {
JSON.toJSONString(objList);
public void fastJSON1ArraySeTime(Blackhole bh) {
bh.consume(JSON.toJSONString(objList));
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void fastJSON1ArrayDeTime() {
JSON.parseArray(source, Student.class);
public void fastJSON1ArrayDeTime(Blackhole bh) {
bh.consume(
JSON.parseArray(source, Student.class)
);
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void fastJSON2ArraySeTime() {
com.alibaba.fastjson2.JSON.toJSONString(objList, JSONWriter.Feature.ReferenceDetection);
public void fastJSON2ArraySeTime(Blackhole bh) {
bh.consume(com.alibaba.fastjson2.JSON.toJSONString(objList, JSONWriter.Feature.ReferenceDetection));
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void fastJSON2ArrayDeTime() {
com.alibaba.fastjson2.JSON.parseArray(source, Student.class);
public void fastJSON2ArrayDeTime(Blackhole bh) {
bh.consume(com.alibaba.fastjson2.JSON.parseArray(source, Student.class));
}

private static class Student {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.alibaba.fastjson2.benchmark;

import static com.alibaba.fastjson2.benchmark.JMH.BH;

public class Issue609Test {
public static void fastJSON1ArrayDeTime_test() throws Exception {
for (int i = 0; i < 10; i++) {
fastJSON1ArrayDeTime();
}
}

public static void fastJSON1ArrayDeTime() throws Exception {
Issue609 benchmark = new Issue609();

long start = System.currentTimeMillis();
for (int i = 0; i < 100; ++i) {
benchmark.fastJSON1ArrayDeTime(BH);
}
long millis = System.currentTimeMillis() - start;
System.out.println("fastJSON1ArrayDeTime : " + millis);
// zulu8.62.0.19 : 2365
// zulu11.52.13 :
// zulu17.32.13 :
// zulu18.28.13 :
// zulu19.0.47 :
// corretto-8 :
// corretto-11 :
// corretto-17 :
// corretto-18 :
// oracle-jdk-17.0.4 :
// oracle-jdk-18.0.2 :
}

public static void fastJSON2ArrayDeTime_test() throws Exception {
for (int i = 0; i < 10; i++) {
fastJSON2ArrayDeTime();
}
}

public static void fastJSON2ArrayDeTime() throws Exception {
Issue609 benchmark = new Issue609();

long start = System.currentTimeMillis();
for (int i = 0; i < 100; ++i) {
benchmark.fastJSON2ArrayDeTime(BH);
}
long millis = System.currentTimeMillis() - start;
System.out.println("fastJSON2ArrayDeTime : " + millis);
// zulu8.62.0.19 : 1473
// zulu11.52.13 :
// zulu17.32.13 :
// zulu18.28.13 :
// zulu19.0.47 :
// corretto-8 :
// corretto-11 :
// corretto-17 :
// corretto-18 :
// oracle-jdk-17.0.4 :
// oracle-jdk-18.0.2 :
}

public static void main(String[] args) throws Exception {
fastJSON1ArrayDeTime_test();
// fastJSON2ArrayDeTime_test();
}
}

0 comments on commit 7ff203d

Please sign in to comment.