Skip to content

Instantly share code, notes, and snippets.

@vladimirdolzhenko
Last active November 14, 2016 12:06
Show Gist options
  • Save vladimirdolzhenko/2afff0936fef7dbc7071030143fdfc18 to your computer and use it in GitHub Desktop.
Save vladimirdolzhenko/2afff0936fef7dbc7071030143fdfc18 to your computer and use it in GitHub Desktop.
package perf;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
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;
import org.openjdk.jmh.runner.options.OptionsBuilder;
/**
* @author [email protected]
* @since 2016-11-12
*/
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(1)
@Warmup(iterations = 2, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 2, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Threads(1)
public class PerfInternCache {
@State(Scope.Benchmark)
public static class BenchmarkState {
final JsonFactory factory = new JsonFactory();
String json;
@Param( {"100", "1000", "10000", "100000"})
public int size;
@Param( {"true", "false"})
public boolean intern;
@Setup
public void setUp() {
if (intern) {
factory.enable(JsonFactory.Feature.INTERN_FIELD_NAMES);
} else {
factory.disable(JsonFactory.Feature.INTERN_FIELD_NAMES);
}
StringBuilder builder = new StringBuilder("{");
for (int i = 0; i < size; i++) {
if (i > 0) {
builder.append(", \n");
}
builder.append('"')
.append("someQName")
.append(i)
.append("\": ")
.append(i);
}
builder.append("}");
json = builder.toString();
}
}
@Benchmark
public void parseJson(BenchmarkState state, Blackhole bh) throws IOException {
JsonParser parser = state.factory.createParser(state.json);
for (; ; ) {
JsonToken x = parser.nextToken();
if (x == null) {
break;
}
bh.consume(x);
}
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(PerfInternCache.class.getSimpleName())
.addProfiler(GCProfiler.class)
.warmupIterations(5)
.measurementIterations(5)
.build();
new Runner(opt).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment