• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 Square, Inc.
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 package com.squareup.okhttp.benchmarks;
17 
18 import com.squareup.okhttp.HttpUrl;
19 import com.squareup.okhttp.internal.SslContextBuilder;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.concurrent.TimeUnit;
23 import java.util.zip.GZIPInputStream;
24 import javax.net.ssl.SSLContext;
25 import org.apache.http.Header;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.client.HttpClient;
28 import org.apache.http.client.methods.HttpGet;
29 import org.apache.http.conn.ClientConnectionManager;
30 import org.apache.http.conn.scheme.Scheme;
31 import org.apache.http.conn.ssl.SSLSocketFactory;
32 import org.apache.http.impl.client.DefaultHttpClient;
33 import org.apache.http.impl.conn.PoolingClientConnectionManager;
34 
35 /** Benchmark Apache HTTP client. */
36 class ApacheHttpClient extends SynchronousHttpClient {
37   private static final boolean VERBOSE = false;
38 
39   private HttpClient client;
40 
prepare(Benchmark benchmark)41   @Override public void prepare(Benchmark benchmark) {
42     super.prepare(benchmark);
43     ClientConnectionManager connectionManager = new PoolingClientConnectionManager();
44     if (benchmark.tls) {
45       SSLContext sslContext = SslContextBuilder.localhost();
46       connectionManager.getSchemeRegistry().register(
47           new Scheme("https", 443, new SSLSocketFactory(sslContext)));
48     }
49     client = new DefaultHttpClient(connectionManager);
50   }
51 
request(HttpUrl url)52   @Override public Runnable request(HttpUrl url) {
53     return new ApacheHttpClientRequest(url);
54   }
55 
56   class ApacheHttpClientRequest implements Runnable {
57     private final HttpUrl url;
58 
ApacheHttpClientRequest(HttpUrl url)59     public ApacheHttpClientRequest(HttpUrl url) {
60       this.url = url;
61     }
62 
run()63     public void run() {
64       long start = System.nanoTime();
65       try {
66         HttpResponse response = client.execute(new HttpGet(url.toString()));
67         InputStream in = response.getEntity().getContent();
68         Header contentEncoding = response.getFirstHeader("Content-Encoding");
69         if (contentEncoding != null && contentEncoding.getValue().equals("gzip")) {
70           in = new GZIPInputStream(in);
71         }
72 
73         long total = readAllAndClose(in);
74         long finish = System.nanoTime();
75 
76         if (VERBOSE) {
77           System.out.println(String.format("Transferred % 8d bytes in %4d ms",
78               total, TimeUnit.NANOSECONDS.toMillis(finish - start)));
79         }
80       } catch (IOException e) {
81         System.out.println("Failed: " + e);
82       }
83     }
84   }
85 }
86