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' as json; | 7 import 'dart:json' as json; |
8 | 8 |
9 | 9 |
10 class ChatStressClient { | 10 class ChatStressClient { |
(...skipping 26 matching lines...) Expand all Loading... |
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.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 HttpClientConnection conn = httpClient.post("127.0.0.1", port, "/leave"); | 53 httpClient.post("127.0.0.1", port, "/leave") |
54 conn.onRequest = (HttpClientRequest request) { | 54 .then((HttpClientRequest request) { |
55 request.outputStream.writeString(json.stringify(leaveRequest)); | 55 request.addString(json.stringify(leaveRequest)); |
56 request.outputStream.close(); | 56 return request.close(); |
57 }; | 57 }) |
58 conn.onResponse = (HttpClientResponse response) { | 58 .then((HttpClientResponse response) { |
59 StringInputStream stream = new StringInputStream(response.inputStream); | 59 StringBuffer body = new StringBuffer(); |
60 StringBuffer body = new StringBuffer(); | 60 response.listen( |
61 stream.onData = () => body.add(stream.read()); | 61 (data) => body.add(new String.fromCharCodes(data)), |
62 stream.onClosed = () { | 62 onDone: () => leaveResponseHandler(response, body.toString())); |
63 leaveResponseHandler(response, body.toString()); | 63 }); |
64 }; | |
65 }; | |
66 } | 64 } |
67 | 65 |
68 var sendMessage; | 66 var sendMessage; |
69 void receive() { | 67 void receive() { |
70 void receiveResponseHandler(HttpClientResponse response, String data) { | 68 void receiveResponseHandler(HttpClientResponse response, String data) { |
71 var responseData = parseResponse(response, data, "receive"); | 69 var responseData = parseResponse(response, data, "receive"); |
72 if (responseData == null) return; | 70 if (responseData == null) return; |
73 if (responseData["disconnect"] == true) return; | 71 if (responseData["disconnect"] == true) return; |
74 | 72 |
75 sendMessage(); | 73 sendMessage(); |
76 } | 74 } |
77 | 75 |
78 Map messageRequest = new Map(); | 76 Map messageRequest = new Map(); |
79 messageRequest["request"] = "receive"; | 77 messageRequest["request"] = "receive"; |
80 messageRequest["sessionId"] = sessionId; | 78 messageRequest["sessionId"] = sessionId; |
81 messageRequest["nextMessage"] = receiveMessageCount; | 79 messageRequest["nextMessage"] = receiveMessageCount; |
82 messageRequest["maxMessages"] = 100; | 80 messageRequest["maxMessages"] = 100; |
83 HttpClientConnection conn = | 81 httpClient.post("127.0.0.1", port, "/receive") |
84 httpClient.post("127.0.0.1", port, "/receive"); | 82 .then((HttpClientRequest request) { |
85 conn.onRequest = (HttpClientRequest request) { | 83 request.addString(json.stringify(messageRequest)); |
86 request.outputStream.writeString(json.stringify(messageRequest)); | 84 return request.close(); |
87 request.outputStream.close(); | 85 }) |
88 }; | 86 .then((HttpClientResponse response) { |
89 conn.onResponse = (HttpClientResponse response) { | 87 StringBuffer body = new StringBuffer(); |
90 StringInputStream stream = new StringInputStream(response.inputStream); | 88 response.listen( |
91 StringBuffer body = new StringBuffer(); | 89 (data) => body.add(new String.fromCharCodes(data)), |
92 stream.onData = () => body.add(stream.read()); | 90 onDone: () => receiveResponseHandler(response, body.toString())); |
93 stream.onClosed = () { | 91 }); |
94 receiveResponseHandler(response, body.toString()); | |
95 }; | |
96 }; | |
97 } | 92 } |
98 | 93 |
99 sendMessage = () { | 94 sendMessage = () { |
100 void sendResponseHandler(HttpClientResponse response, String data) { | 95 void sendResponseHandler(HttpClientResponse response, String data) { |
101 var responseData = parseResponse(response, data, "message"); | 96 var responseData = parseResponse(response, data, "message"); |
102 if (responseData == null) return; | 97 if (responseData == null) return; |
103 | 98 |
104 sendMessageCount++; | 99 sendMessageCount++; |
105 if (verbose) { | 100 if (verbose) { |
106 if (sendMessageCount % 10 == 0) { | 101 if (sendMessageCount % 10 == 0) { |
107 print("$sendMessageCount messages"); | 102 print("$sendMessageCount messages"); |
108 } | 103 } |
109 } | 104 } |
110 if (sendMessageCount < messagesToSend) { | 105 if (sendMessageCount < messagesToSend) { |
111 receive(); | 106 receive(); |
112 } else { | 107 } else { |
113 leave(); | 108 leave(); |
114 } | 109 } |
115 } | 110 } |
116 | 111 |
117 Map messageRequest = new Map(); | 112 Map messageRequest = new Map(); |
118 messageRequest["request"] = "message"; | 113 messageRequest["request"] = "message"; |
119 messageRequest["sessionId"] = sessionId; | 114 messageRequest["sessionId"] = sessionId; |
120 messageRequest["message"] = "message $sendMessageCount"; | 115 messageRequest["message"] = "message $sendMessageCount"; |
121 HttpClientConnection conn = | 116 httpClient.post("127.0.0.1", port, "/message") |
122 httpClient.post("127.0.0.1", port, "/message"); | 117 .then((HttpClientRequest request) { |
123 conn.onRequest = (HttpClientRequest request) { | 118 request.addString(json.stringify(messageRequest)); |
124 request.outputStream.writeString(json.stringify(messageRequest)); | 119 return request.close(); |
125 request.outputStream.close(); | 120 }) |
126 }; | 121 .then((HttpClientResponse response) { |
127 conn.onResponse = (HttpClientResponse response) { | 122 StringBuffer body = new StringBuffer(); |
128 StringInputStream stream = new StringInputStream(response.inputStream); | 123 response.listen( |
129 StringBuffer body = new StringBuffer(); | 124 (data) => body.add(new String.fromCharCodes(data)), |
130 stream.onData = () => body.add(stream.read()); | 125 onDone: () => sendResponseHandler(response, body.toString())); |
131 stream.onClosed = () { | 126 }); |
132 sendResponseHandler(response, body.toString()); | |
133 }; | |
134 }; | |
135 }; | 127 }; |
136 | 128 |
137 void join() { | 129 void join() { |
138 void joinResponseHandler(HttpClientResponse response, String data) { | 130 void joinResponseHandler(HttpClientResponse response, String data) { |
139 var responseData = parseResponse(response, data, "join"); | 131 var responseData = parseResponse(response, data, "join"); |
140 if (responseData == null) return; | 132 if (responseData == null) return; |
141 sessionId = responseData["sessionId"]; | 133 sessionId = responseData["sessionId"]; |
142 | 134 |
143 messageCount = 0; | 135 messageCount = 0; |
144 sendMessageCount = 0; | 136 sendMessageCount = 0; |
145 receiveMessageCount = 0; | 137 receiveMessageCount = 0; |
146 sendMessage(); | 138 sendMessage(); |
147 } | 139 } |
148 | 140 |
149 Map joinRequest = new Map(); | 141 Map joinRequest = new Map(); |
150 joinRequest["request"] = "join"; | 142 joinRequest["request"] = "join"; |
151 joinRequest["handle"] = "test1"; | 143 joinRequest["handle"] = "test1"; |
152 HttpClientConnection conn = httpClient.post("127.0.0.1", port, "/join"); | 144 httpClient.post("127.0.0.1", port, "/join") |
153 conn.onRequest = (HttpClientRequest request) { | 145 .then((HttpClientRequest request) { |
154 request.outputStream.writeString(json.stringify(joinRequest)); | 146 request.addString(json.stringify(joinRequest)); |
155 request.outputStream.close(); | 147 return request.close(); |
156 }; | 148 }) |
157 conn.onResponse = (HttpClientResponse response) { | 149 .then((HttpClientResponse response) { |
158 StringInputStream stream = new StringInputStream(response.inputStream); | 150 StringBuffer body = new StringBuffer(); |
159 StringBuffer body = new StringBuffer(); | 151 response.listen( |
160 stream.onData = () => body.add(stream.read()); | 152 (data) => body.add(new String.fromCharCodes(data)), |
161 stream.onClosed = () { | 153 onDone: () => joinResponseHandler(response, body.toString())); |
162 joinResponseHandler(response, body.toString()); | 154 }); |
163 }; | |
164 }; | |
165 } | 155 } |
166 | 156 |
167 // Create a HTTP client factory. | 157 // Create a HTTP client factory. |
168 httpClient = new HttpClient(); | 158 httpClient = new HttpClient(); |
169 port = 8123; | 159 port = 8123; |
170 | 160 |
171 // Start the client by joining the chat topic. | 161 // Start the client by joining the chat topic. |
172 join(); | 162 join(); |
173 } | 163 } |
174 | 164 |
175 int messagesToSend; | 165 int messagesToSend; |
176 bool verbose; | 166 bool verbose; |
177 } | 167 } |
178 | 168 |
179 | 169 |
180 void main () { | 170 void main () { |
181 ChatStressClient stresser = new ChatStressClient(); | 171 ChatStressClient stresser = new ChatStressClient(); |
182 stresser.run(); | 172 stresser.run(); |
183 } | 173 } |
OLD | NEW |