/*
 * 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 com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;
import com.squareup.okhttp.internal.SslContextBuilder;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;

class OkHttp extends SynchronousHttpClient {
  private static final boolean VERBOSE = false;

  private OkHttpClient client;

  @Override public void prepare(Benchmark benchmark) {
    super.prepare(benchmark);
    client = new OkHttpClient();
    client.setProtocols(benchmark.protocols);

    if (benchmark.tls) {
      SSLContext sslContext = SslContextBuilder.localhost();
      SSLSocketFactory socketFactory = sslContext.getSocketFactory();
      HostnameVerifier hostnameVerifier = new HostnameVerifier() {
        @Override public boolean verify(String s, SSLSession session) {
          return true;
        }
      };
      client.setSslSocketFactory(socketFactory);
      client.setHostnameVerifier(hostnameVerifier);
    }
  }

  @Override public Runnable request(HttpUrl url) {
    return new OkHttpRequest(url);
  }

  class OkHttpRequest implements Runnable {
    private final HttpUrl url;

    public OkHttpRequest(HttpUrl url) {
      this.url = url;
    }

    public void run() {
      long start = System.nanoTime();
      try {
        HttpURLConnection urlConnection = new OkUrlFactory(client).open(url.url());
        long total = readAllAndClose(urlConnection.getInputStream());
        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);
      }
    }
  }
}

@@ -635,16 +635,49 @@
 marks;%0A%0A
+import com.squareup.okhttp.Call;%0A
 import c
@@ -772,27 +772,63 @@
 .okhttp.
-OkUrlFactor
+Request;%0Aimport com.squareup.okhttp.ResponseBod
 y;%0Aimpor
@@ -909,43 +909,8 @@
 on;%0A
-import java.net.HttpURLConnection;%0A
 impo
@@ -1875,24 +1875,96 @@
 pUrl url) %7B%0A
+    Call call = client.newCall(new Request.Builder().url(url).build());%0A
     return n
@@ -1980,18 +1980,19 @@
 Request(
-ur
+cal
 l);%0A  %7D%0A
@@ -2054,26 +2054,24 @@
 e final 
-HttpUrl ur
+Call cal
 l;%0A%0A    
@@ -2087,34 +2087,32 @@
 HttpRequest(
-HttpUrl ur
+Call cal
 l) %7B%0A      t
@@ -2119,16 +2119,18 @@
 his.
-ur
+cal
 l = 
-ur
+cal
 l;%0A 
@@ -2221,81 +2221,48 @@
     
-HttpURLConnection urlConnection = new OkUrlFactory(client).open(url.url()
+ResponseBody body = call.execute().body(
 );%0A 
@@ -2301,30 +2301,17 @@
 ose(
-urlConnection.getInput
+body.byte
 Stre

Change benchmark to use OkHttp's native API.