OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library chat_stress_client; | 5 library chat_stress_client; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 import 'dart:json'; | 7 import 'dart:json' as json; |
8 | 8 |
9 | 9 |
10 class ChatStressClient { | 10 class ChatStressClient { |
11 ChatStressClient() : verbose = false, messagesToSend = 100; | 11 ChatStressClient() : verbose = false, messagesToSend = 100; |
12 | 12 |
13 void run() { | 13 void run() { |
14 HttpClient httpClient; // HTTP client connection factory. | 14 HttpClient httpClient; // HTTP client connection factory. |
15 String sessionId; // Session id when connected. | 15 String sessionId; // Session id when connected. |
16 int sendMessageCount; // Number of messages sent. | 16 int sendMessageCount; // Number of messages sent. |
17 int messageCount; | 17 int messageCount; |
18 int receiveMessageCount; // Number of messages received. | 18 int receiveMessageCount; // Number of messages received. |
19 | 19 |
20 int port; | 20 int port; |
21 | 21 |
22 void printServerError(HttpClientResponse response) { | 22 void printServerError(HttpClientResponse response) { |
23 print("Server error ${response.statusCode} ${response.reasonPhrase}"); | 23 print("Server error ${response.statusCode} ${response.reasonPhrase}"); |
24 } | 24 } |
25 | 25 |
26 void printProtocolError() { | 26 void printProtocolError() { |
27 print("Protocol error"); | 27 print("Protocol error"); |
28 } | 28 } |
29 | 29 |
30 Map parseResponse(HttpClientResponse response, | 30 Map parseResponse(HttpClientResponse response, |
31 String data, | 31 String data, |
32 String expected) { | 32 String expected) { |
33 if (response.statusCode != HttpStatus.OK) { | 33 if (response.statusCode != HttpStatus.OK) { |
34 printServerError(response); | 34 printServerError(response); |
35 return null; | 35 return null; |
36 } | 36 } |
37 var responseData = JSON.parse(data); | 37 var responseData = json.parse(data); |
38 if (responseData["response"] != expected ) { | 38 if (responseData["response"] != expected ) { |
39 printProtocolError(); | 39 printProtocolError(); |
40 return null; | 40 return null; |
41 } | 41 } |
42 return responseData; | 42 return responseData; |
43 } | 43 } |
44 | 44 |
45 void leave() { | 45 void leave() { |
46 void leaveResponseHandler(HttpClientResponse response, String data) { | 46 void leaveResponseHandler(HttpClientResponse response, String data) { |
47 httpClient.shutdown(); | 47 httpClient.shutdown(); |
48 } | 48 } |
49 | 49 |
50 Map leaveRequest = new Map(); | 50 Map leaveRequest = new Map(); |
51 leaveRequest["request"] = "leave"; | 51 leaveRequest["request"] = "leave"; |
52 leaveRequest["sessionId"] = sessionId; | 52 leaveRequest["sessionId"] = sessionId; |
53 HttpClientConnection conn = httpClient.post("127.0.0.1", port, "/leave"); | 53 HttpClientConnection conn = httpClient.post("127.0.0.1", port, "/leave"); |
54 conn.onRequest = (HttpClientRequest request) { | 54 conn.onRequest = (HttpClientRequest request) { |
55 request.outputStream.writeString(JSON.stringify(leaveRequest)); | 55 request.outputStream.writeString(json.stringify(leaveRequest)); |
56 request.outputStream.close(); | 56 request.outputStream.close(); |
57 }; | 57 }; |
58 conn.onResponse = (HttpClientResponse response) { | 58 conn.onResponse = (HttpClientResponse response) { |
59 StringInputStream stream = new StringInputStream(response.inputStream); | 59 StringInputStream stream = new StringInputStream(response.inputStream); |
60 StringBuffer body = new StringBuffer(); | 60 StringBuffer body = new StringBuffer(); |
61 stream.onData = () => body.add(stream.read()); | 61 stream.onData = () => body.add(stream.read()); |
62 stream.onClosed = () { | 62 stream.onClosed = () { |
63 leaveResponseHandler(response, body.toString()); | 63 leaveResponseHandler(response, body.toString()); |
64 }; | 64 }; |
65 }; | 65 }; |
(...skipping 10 matching lines...) Expand all Loading... |
76 } | 76 } |
77 | 77 |
78 Map messageRequest = new Map(); | 78 Map messageRequest = new Map(); |
79 messageRequest["request"] = "receive"; | 79 messageRequest["request"] = "receive"; |
80 messageRequest["sessionId"] = sessionId; | 80 messageRequest["sessionId"] = sessionId; |
81 messageRequest["nextMessage"] = receiveMessageCount; | 81 messageRequest["nextMessage"] = receiveMessageCount; |
82 messageRequest["maxMessages"] = 100; | 82 messageRequest["maxMessages"] = 100; |
83 HttpClientConnection conn = | 83 HttpClientConnection conn = |
84 httpClient.post("127.0.0.1", port, "/receive"); | 84 httpClient.post("127.0.0.1", port, "/receive"); |
85 conn.onRequest = (HttpClientRequest request) { | 85 conn.onRequest = (HttpClientRequest request) { |
86 request.outputStream.writeString(JSON.stringify(messageRequest)); | 86 request.outputStream.writeString(json.stringify(messageRequest)); |
87 request.outputStream.close(); | 87 request.outputStream.close(); |
88 }; | 88 }; |
89 conn.onResponse = (HttpClientResponse response) { | 89 conn.onResponse = (HttpClientResponse response) { |
90 StringInputStream stream = new StringInputStream(response.inputStream); | 90 StringInputStream stream = new StringInputStream(response.inputStream); |
91 StringBuffer body = new StringBuffer(); | 91 StringBuffer body = new StringBuffer(); |
92 stream.onData = () => body.add(stream.read()); | 92 stream.onData = () => body.add(stream.read()); |
93 stream.onClosed = () { | 93 stream.onClosed = () { |
94 receiveResponseHandler(response, body.toString()); | 94 receiveResponseHandler(response, body.toString()); |
95 }; | 95 }; |
96 }; | 96 }; |
(...skipping 17 matching lines...) Expand all Loading... |
114 } | 114 } |
115 } | 115 } |
116 | 116 |
117 Map messageRequest = new Map(); | 117 Map messageRequest = new Map(); |
118 messageRequest["request"] = "message"; | 118 messageRequest["request"] = "message"; |
119 messageRequest["sessionId"] = sessionId; | 119 messageRequest["sessionId"] = sessionId; |
120 messageRequest["message"] = "message $sendMessageCount"; | 120 messageRequest["message"] = "message $sendMessageCount"; |
121 HttpClientConnection conn = | 121 HttpClientConnection conn = |
122 httpClient.post("127.0.0.1", port, "/message"); | 122 httpClient.post("127.0.0.1", port, "/message"); |
123 conn.onRequest = (HttpClientRequest request) { | 123 conn.onRequest = (HttpClientRequest request) { |
124 request.outputStream.writeString(JSON.stringify(messageRequest)); | 124 request.outputStream.writeString(json.stringify(messageRequest)); |
125 request.outputStream.close(); | 125 request.outputStream.close(); |
126 }; | 126 }; |
127 conn.onResponse = (HttpClientResponse response) { | 127 conn.onResponse = (HttpClientResponse response) { |
128 StringInputStream stream = new StringInputStream(response.inputStream); | 128 StringInputStream stream = new StringInputStream(response.inputStream); |
129 StringBuffer body = new StringBuffer(); | 129 StringBuffer body = new StringBuffer(); |
130 stream.onData = () => body.add(stream.read()); | 130 stream.onData = () => body.add(stream.read()); |
131 stream.onClosed = () { | 131 stream.onClosed = () { |
132 sendResponseHandler(response, body.toString()); | 132 sendResponseHandler(response, body.toString()); |
133 }; | 133 }; |
134 }; | 134 }; |
135 }; | 135 }; |
136 | 136 |
137 void join() { | 137 void join() { |
138 void joinResponseHandler(HttpClientResponse response, String data) { | 138 void joinResponseHandler(HttpClientResponse response, String data) { |
139 var responseData = parseResponse(response, data, "join"); | 139 var responseData = parseResponse(response, data, "join"); |
140 if (responseData == null) return; | 140 if (responseData == null) return; |
141 sessionId = responseData["sessionId"]; | 141 sessionId = responseData["sessionId"]; |
142 | 142 |
143 messageCount = 0; | 143 messageCount = 0; |
144 sendMessageCount = 0; | 144 sendMessageCount = 0; |
145 receiveMessageCount = 0; | 145 receiveMessageCount = 0; |
146 sendMessage(); | 146 sendMessage(); |
147 } | 147 } |
148 | 148 |
149 Map joinRequest = new Map(); | 149 Map joinRequest = new Map(); |
150 joinRequest["request"] = "join"; | 150 joinRequest["request"] = "join"; |
151 joinRequest["handle"] = "test1"; | 151 joinRequest["handle"] = "test1"; |
152 HttpClientConnection conn = httpClient.post("127.0.0.1", port, "/join"); | 152 HttpClientConnection conn = httpClient.post("127.0.0.1", port, "/join"); |
153 conn.onRequest = (HttpClientRequest request) { | 153 conn.onRequest = (HttpClientRequest request) { |
154 request.outputStream.writeString(JSON.stringify(joinRequest)); | 154 request.outputStream.writeString(json.stringify(joinRequest)); |
155 request.outputStream.close(); | 155 request.outputStream.close(); |
156 }; | 156 }; |
157 conn.onResponse = (HttpClientResponse response) { | 157 conn.onResponse = (HttpClientResponse response) { |
158 StringInputStream stream = new StringInputStream(response.inputStream); | 158 StringInputStream stream = new StringInputStream(response.inputStream); |
159 StringBuffer body = new StringBuffer(); | 159 StringBuffer body = new StringBuffer(); |
160 stream.onData = () => body.add(stream.read()); | 160 stream.onData = () => body.add(stream.read()); |
161 stream.onClosed = () { | 161 stream.onClosed = () { |
162 joinResponseHandler(response, body.toString()); | 162 joinResponseHandler(response, body.toString()); |
163 }; | 163 }; |
164 }; | 164 }; |
165 } | 165 } |
166 | 166 |
167 // Create a HTTP client factory. | 167 // Create a HTTP client factory. |
168 httpClient = new HttpClient(); | 168 httpClient = new HttpClient(); |
169 port = 8123; | 169 port = 8123; |
170 | 170 |
171 // Start the client by joining the chat topic. | 171 // Start the client by joining the chat topic. |
172 join(); | 172 join(); |
173 } | 173 } |
174 | 174 |
175 int messagesToSend; | 175 int messagesToSend; |
176 bool verbose; | 176 bool verbose; |
177 } | 177 } |
178 | 178 |
179 | 179 |
180 void main () { | 180 void main () { |
181 ChatStressClient stresser = new ChatStressClient(); | 181 ChatStressClient stresser = new ChatStressClient(); |
182 stresser.run(); | 182 stresser.run(); |
183 } | 183 } |
OLD | NEW |