View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.example.http.websocketx.benchmarkserver;
17  
18  import io.netty5.buffer.api.Buffer;
19  import io.netty5.buffer.api.BufferAllocator;
20  
21  import static java.nio.charset.StandardCharsets.US_ASCII;
22  
23  /**
24   * Generates the benchmark HTML page which is served at http://localhost:8080/
25   */
26  public final class WebSocketServerBenchmarkPage {
27  
28      private static final String NEWLINE = "\r\n";
29  
30      public static Buffer getContent(BufferAllocator allocator, String webSocketLocation) {
31          final String content = "<html><head><title>Web Socket Performance Test</title></head>" + NEWLINE +
32                  "<body>" + NEWLINE +
33                  "<h2>WebSocket Performance Test</h2>" + NEWLINE +
34                  "<label>Connection Status:</label>" + NEWLINE +
35                  "<label id=\"connectionLabel\"></label><br />" + NEWLINE +
36  
37                  "<form onsubmit=\"return false;\">" + NEWLINE +
38                  "Message size:" +
39                  "<input type=\"text\" id=\"messageSize\" value=\"1024\"/><br>" + NEWLINE +
40                  "Number of messages:" +
41                  "<input type=\"text\" id=\"nrMessages\" value=\"100000\"/><br>" + NEWLINE +
42                  "Data Type:" +
43                  "<input type=\"radio\" name=\"type\" id=\"typeText\" value=\"text\" checked>text" +
44                  "<input type=\"radio\" name=\"type\" id=\"typeBinary\" value=\"binary\">binary<br>" + NEWLINE +
45                  "Mode:<br>" + NEWLINE +
46                  "<input type=\"radio\" name=\"mode\" id=\"modeSingle\" value=\"single\" checked>" +
47                  "Wait for response after each messages<br>" + NEWLINE +
48                  "<input type=\"radio\" name=\"mode\" id=\"modeAll\" value=\"all\">" +
49                  "Send all messages and then wait for all responses<br>" + NEWLINE +
50                  "<input type=\"checkbox\" id=\"verifiyResponses\">Verify responded messages<br>" + NEWLINE +
51                  "<input type=\"button\" value=\"Start Benchmark\"" + NEWLINE +
52                  "       onclick=\"startBenchmark()\" />" + NEWLINE +
53                  "<h3>Output</h3>" + NEWLINE +
54                  "<textarea id=\"output\" style=\"width:500px;height:300px;\"></textarea>" + NEWLINE +
55                  "<br>" + NEWLINE +
56                  "<input type=\"button\" value=\"Clear\" onclick=\"clearText()\">" + NEWLINE +
57                  "</form>" + NEWLINE +
58  
59                  "<script type=\"text/javascript\">" + NEWLINE +
60                  "var benchRunning = false;" + NEWLINE +
61                  "var messageSize = 0;" + NEWLINE +
62                  "var totalMessages = 0;" + NEWLINE +
63                  "var rcvdMessages = 0;" + NEWLINE +
64                  "var isBinary = true;" + NEWLINE +
65                  "var isSingle = true;" + NEWLINE +
66                  "var verifiyResponses = false;" + NEWLINE +
67                  "var benchData = null;" + NEWLINE +
68                  "var startTime;" + NEWLINE +
69                  "var endTime;" + NEWLINE +
70                  "var socket;" + NEWLINE +
71                  "var output = document.getElementById('output');" + NEWLINE +
72                  "var connectionLabel = document.getElementById('connectionLabel');" + NEWLINE +
73                  "if (!window.WebSocket) {" + NEWLINE +
74                  "  window.WebSocket = window.MozWebSocket;" + NEWLINE +
75                  '}' + NEWLINE +
76                  "if (window.WebSocket) {" + NEWLINE +
77                  "  socket = new WebSocket(\"" + webSocketLocation + "\");" + NEWLINE +
78                  "  socket.binaryType = 'arraybuffer';" + NEWLINE +
79                  "  socket.onmessage = function(event) {" + NEWLINE +
80                  "    if (verifiyResponses) {" + NEWLINE +
81                  "        if (isBinary) {" + NEWLINE +
82                  "            if (!(event.data instanceof ArrayBuffer) || " + NEWLINE +
83                  "                  event.data.byteLength != benchData.byteLength) {" + NEWLINE +
84                  "                onInvalidResponse(benchData, event.data);" + NEWLINE +
85                  "                return;" + NEWLINE +
86                  "            } else {" + NEWLINE +
87                  "                var v = new Uint8Array(event.data);" + NEWLINE +
88                  "                for (var j = 0; j < benchData.byteLength; j++) {" + NEWLINE +
89                  "                    if (v[j] != benchData[j]) {" + NEWLINE +
90                  "                        onInvalidResponse(benchData, event.data);" + NEWLINE +
91                  "                        return;" + NEWLINE +
92                  "                    }" + NEWLINE +
93                  "                }" + NEWLINE +
94                  "            }" + NEWLINE +
95                  "        } else {" + NEWLINE +
96                  "            if (event.data != benchData) {" + NEWLINE +
97                  "                onInvalidResponse(benchData, event.data);" + NEWLINE +
98                  "                return;" + NEWLINE +
99                  "            }" + NEWLINE +
100                 "        }" + NEWLINE +
101                 "    }" + NEWLINE +
102                 "    rcvdMessages++;" + NEWLINE +
103                 "    if (rcvdMessages == totalMessages) {" + NEWLINE +
104                 "        onFinished();" + NEWLINE +
105                 "    } else if (isSingle) {" + NEWLINE +
106                 "        socket.send(benchData);" + NEWLINE +
107                 "    }" + NEWLINE +
108                 "  };" + NEWLINE +
109                 "  socket.onopen = function(event) {" + NEWLINE +
110                 "    connectionLabel.innerHTML = \"Connected\";" + NEWLINE +
111                 "  };" + NEWLINE +
112                 "  socket.onclose = function(event) {" + NEWLINE +
113                 "    benchRunning = false;" + NEWLINE +
114                 "    connectionLabel.innerHTML = \"Disconnected\";" + NEWLINE +
115                 "  };" + NEWLINE +
116                 "} else {" + NEWLINE +
117                 "  alert(\"Your browser does not support Web Socket.\");" + NEWLINE +
118                 '}' + NEWLINE +
119                 NEWLINE +
120                 "function onInvalidResponse(sent,recvd) {" + NEWLINE +
121                 "    socket.close();" + NEWLINE +
122                 "    alert(\"Error: Sent data did not match the received data!\");" + NEWLINE +
123                 "}" + NEWLINE +
124                 NEWLINE +
125                 "function clearText() {" + NEWLINE +
126                 "    output.value=\"\";" + NEWLINE +
127                 "}" + NEWLINE +
128                 NEWLINE +
129                 "function createBenchData() {" + NEWLINE +
130                 "    if (isBinary) {" + NEWLINE +
131                 "        benchData = new Uint8Array(messageSize);" + NEWLINE +
132                 "        for (var i=0; i < messageSize; i++) {" + NEWLINE +
133                 "            benchData[i] += Math.floor(Math.random() * 255);" + NEWLINE +
134                 "        }" + NEWLINE +
135                 "    } else { " + NEWLINE +
136                 "        benchData = \"\";" + NEWLINE +
137                 "        for (var i=0; i < messageSize; i++) {" + NEWLINE +
138                 "            benchData += String.fromCharCode(Math.floor(Math.random() * (123 - 65) + 65));" + NEWLINE +
139                 "        }" + NEWLINE +
140                 "    }" + NEWLINE +
141                 "}" + NEWLINE +
142                 NEWLINE +
143                 "function startBenchmark(message) {" + NEWLINE +
144                 "  if (!window.WebSocket || benchRunning) { return; }" + NEWLINE +
145                 "  if (socket.readyState == WebSocket.OPEN) {" + NEWLINE +
146                 "    isBinary = document.getElementById('typeBinary').checked;" + NEWLINE +
147                 "    isSingle = document.getElementById('modeSingle').checked;" + NEWLINE +
148                 "    verifiyResponses = document.getElementById('verifiyResponses').checked;" + NEWLINE +
149                 "    messageSize = parseInt(document.getElementById('messageSize').value);" + NEWLINE +
150                 "    totalMessages = parseInt(document.getElementById('nrMessages').value);" + NEWLINE +
151                 "    if (isNaN(messageSize) || isNaN(totalMessages)) return;" + NEWLINE +
152                 "    createBenchData();" + NEWLINE +
153                 "    output.value = output.value + '\\nStarting Benchmark';" + NEWLINE +
154                 "    rcvdMessages = 0;" + NEWLINE +
155                 "    benchRunning = true;" + NEWLINE +
156                 "    startTime = new Date();" + NEWLINE +
157                 "    if (isSingle) {" + NEWLINE +
158                 "        socket.send(benchData);" + NEWLINE +
159                 "    } else {" + NEWLINE +
160                 "        for (var i = 0; i < totalMessages; i++) socket.send(benchData);" + NEWLINE +
161                 "    }" + NEWLINE +
162                 "  } else {" + NEWLINE +
163                 "    alert(\"The socket is not open.\");" + NEWLINE +
164                 "  }" + NEWLINE +
165                 '}' + NEWLINE +
166                 NEWLINE +
167                 "function onFinished() {" + NEWLINE +
168                 "    endTime = new Date();" + NEWLINE +
169                 "    var duration = (endTime - startTime) / 1000.0;" + NEWLINE +
170                 "    output.value = output.value + '\\nTest took: ' + duration + 's';" + NEWLINE +
171                 "    var messagesPerS = totalMessages / duration;" + NEWLINE +
172                 "    output.value = output.value + '\\nPerformance: ' + messagesPerS + ' Messages/s';" + NEWLINE +
173                 "    output.value = output.value + ' in each direction';" + NEWLINE +
174                 "    output.value = output.value + '\\nRound trip: ' + 1000.0/messagesPerS + 'ms';" + NEWLINE +
175                 "    var throughput = messageSize * totalMessages / duration;" + NEWLINE +
176                 "    var throughputText;" + NEWLINE +
177                 "    if (isBinary) throughputText = throughput / (1024*1024) + ' MB/s';" + NEWLINE +
178                 "    else throughputText = throughput / (1000*1000) + ' MChars/s';" + NEWLINE +
179                 "    output.value = output.value + '\\nThroughput: ' + throughputText;" + NEWLINE +
180                 "    output.value = output.value + ' in each direction';" + NEWLINE +
181                 "    benchRunning = false;" + NEWLINE +
182                 "}" + NEWLINE +
183                 "</script>" + NEWLINE +
184                 "</body>" + NEWLINE +
185                 "</html>" + NEWLINE;
186         return allocator.copyOf(content, US_ASCII);
187     }
188 
189     private WebSocketServerBenchmarkPage() {
190         // Unused
191     }
192 }