From 5fb0d4224639f941d585ac760e9c4575a5bba945 Mon Sep 17 00:00:00 2001 From: Brian Carlstrom Date: Wed, 3 Apr 2013 15:16:10 -0700 Subject: Move URLConnectionBenchmark to latest mockwebserver API (cherry picked from commit 50b871d4eb3b42a8209bb4a6d54cb649f56e873f) Change-Id: I67e7ae5de6fc3b568c34bf56706dd5146d9f2b18 --- .../regression/URLConnectionBenchmark.java | 23 ++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/benchmarks/src/benchmarks/regression/URLConnectionBenchmark.java b/benchmarks/src/benchmarks/regression/URLConnectionBenchmark.java index 36c5666..a4817b5 100644 --- a/benchmarks/src/benchmarks/regression/URLConnectionBenchmark.java +++ b/benchmarks/src/benchmarks/regression/URLConnectionBenchmark.java @@ -18,8 +18,10 @@ package benchmarks.regression; import com.google.caliper.Param; import com.google.caliper.SimpleBenchmark; +import com.google.mockwebserver.Dispatcher; import com.google.mockwebserver.MockResponse; import com.google.mockwebserver.MockWebServer; +import com.google.mockwebserver.RecordedRequest; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; @@ -38,6 +40,16 @@ public final class URLConnectionBenchmark extends SimpleBenchmark { private MockWebServer server; private URL url; + private static class SingleResponseDispatcher extends Dispatcher { + private MockResponse response; + SingleResponseDispatcher(MockResponse response) { + this.response = response; + } + @Override public MockResponse dispatch(RecordedRequest request) { + return response; + } + }; + protected void setUp() throws Exception { readBuffer = new byte[readBufferSize]; server = new MockWebServer(); @@ -45,10 +57,9 @@ public final class URLConnectionBenchmark extends SimpleBenchmark { MockResponse response = new MockResponse(); responseHeaders.apply(response); transferEncoding.setBody(response, bodySize, chunkSize); - server.enqueue(response); // keep serving the same response for all iterations - server.setSingleResponse(true); + server.setDispatcher(new SingleResponseDispatcher(response)); server.play(); url = server.getUrl("/"); @@ -56,14 +67,6 @@ public final class URLConnectionBenchmark extends SimpleBenchmark { } protected void tearDown() throws Exception { - /* - * Entice the server to shut itself down gracefully. The shutdown method - * doesn't work on Dalvik because socket.close() doesn't release blocked - * threads. Instead, read the last continuously-served request, and then - * cause the server to close the otherwise-reusable HTTP connection. - */ - server.setSingleResponse(false); - get(); server.shutdown(); } -- cgit v1.1