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
17package benchmarks.regression;
18
19import com.google.caliper.BeforeExperiment;
20import com.google.caliper.Param;
21import java.io.IOException;
22import java.io.InputStream;
23import java.io.OutputStream;
24import java.net.InetAddress;
25import java.net.Socket;
26import java.net.URL;
27import javax.net.SocketFactory;
28import javax.net.ssl.SSLContext;
29
30public 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;
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
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
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