Skip to content

Commit

Permalink
Improved benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
franz1981 authored and vietj committed Jan 27, 2023
1 parent 0eb9fdb commit d2fa9b2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.netty.handler.codec.http.HttpHeaders;
import io.vertx.core.http.impl.headers.HeadersMultiMap;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
Expand All @@ -34,8 +35,8 @@ public class HeadersContainsBenchmark extends BenchmarkBase {
public void setup() {
nettySmallHeaders = new DefaultHttpHeaders();
vertxSmallHeaders = HeadersMultiMap.httpHeaders();
setBaseHeaders(nettySmallHeaders);
setBaseHeaders(vertxSmallHeaders);
setBaseHeaders(nettySmallHeaders, true, true);
setBaseHeaders(vertxSmallHeaders, true, true);
}

@Benchmark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.vertx.core.http.impl.headers.HeadersMultiMap;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
Expand All @@ -32,6 +33,12 @@
@State(Scope.Thread)
public class HeadersEncodeBenchmark extends BenchmarkBase {

@Param({"true", "false"})
public boolean asciiNames;

@Param({"true", "false"})
public boolean asciiValues;

@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public static void consume(final ByteBuf buf) {
}
Expand All @@ -58,8 +65,8 @@ public void setup() {
emptyHeaders = EmptyHttpHeaders.INSTANCE;
nettySmallHeaders = new DefaultHttpHeaders();
vertxSmallHeaders = HeadersMultiMap.httpHeaders();
setBaseHeaders(nettySmallHeaders);
setBaseHeaders(vertxSmallHeaders);
setBaseHeaders(nettySmallHeaders, asciiNames, asciiValues);
setBaseHeaders(vertxSmallHeaders, asciiNames, asciiValues);
}

@Benchmark
Expand Down
26 changes: 20 additions & 6 deletions src/test/benchmarks/io/vertx/benchmarks/HeadersSetBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,35 @@

import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpHeaders;
import io.vertx.core.http.impl.HttpUtils;
import io.vertx.core.http.impl.headers.HeadersMultiMap;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.Measurement;
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.Warmup;

import java.util.concurrent.TimeUnit;

import static io.vertx.benchmarks.HeadersUtils.setBaseHeaders;

/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
@State(Scope.Thread)
@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 400, timeUnit = TimeUnit.MILLISECONDS)
public class HeadersSetBenchmark extends BenchmarkBase {

@Param({"true", "false"})
public boolean validate;

@Param({"true", "false"})
public boolean asciiNames;

@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public static void consume(final HttpHeaders headers) {
}
Expand All @@ -37,21 +51,21 @@ public static void consume(final HttpHeaders headers) {

@Setup
public void setup() {
nettySmallHeaders = new DefaultHttpHeaders();
vertxSmallHeaders = HeadersMultiMap.httpHeaders();
nettySmallHeaders = new DefaultHttpHeaders(validate);
vertxSmallHeaders = new HeadersMultiMap(validate? HttpUtils::validateHeader : null);
}

@Benchmark
public void nettySmall() throws Exception {
public void nettySmall() {
nettySmallHeaders.clear();
setBaseHeaders(nettySmallHeaders);
setBaseHeaders(nettySmallHeaders, asciiNames, true);
consume(nettySmallHeaders);
}

@Benchmark
public void vertxSmall() throws Exception {
public void vertxSmall() {
vertxSmallHeaders.clear();
setBaseHeaders(vertxSmallHeaders);
setBaseHeaders(vertxSmallHeaders, asciiNames, true);
consume(vertxSmallHeaders);
}
}
22 changes: 17 additions & 5 deletions src/test/benchmarks/io/vertx/benchmarks/HeadersUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,22 @@ public abstract class HeadersUtils {
public static final CharSequence CONTENT_LENGTH_HEADER = io.vertx.core.http.HttpHeaders.createOptimized("20");
public static final CharSequence DATE_HEADER = io.vertx.core.http.HttpHeaders.createOptimized(DATE_FORMAT.format(new Date()));

public static void setBaseHeaders(HttpHeaders headers) {
headers.add(io.vertx.core.http.HttpHeaders.CONTENT_TYPE, TEXT_PLAIN_HEADER);
headers.add(io.vertx.core.http.HttpHeaders.CONTENT_LENGTH, CONTENT_LENGTH_HEADER);
headers.add(io.vertx.core.http.HttpHeaders.SERVER, VERTX_HEADER);
headers.add(io.vertx.core.http.HttpHeaders.DATE, DATE_HEADER);
public static void setBaseHeaders(HttpHeaders headers, boolean asciiNames, boolean asciiValues) {
headers.add(toString(io.vertx.core.http.HttpHeaders.CONTENT_TYPE, !asciiNames),
toString(TEXT_PLAIN_HEADER, !asciiValues));
headers.add(toString(io.vertx.core.http.HttpHeaders.CONTENT_LENGTH, !asciiNames),
toString(CONTENT_LENGTH_HEADER, !asciiValues));
headers.add(toString(io.vertx.core.http.HttpHeaders.SERVER, !asciiNames),
toString(VERTX_HEADER, !asciiValues));
headers.add(toString(io.vertx.core.http.HttpHeaders.DATE, !asciiNames),
toString(DATE_HEADER, !asciiValues));
}

private static CharSequence toString(CharSequence chars, boolean toString) {
if (!toString) {
return chars;
}
return chars.toString();
}

}

0 comments on commit d2fa9b2

Please sign in to comment.