Skip to content

Instantly share code, notes, and snippets.

@stokito
Last active July 30, 2020 13:19
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 stokito/a82eed1aef6ad965e2a279825f1c3420 to your computer and use it in GitHub Desktop.
Save stokito/a82eed1aef6ad965e2a279825f1c3420 to your computer and use it in GitHub Desktop.
package com.github.stokito.experiments;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import java.util.StringTokenizer;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.openjdk.jmh.annotations.Mode.Throughput;
/**
* To run the benchmark:
* java -jar target/benchmarks.jar -wi 1 -wf 1 -tu ms -i 1 -f 1 -foe true -gc true EtagBench
* Results:
* Benchmark (headerValue) Mode Cnt Score Error Units
* EtagBench.testParseOld W/"1047-1578315296666" thrpt 36099.731 ops/ms
* EtagBench.testParseOld "AnotherEtag", W/"1047-1578315296666" thrpt 8522.215 ops/ms
* EtagBench.testParseOld "AnotherEtag", "1047-1578315296666" thrpt 11069.839 ops/ms
* EtagBench.testParseSimple W/"1047-1578315296666" thrpt 108970.307 ops/ms
* EtagBench.testParseSimple "AnotherEtag", W/"1047-1578315296666" thrpt 95318.920 ops/ms
* EtagBench.testParseSimple "AnotherEtag", "1047-1578315296666" thrpt 108959.153 ops/ms
*/
@State(Scope.Thread)
public class EtagBench {
private String eTag = "\"1047-1578315296666\"";
@Param({
"W/\"1047-1578315296666\"",
"\"AnotherEtag\", W/\"1047-1578315296666\"",
"\"AnotherEtag\", \"1047-1578315296666\""
})
private String headerValue;
@Benchmark
@BenchmarkMode(Throughput)
@OutputTimeUnit(MILLISECONDS)
public boolean testParseOld() {
boolean conditionSatisfied = false;
StringTokenizer commaTokenizer =
new StringTokenizer(headerValue, ",");
while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) {
String currentToken = commaTokenizer.nextToken();
currentToken = currentToken.trim();
if (currentToken.startsWith("W/")) {
currentToken = currentToken.substring(2);
}
if (currentToken.equals(eTag))
conditionSatisfied = true;
}
return conditionSatisfied;
}
@Benchmark
@BenchmarkMode(Throughput)
@OutputTimeUnit(MILLISECONDS)
public boolean testParseSimple() {
boolean conditionSatisfied = headerValue.contains(eTag);
return conditionSatisfied;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment