Created
November 13, 2016 08:30
-
-
Save vladimirdolzhenko/4116b4042f9abff0b03570984cb92346 to your computer and use it in GitHub Desktop.
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
package perf; | |
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.Scope; | |
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; | |
import java.io.IOException; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* @author vladimir.dolzhenko@gmail.com | |
* @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 String json; | |
public BenchmarkState() { | |
StringBuilder builder = new StringBuilder("{"); | |
for(int i = 0; i < 10000; 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 { | |
final JsonFactory factory = new JsonFactory(); | |
JsonParser parser = factory.createParser(state.json); | |
while (parser.nextValue() != null) { | |
JsonToken x = parser.currentToken(); | |
bh.consume(x); | |
} | |
} | |
public static void main(String[] args) throws RunnerException { | |
Options opt = new OptionsBuilder() | |
.include(PerfInternCache.class.getSimpleName()) | |
.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