1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package benchmarks.regression; 18 19import com.google.caliper.Param; 20import com.google.caliper.SimpleBenchmark; 21import com.google.mockwebserver.Dispatcher; 22import com.google.mockwebserver.MockResponse; 23import com.google.mockwebserver.MockWebServer; 24import com.google.mockwebserver.RecordedRequest; 25import java.io.IOException; 26import java.io.InputStream; 27import java.net.HttpURLConnection; 28import java.net.URL; 29import java.net.URLConnection; 30 31public final class URLConnectionBenchmark extends SimpleBenchmark { 32 33 @Param({"0", "1024", "1048576"}) private int bodySize; 34 @Param({"2048"}) private int chunkSize; 35 @Param({"1024"}) private int readBufferSize; 36 @Param private ResponseHeaders responseHeaders; 37 @Param private TransferEncoding transferEncoding; 38 private byte[] readBuffer; 39 40 private MockWebServer server; 41 private URL url; 42 43 private static class SingleResponseDispatcher extends Dispatcher { 44 private MockResponse response; 45 SingleResponseDispatcher(MockResponse response) { 46 this.response = response; 47 } 48 @Override public MockResponse dispatch(RecordedRequest request) { 49 return response; 50 } 51 }; 52 53 protected void setUp() throws Exception { 54 readBuffer = new byte[readBufferSize]; 55 server = new MockWebServer(); 56 57 MockResponse response = new MockResponse(); 58 responseHeaders.apply(response); 59 transferEncoding.setBody(response, bodySize, chunkSize); 60 61 // keep serving the same response for all iterations 62 server.setDispatcher(new SingleResponseDispatcher(response)); 63 server.play(); 64 65 url = server.getUrl("/"); 66 get(); // ensure the server has started its threads, etc. 67 } 68 69 protected void tearDown() throws Exception { 70 server.shutdown(); 71 } 72 73 public int timeGet(int reps) throws IOException { 74 int totalBytesRead = 0; 75 for (int i = 0; i < reps; i++) { 76 totalBytesRead += get(); 77 } 78 return totalBytesRead; 79 } 80 81 private int get() throws IOException { 82 int totalBytesRead = 0; 83 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 84 // URLConnection connection = url.openConnection(); 85 InputStream in = connection.getInputStream(); 86 int count; 87 while ((count = in.read(readBuffer)) != -1) { 88 totalBytesRead += count; 89 } 90 return totalBytesRead; 91 } 92 93 enum TransferEncoding { 94 FIXED_LENGTH, 95 CHUNKED; 96 97 void setBody(MockResponse response, int bodySize, int chunkSize) throws IOException { 98 if (this == TransferEncoding.FIXED_LENGTH) { 99 response.setBody(new byte[bodySize]); 100 } else if (this == TransferEncoding.CHUNKED) { 101 response.setChunkedBody(new byte[bodySize], chunkSize); 102 } 103 } 104 } 105 106 enum ResponseHeaders { 107 MINIMAL, 108 TYPICAL; 109 110 void apply(MockResponse response) { 111 if (this == TYPICAL) { 112 /* from http://api.twitter.com/1/statuses/public_timeline.json */ 113 response.addHeader("Date: Wed, 30 Jun 2010 17:57:39 GMT"); 114 response.addHeader("Server: hi"); 115 response.addHeader("X-RateLimit-Remaining: 0"); 116 response.addHeader("X-Runtime: 0.01637"); 117 response.addHeader("Content-Type: application/json; charset=utf-8"); 118 response.addHeader("X-RateLimit-Class: api_whitelisted"); 119 response.addHeader("Cache-Control: no-cache, max-age=300"); 120 response.addHeader("X-RateLimit-Reset: 1277920980"); 121 response.addHeader("Set-Cookie: _twitter_sess=BAh7EDoOcmV0dXJuX3RvIjZodHRwOi8vZGV2L" 122 + "nR3aXR0ZXIuY29tL3BhZ2Vz%250AL3NpZ25faW5fd2l0aF90d2l0dGVyOgxjc3JmX2lkIiUw" 123 + "ODFhNGY2NTM5NjRm%250ANjY1N2M2NzcwNWI0MDlmZGZjZjoVaW5fbmV3X3VzZXJfZmxvdzA" 124 + "6EXRyYW5z%250AX3Byb21wdDAiKXNob3dfZGlzY292ZXJhYmlsaXR5X2Zvcl9qZXNzZXdpbH" 125 + "Nv%250AbjA6E3Nob3dfaGVscF9saW5rMDoTcGFzc3dvcmRfdG9rZW4iLWUyYjlhNmM3%250A" 126 + "MWJiNzI3NWNlZDI1NDY3MGMzZWNmMTE0MjI4N2EyNGE6D2NyZWF0ZWRfYXRs%250AKwhiM%2" 127 + "52F6JKQE6CXVzZXJpA8tE3iIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxl%250Acjo6Rmxhc2" 128 + "g6OkZsYXNoSGFzaHsABjoKQHVzZWR7ADoHaWQiJWZmMTNhM2Qx%250AZTU1YTkzMmYyMWM0M" 129 + "GNhZjU4NDVjMTQz--11250628c85830219438eb7eba96a541a9af4098; domain=.twitt" 130 + "er.com; path=/"); 131 response.addHeader("Expires: Wed, 30 Jun 2010 18:02:39 GMT"); 132 response.addHeader("Vary: Accept-Encoding"); 133 } 134 } 135 } 136} 137