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.BeforeExperiment;
20 import com.google.caliper.Param;
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 {
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;
WebSite(String uri)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     @BeforeExperiment
setUp()75     protected void setUp() throws Exception {
76         SSLContext sslContext = SSLContext.getInstance("SSL");
77         sslContext.init(null, null, null);
78         this.sf = sslContext.getSocketFactory();
79     }
80 
time(int reps)81     public void time(int reps) throws Exception {
82         for (int i = 0; i < reps; ++i) {
83             Socket s = sf.createSocket(webSite.host, webSite.port);
84             OutputStream out = s.getOutputStream();
85             out.write(webSite.request);
86             InputStream in = s.getInputStream();
87             while (true) {
88                 int n = in.read(buffer);
89                 if (n == -1) {
90                     break;
91                 }
92             }
93             in.close();
94         }
95     }
96 }
97