Home | History | Annotate | Download | only in regression
      1 /*
      2  * Copyright (C) 2010 Google 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 
     17 package benchmarks.regression;
     18 
     19 import com.google.caliper.Param;
     20 import com.google.caliper.SimpleBenchmark;
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 import java.io.OutputStream;
     24 import java.net.InetAddress;
     25 import java.net.Socket;
     26 import java.net.URL;
     27 import javax.net.SocketFactory;
     28 import javax.net.ssl.SSLContext;
     29 
     30 public class SSLSocketBenchmark extends SimpleBenchmark {
     31 
     32     private static final int BUFFER_SIZE = 8192;
     33 
     34     final byte[] buffer = new byte[BUFFER_SIZE];
     35 
     36     @Param private WebSite webSite;
     37 
     38     public enum WebSite {
     39         DOCS("https://docs.google.com"),
     40         MAIL("https://mail.google.com"),
     41         SITES("https://sites.google.com"),
     42         WWW("https://www.google.com");
     43         final InetAddress host;
     44         final int port;
     45         final byte[] request;
     46         WebSite(String uri) {
     47             try {
     48                 URL url = new URL(uri);
     49 
     50                 this.host = InetAddress.getByName(url.getHost());
     51 
     52                 int p = url.getPort();
     53                 String portString;
     54                 if (p == -1) {
     55                     this.port = 443;
     56                     portString = "";
     57                 } else {
     58                     this.port = p;
     59                     portString = ":" + port;
     60                 }
     61 
     62                 this.request = ("GET " + uri + " HTTP/1.0\r\n"
     63                                 + "Host: " + host + portString + "\r\n"
     64                                 +"\r\n").getBytes();
     65 
     66             } catch (IOException e) {
     67                 throw new RuntimeException(e);
     68             }
     69         }
     70     }
     71 
     72     private SocketFactory sf;
     73 
     74     @Override protected void setUp() throws Exception {
     75         SSLContext sslContext = SSLContext.getInstance("SSL");
     76         sslContext.init(null, null, null);
     77         this.sf = sslContext.getSocketFactory();
     78     }
     79 
     80     public void time(int reps) throws Exception {
     81         for (int i = 0; i < reps; ++i) {
     82             Socket s = sf.createSocket(webSite.host, webSite.port);
     83             OutputStream out = s.getOutputStream();
     84             out.write(webSite.request);
     85             InputStream in = s.getInputStream();
     86             while (true) {
     87                 int n = in.read(buffer);
     88                 if (n == -1) {
     89                     break;
     90                 }
     91             }
     92             in.close();
     93         }
     94     }
     95 }
     96