diff --git a/benchmarks/README.md b/benchmarks/README.md new file mode 100644 index 000000000..59f571fc5 --- /dev/null +++ b/benchmarks/README.md @@ -0,0 +1,8 @@ +OkHttp Benchmarks +======================================= + +This module allows you to test the performance of HTTP clients. + +### Running + 1. If you made modifications to `com.squareup.okhttp.benchmarks.Benchmark` run `mvn compile`. + 2. Run `mvn exec:exec` to launch a new JVM, which will execute the benchmark. diff --git a/benchmarks/pom.xml b/benchmarks/pom.xml index 14d93eeef..cecf86480 100644 --- a/benchmarks/pom.xml +++ b/benchmarks/pom.xml @@ -52,4 +52,30 @@ 4.0.15.Final + + + + org.codehaus.mojo + exec-maven-plugin + + + + java + + + + + java + + -Xms512m + -Xmx512m + -Xbootclasspath/p:${settings.localRepository}/org/mortbay/jetty/npn/npn-boot/${npn.version}/npn-boot-${npn.version}.jar + -classpath + + com.squareup.okhttp.benchmarks.Benchmark + + + + + diff --git a/benchmarks/src/main/java/com/squareup/okhttp/benchmarks/Benchmark.java b/benchmarks/src/main/java/com/squareup/okhttp/benchmarks/Benchmark.java index 16bd06350..fe23182fc 100644 --- a/benchmarks/src/main/java/com/squareup/okhttp/benchmarks/Benchmark.java +++ b/benchmarks/src/main/java/com/squareup/okhttp/benchmarks/Benchmark.java @@ -138,6 +138,7 @@ public class Benchmark { SSLContext sslContext = SslContextBuilder.localhost(); server.useHttps(sslContext.getSocketFactory(), false); server.setNpnEnabled(true); + server.setNpnProtocols(protocols); } final MockResponse response = newResponse(); diff --git a/mockwebserver/src/main/java/com/squareup/okhttp/mockwebserver/MockWebServer.java b/mockwebserver/src/main/java/com/squareup/okhttp/mockwebserver/MockWebServer.java index 90e2925e6..71c7c860f 100644 --- a/mockwebserver/src/main/java/com/squareup/okhttp/mockwebserver/MockWebServer.java +++ b/mockwebserver/src/main/java/com/squareup/okhttp/mockwebserver/MockWebServer.java @@ -106,6 +106,7 @@ public final class MockWebServer { private int port = -1; private boolean npnEnabled = true; + private List npnProtocols = Protocol.HTTP2_SPDY3_AND_HTTP; public int getPort() { if (port == -1) throw new IllegalStateException("Cannot retrieve port before calling play()"); @@ -165,6 +166,27 @@ public final class MockWebServer { this.npnEnabled = npnEnabled; } + /** + * Indicates the protocols supported by NPN on incoming HTTPS connections. + * This list is ignored when npn is disabled. + * + * @param protocols the protocols to use, in order of preference. The list + * must contain "http/1.1". It must not contain null. + */ + public void setNpnProtocols(List protocols) { + protocols = Util.immutableList(protocols); + if (!protocols.contains(Protocol.HTTP_11)) { + throw new IllegalArgumentException("protocols doesn't contain http/1.1: " + protocols); + } + if (protocols.contains(null)) { + throw new IllegalArgumentException("protocols must not contain null"); + } + if (protocols.contains(ByteString.EMPTY)) { + throw new IllegalArgumentException("protocols contains an empty string"); + } + this.npnProtocols = Util.immutableList(protocols); + } + /** * Serve requests with HTTPS rather than otherwise. * @param tunnelProxy true to expect the HTTP CONNECT method before @@ -304,8 +326,7 @@ public final class MockWebServer { openClientSockets.put(socket, true); if (npnEnabled) { - // TODO: expose means to select which protocols to advertise. - Platform.get().setNpnProtocols(sslSocket, Protocol.HTTP2_SPDY3_AND_HTTP); + Platform.get().setNpnProtocols(sslSocket, npnProtocols); } sslSocket.startHandshake(); diff --git a/okhttp/src/test/java/com/squareup/okhttp/internal/http/URLConnectionTest.java b/okhttp/src/test/java/com/squareup/okhttp/internal/http/URLConnectionTest.java index d9d3d3d2f..6ed031d54 100644 --- a/okhttp/src/test/java/com/squareup/okhttp/internal/http/URLConnectionTest.java +++ b/okhttp/src/test/java/com/squareup/okhttp/internal/http/URLConnectionTest.java @@ -2834,10 +2834,11 @@ public final class URLConnectionTest { * -Xbootclasspath/p:/tmp/npn-boot-8.1.2.v20120308.jar} */ private void enableNpn(Protocol protocol) { - server.useHttps(sslContext.getSocketFactory(), false); - server.setNpnEnabled(true); client.setSslSocketFactory(sslContext.getSocketFactory()); client.setHostnameVerifier(new RecordingHostnameVerifier()); client.setProtocols(Arrays.asList(protocol, Protocol.HTTP_11)); + server.useHttps(sslContext.getSocketFactory(), false); + server.setNpnEnabled(true); + server.setNpnProtocols(client.getProtocols()); } }