Skip to content

Instantly share code, notes, and snippets.

@gvsmirnov
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvsmirnov/9682478 to your computer and use it in GitHub Desktop.
Save gvsmirnov/9682478 to your computer and use it in GitHub Desktop.
package sample;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import org.openjdk.jmh.annotations.*;
import java.io.IOException;
public class DummyParserBenchmark {
@GenerateMicroBenchmark
public void measureSax_slow(Json json) throws IOException {
json.parser.nextToken();
JsonToken token;
while ((token = json.parser.nextToken()) != null) {
if (token != JsonToken.FIELD_NAME) {
break;
}
json.parser.nextToken();
}
}
@GenerateMicroBenchmark
public void measureSax_fast(Json json) throws IOException {
json.parser.nextToken();
JsonToken token;
while ((token = json.parser.nextToken()) != null) {
json.parser.nextToken();
if (token != JsonToken.FIELD_NAME) {
break;
}
}
}
@GenerateMicroBenchmark
public void measureSax_new_parser_slow() throws IOException {
final JsonParser parser = new JsonFactory().createParser(Json.JSON);
parser.nextToken();
JsonToken token;
while ((token = parser.nextToken()) != null) {
if (token != JsonToken.FIELD_NAME) {
break;
}
parser.nextToken();
}
}
@GenerateMicroBenchmark
public void measureSax_new_parser_fast() throws IOException {
final JsonParser parser = new JsonFactory().createParser(Json.JSON);
parser.nextToken();
JsonToken token;
while ((token = parser.nextToken()) != null) {
parser.nextToken();
if (token != JsonToken.FIELD_NAME) {
break;
}
}
}
@State(Scope.Thread)
public static class Json {
private static final String JSON = "{\"key\":\"value\"}";
private JsonParser parser;
@Setup(Level.Iteration)
public void init() throws IOException {
parser = new JsonFactory().createParser(JSON);
}
}
}
$ java -jar build/distributions/jmh-gradle-sample-master-0.0.1-benchmarks.jar ".*Parser.*" -f 1 -wi 10 -i 10 -bm avgt -tu us
Benchmark Mode Samples Mean Mean error Units
s.DummyParserBenchmark.measureSax_fast avgt 10 0.029 0.001 us/op
s.DummyParserBenchmark.measureSax_new_parser_fast avgt 10 0.813 0.034 us/op
s.DummyParserBenchmark.measureSax_new_parser_slow avgt 10 1.733 0.056 us/op
s.DummyParserBenchmark.measureSax_slow avgt 10 0.027 0.001 us/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment