Skip to content

Instantly share code, notes, and snippets.

@mvitz
Created October 21, 2015 08:27
Show Gist options
  • Select an option

  • Save mvitz/88870a52aafa3cbc631e to your computer and use it in GitHub Desktop.

Select an option

Save mvitz/88870a52aafa3cbc631e to your computer and use it in GitHub Desktop.
Adam Bien JSON Parser Benchmark
mvn clean package && java -jar target/jmh-abien-json-1.0-SNAPSHOT.jar -f 1
package com.innoq;
import java.util.Map;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
@State(Scope.Benchmark)
public class MyBenchmark {
private ScriptEngine engine;
private ObjectMapper mapper;
private String json;
@Setup
public void setUp() throws Exception {
engine = new ScriptEngineManager().getEngineByName("javascript");
mapper = new ObjectMapper();
json = "{ \"foo\": \"bar\", \"bar\": 1 }";
}
@Benchmark
public Object nashorn() throws Exception {
Map<String, Object> map = (Map<String, Object>) engine.eval("Java.asJSONCompatible(" + json + ")");
return map.get("foo");
}
@Benchmark
public Object jackson() throws Exception {
Map<String, Object> map = mapper.readValue(json, Map.class);
return map.get("foo");
}
}
<?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>
<groupId>com.innoq</groupId>
<artifactId>jmh-abien-json</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<encoding>UTF-8</encoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jackson.version>2.6.3</jackson.version>
<jmh.version>1.11.1</jmh.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jmh.Main</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<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>
@mvitz
Copy link
Copy Markdown
Author

mvitz commented Oct 21, 2015

My results:

# JMH 1.11.1 (released 26 days ago)
# VM version: JDK 1.8.0_66, VM 25.66-b17
# VM invoker: /Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/jre/bin/java
# VM options: <none>
# Warmup: 20 iterations, 1 s each
# Measurement: 20 iterations, 1 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.innoq.MyBenchmark.jackson

# Run progress: 0.00% complete, ETA 00:01:20
# Fork: 1 of 1
# Warmup Iteration   1: 2629005.675 ops/s
# Warmup Iteration   2: 3087809.273 ops/s
# Warmup Iteration   3: 2899430.236 ops/s
# Warmup Iteration   4: 3493158.693 ops/s
# Warmup Iteration   5: 3484880.289 ops/s
# Warmup Iteration   6: 3460900.099 ops/s
# Warmup Iteration   7: 3478273.312 ops/s
# Warmup Iteration   8: 3488365.981 ops/s
# Warmup Iteration   9: 3457305.762 ops/s
# Warmup Iteration  10: 3392265.166 ops/s
# Warmup Iteration  11: 3501613.345 ops/s
# Warmup Iteration  12: 3401774.519 ops/s
# Warmup Iteration  13: 3466809.456 ops/s
# Warmup Iteration  14: 3409601.714 ops/s
# Warmup Iteration  15: 3397648.992 ops/s
# Warmup Iteration  16: 3419631.443 ops/s
# Warmup Iteration  17: 3452761.339 ops/s
# Warmup Iteration  18: 3470757.832 ops/s
# Warmup Iteration  19: 3433505.389 ops/s
# Warmup Iteration  20: 3444415.722 ops/s
Iteration   1: 3454980.584 ops/s
Iteration   2: 3448530.500 ops/s
Iteration   3: 3386573.800 ops/s
Iteration   4: 3443412.639 ops/s
Iteration   5: 3424943.635 ops/s
Iteration   6: 3430292.386 ops/s
Iteration   7: 3433817.987 ops/s
Iteration   8: 3461839.992 ops/s
Iteration   9: 3422743.708 ops/s
Iteration  10: 3411311.700 ops/s
Iteration  11: 3447718.821 ops/s
Iteration  12: 3458064.274 ops/s
Iteration  13: 3473750.820 ops/s
Iteration  14: 3458985.171 ops/s
Iteration  15: 3431701.059 ops/s
Iteration  16: 3451649.231 ops/s
Iteration  17: 3347815.005 ops/s
Iteration  18: 3424948.197 ops/s
Iteration  19: 3461147.893 ops/s
Iteration  20: 3473233.353 ops/s


Result "jackson":
  3437373.038 ±(99.9%) 26239.248 ops/s [Average]
  (min, avg, max) = (3347815.005, 3437373.038, 3473750.820), stdev = 30217.157
  CI (99.9%): [3411133.790, 3463612.286] (assumes normal distribution)


# JMH 1.11.1 (released 26 days ago)
# VM version: JDK 1.8.0_66, VM 25.66-b17
# VM invoker: /Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/jre/bin/java
# VM options: <none>
# Warmup: 20 iterations, 1 s each
# Measurement: 20 iterations, 1 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.innoq.MyBenchmark.nashorn

# Run progress: 50.00% complete, ETA 00:00:40
# Fork: 1 of 1
# Warmup Iteration   1: 314836.784 ops/s
# Warmup Iteration   2: 457292.958 ops/s
# Warmup Iteration   3: 452481.341 ops/s
# Warmup Iteration   4: 467549.602 ops/s
# Warmup Iteration   5: 434453.489 ops/s
# Warmup Iteration   6: 478652.579 ops/s
# Warmup Iteration   7: 467969.732 ops/s
# Warmup Iteration   8: 456898.670 ops/s
# Warmup Iteration   9: 438399.655 ops/s
# Warmup Iteration  10: 480044.350 ops/s
# Warmup Iteration  11: 484631.348 ops/s
# Warmup Iteration  12: 449051.037 ops/s
# Warmup Iteration  13: 490185.729 ops/s
# Warmup Iteration  14: 450980.721 ops/s
# Warmup Iteration  15: 419229.008 ops/s
# Warmup Iteration  16: 483701.894 ops/s
# Warmup Iteration  17: 476033.288 ops/s
# Warmup Iteration  18: 475878.308 ops/s
# Warmup Iteration  19: 455872.049 ops/s
# Warmup Iteration  20: 483577.066 ops/s
Iteration   1: 481456.798 ops/s
Iteration   2: 479429.156 ops/s
Iteration   3: 434238.054 ops/s
Iteration   4: 483401.128 ops/s
Iteration   5: 482542.221 ops/s
Iteration   6: 458821.388 ops/s
Iteration   7: 473754.482 ops/s
Iteration   8: 479511.061 ops/s
Iteration   9: 480845.175 ops/s
Iteration  10: 453889.462 ops/s
Iteration  11: 477882.852 ops/s
Iteration  12: 480242.007 ops/s
Iteration  13: 485578.282 ops/s
Iteration  14: 452466.312 ops/s
Iteration  15: 479398.956 ops/s
Iteration  16: 479512.344 ops/s
Iteration  17: 464966.955 ops/s
Iteration  18: 451470.362 ops/s
Iteration  19: 475590.137 ops/s
Iteration  20: 478809.403 ops/s


Result "nashorn":
  471690.327 ±(99.9%) 12214.032 ops/s [Average]
  (min, avg, max) = (434238.054, 471690.327, 485578.282), stdev = 14065.697
  CI (99.9%): [459476.295, 483904.358] (assumes normal distribution)


# Run complete. Total time: 00:01:22

Benchmark             Mode  Cnt        Score       Error  Units
MyBenchmark.jackson  thrpt   20  3437373.038 ± 26239.248  ops/s
MyBenchmark.nashorn  thrpt   20   471690.327 ± 12214.032  ops/s

@hendriks73
Copy link
Copy Markdown

Interesting.
What about memory consumption? How does that compare?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment