forked from square/okhttp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New benchmark targets for Apache HTTP Client and URLConnection.
OkHttp [HTTP_11] bodyByteCount=1048576 headerCount=20 threadCount=10 Requests per second: 690.9 UrlConnection [HTTP_11] bodyByteCount=1048576 headerCount=20 threadCount=10 Requests per second: 671.3 ApacheHttpClient [HTTP_11] bodyByteCount=1048576 headerCount=20 threadCount=10 Requests per second: 317.4
- Loading branch information
1 parent
e369755
commit 7c21992
Showing
5 changed files
with
190 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
benchmarks/src/main/java/com/squareup/okhttp/benchmarks/ApacheHttpClientRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (C) 2014 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.squareup.okhttp.benchmarks; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.zip.GZIPInputStream; | ||
import org.apache.http.Header; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.methods.HttpGet; | ||
|
||
class ApacheHttpClientRequest implements Runnable { | ||
private static final boolean VERBOSE = false; | ||
private final HttpClient client; | ||
private final String url; | ||
|
||
public ApacheHttpClientRequest(String url, HttpClient client) { | ||
this.client = client; | ||
this.url = url; | ||
} | ||
|
||
public void run() { | ||
byte[] buffer = new byte[1024]; | ||
long start = System.nanoTime(); | ||
try { | ||
HttpResponse response = client.execute(new HttpGet(url)); | ||
InputStream in = response.getEntity().getContent(); | ||
Header contentEncoding = response.getFirstHeader("Content-Encoding"); | ||
if (contentEncoding != null && contentEncoding.getValue().equals("gzip")) { | ||
in = new GZIPInputStream(in); | ||
} | ||
|
||
// Consume the response body. | ||
int total = 0; | ||
for (int count; (count = in.read(buffer)) != -1; ) { | ||
total += count; | ||
} | ||
in.close(); | ||
long finish = System.nanoTime(); | ||
|
||
if (VERBOSE) { | ||
System.out.println(String.format("Transferred % 8d bytes in %4d ms", | ||
total, TimeUnit.NANOSECONDS.toMillis(finish - start))); | ||
} | ||
} catch (IOException e) { | ||
System.out.println("Failed: " + e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
benchmarks/src/main/java/com/squareup/okhttp/benchmarks/UrlConnectionRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (C) 2014 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.squareup.okhttp.benchmarks; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
/** Uses the default java.net.HttpURLConnection implementation. */ | ||
class UrlConnectionRequest implements Runnable { | ||
private static final boolean VERBOSE = false; | ||
|
||
private final URL url; | ||
|
||
public UrlConnectionRequest(String url) { | ||
try { | ||
this.url = new URL(url); | ||
} catch (MalformedURLException e) { | ||
throw new AssertionError(); | ||
} | ||
} | ||
|
||
public void run() { | ||
byte[] buffer = new byte[1024]; | ||
long start = System.nanoTime(); | ||
try { | ||
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); | ||
InputStream in = urlConnection.getInputStream(); | ||
if ("gzip".equals(urlConnection.getHeaderField("Content-Encoding"))) { | ||
in = new GZIPInputStream(in); | ||
} | ||
|
||
// Consume the response body. | ||
int total = 0; | ||
for (int count; (count = in.read(buffer)) != -1; ) { | ||
total += count; | ||
} | ||
in.close(); | ||
long finish = System.nanoTime(); | ||
|
||
if (VERBOSE) { | ||
System.out.println(String.format("Transferred % 8d bytes in %4d ms", | ||
total, TimeUnit.NANOSECONDS.toMillis(finish - start))); | ||
} | ||
} catch (IOException e) { | ||
System.out.println("Failed: " + e); | ||
} | ||
} | ||
} |