| 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:convert"; | 7 import "dart:convert"; |
| 8 | 8 |
| 9 | 9 |
| 10 class ChatStressClient { | 10 class ChatStressClient { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 void leave() { | 45 void leave() { |
| 46 void leaveResponseHandler(HttpClientResponse response, String data) { | 46 void leaveResponseHandler(HttpClientResponse response, String data) { |
| 47 httpClient.close(); | 47 httpClient.close(); |
| 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 httpClient.post("127.0.0.1", port, "/leave") | 53 httpClient.post("127.0.0.1", port, "/leave") |
| 54 .then((HttpClientRequest request) { | 54 .then((HttpClientRequest request) { |
| 55 request.addString(JSON.encode(leaveRequest)); | 55 request.write(JSON.encode(leaveRequest)); |
| 56 return request.close(); | 56 return request.close(); |
| 57 }) | 57 }) |
| 58 .then((HttpClientResponse response) { | 58 .then((HttpClientResponse response) { |
| 59 StringBuffer body = new StringBuffer(); | 59 StringBuffer body = new StringBuffer(); |
| 60 response.listen( | 60 response.listen( |
| 61 (data) => body.write(new String.fromCharCodes(data)), | 61 (data) => body.write(new String.fromCharCodes(data)), |
| 62 onDone: () => leaveResponseHandler(response, body.toString())); | 62 onDone: () => leaveResponseHandler(response, body.toString())); |
| 63 }); | 63 }); |
| 64 } | 64 } |
| 65 | 65 |
| 66 var sendMessage; | 66 var sendMessage; |
| 67 void receive() { | 67 void receive() { |
| 68 void receiveResponseHandler(HttpClientResponse response, String data) { | 68 void receiveResponseHandler(HttpClientResponse response, String data) { |
| 69 var responseData = parseResponse(response, data, "receive"); | 69 var responseData = parseResponse(response, data, "receive"); |
| 70 if (responseData == null) return; | 70 if (responseData == null) return; |
| 71 if (responseData["disconnect"] == true) return; | 71 if (responseData["disconnect"] == true) return; |
| 72 | 72 |
| 73 sendMessage(); | 73 sendMessage(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 Map messageRequest = new Map(); | 76 Map messageRequest = new Map(); |
| 77 messageRequest["request"] = "receive"; | 77 messageRequest["request"] = "receive"; |
| 78 messageRequest["sessionId"] = sessionId; | 78 messageRequest["sessionId"] = sessionId; |
| 79 messageRequest["nextMessage"] = receiveMessageCount; | 79 messageRequest["nextMessage"] = receiveMessageCount; |
| 80 messageRequest["maxMessages"] = 100; | 80 messageRequest["maxMessages"] = 100; |
| 81 httpClient.post("127.0.0.1", port, "/receive") | 81 httpClient.post("127.0.0.1", port, "/receive") |
| 82 .then((HttpClientRequest request) { | 82 .then((HttpClientRequest request) { |
| 83 request.addString(JSON.encode(messageRequest)); | 83 request.write(JSON.encode(messageRequest)); |
| 84 return request.close(); | 84 return request.close(); |
| 85 }) | 85 }) |
| 86 .then((HttpClientResponse response) { | 86 .then((HttpClientResponse response) { |
| 87 StringBuffer body = new StringBuffer(); | 87 StringBuffer body = new StringBuffer(); |
| 88 response.listen( | 88 response.listen( |
| 89 (data) => body.write(new String.fromCharCodes(data)), | 89 (data) => body.write(new String.fromCharCodes(data)), |
| 90 onDone: () => receiveResponseHandler(response, body.toString())); | 90 onDone: () => receiveResponseHandler(response, body.toString())); |
| 91 }); | 91 }); |
| 92 } | 92 } |
| 93 | 93 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 108 leave(); | 108 leave(); |
| 109 } | 109 } |
| 110 } | 110 } |
| 111 | 111 |
| 112 Map messageRequest = new Map(); | 112 Map messageRequest = new Map(); |
| 113 messageRequest["request"] = "message"; | 113 messageRequest["request"] = "message"; |
| 114 messageRequest["sessionId"] = sessionId; | 114 messageRequest["sessionId"] = sessionId; |
| 115 messageRequest["message"] = "message $sendMessageCount"; | 115 messageRequest["message"] = "message $sendMessageCount"; |
| 116 httpClient.post("127.0.0.1", port, "/message") | 116 httpClient.post("127.0.0.1", port, "/message") |
| 117 .then((HttpClientRequest request) { | 117 .then((HttpClientRequest request) { |
| 118 request.addString(JSON.encode(messageRequest)); | 118 request.write(JSON.encode(messageRequest)); |
| 119 return request.close(); | 119 return request.close(); |
| 120 }) | 120 }) |
| 121 .then((HttpClientResponse response) { | 121 .then((HttpClientResponse response) { |
| 122 StringBuffer body = new StringBuffer(); | 122 StringBuffer body = new StringBuffer(); |
| 123 response.listen( | 123 response.listen( |
| 124 (data) => body.write(new String.fromCharCodes(data)), | 124 (data) => body.write(new String.fromCharCodes(data)), |
| 125 onDone: () => sendResponseHandler(response, body.toString())); | 125 onDone: () => sendResponseHandler(response, body.toString())); |
| 126 }); | 126 }); |
| 127 }; | 127 }; |
| 128 | 128 |
| 129 void join() { | 129 void join() { |
| 130 void joinResponseHandler(HttpClientResponse response, String data) { | 130 void joinResponseHandler(HttpClientResponse response, String data) { |
| 131 var responseData = parseResponse(response, data, "join"); | 131 var responseData = parseResponse(response, data, "join"); |
| 132 if (responseData == null) return; | 132 if (responseData == null) return; |
| 133 sessionId = responseData["sessionId"]; | 133 sessionId = responseData["sessionId"]; |
| 134 | 134 |
| 135 messageCount = 0; | 135 messageCount = 0; |
| 136 sendMessageCount = 0; | 136 sendMessageCount = 0; |
| 137 receiveMessageCount = 0; | 137 receiveMessageCount = 0; |
| 138 sendMessage(); | 138 sendMessage(); |
| 139 } | 139 } |
| 140 | 140 |
| 141 Map joinRequest = new Map(); | 141 Map joinRequest = new Map(); |
| 142 joinRequest["request"] = "join"; | 142 joinRequest["request"] = "join"; |
| 143 joinRequest["handle"] = "test1"; | 143 joinRequest["handle"] = "test1"; |
| 144 httpClient.post("127.0.0.1", port, "/join") | 144 httpClient.post("127.0.0.1", port, "/join") |
| 145 .then((HttpClientRequest request) { | 145 .then((HttpClientRequest request) { |
| 146 request.addString(JSON.encode(joinRequest)); | 146 request.write(JSON.encode(joinRequest)); |
| 147 return request.close(); | 147 return request.close(); |
| 148 }) | 148 }) |
| 149 .then((HttpClientResponse response) { | 149 .then((HttpClientResponse response) { |
| 150 StringBuffer body = new StringBuffer(); | 150 StringBuffer body = new StringBuffer(); |
| 151 response.listen( | 151 response.listen( |
| 152 (data) => body.write(new String.fromCharCodes(data)), | 152 (data) => body.write(new String.fromCharCodes(data)), |
| 153 onDone: () => joinResponseHandler(response, body.toString())); | 153 onDone: () => joinResponseHandler(response, body.toString())); |
| 154 }); | 154 }); |
| 155 } | 155 } |
| 156 | 156 |
| 157 // Create a HTTP client factory. | 157 // Create a HTTP client factory. |
| 158 httpClient = new HttpClient(); | 158 httpClient = new HttpClient(); |
| 159 port = 8123; | 159 port = 8123; |
| 160 | 160 |
| 161 // Start the client by joining the chat topic. | 161 // Start the client by joining the chat topic. |
| 162 join(); | 162 join(); |
| 163 } | 163 } |
| 164 | 164 |
| 165 int messagesToSend; | 165 int messagesToSend; |
| 166 bool verbose; | 166 bool verbose; |
| 167 } | 167 } |
| 168 | 168 |
| 169 | 169 |
| 170 void main () { | 170 void main () { |
| 171 ChatStressClient stresser = new ChatStressClient(); | 171 ChatStressClient stresser = new ChatStressClient(); |
| 172 stresser.run(); | 172 stresser.run(); |
| 173 } | 173 } |
| OLD | NEW |