scale.js
6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
var config = {
IP: require('ip').address(),
port: 8765,
servers: 1,
browsers: 2,
each: 2500,
burst: 1, // do not go below 1!
wait: 1,
route: {
'/': __dirname + '/index.html',
'/gun.js': __dirname + '/../../gun.js',
'/jquery.js': __dirname + '/../../examples/jquery.js',
'/sea.js': __dirname + '/../../sea.js'
}
}
var fs = require('fs');
var server = require('https').createServer({
key: fs.readFileSync(__dirname+'/../https/server.key'),
cert: fs.readFileSync(__dirname+'/../https/server.crt'),
ca: fs.readFileSync(__dirname+'/../https/ca.crt'),
requestCert: true,
rejectUnauthorized: false
});
var panic = require('panic-server');
panic.server(server).on('request', function(req, res){
config.route[req.url] && require('fs').createReadStream(config.route[req.url]).pipe(res);
}).listen(config.port);
var clients = panic.clients;
var manager = require('panic-manager')();
manager.start({
clients: Array(config.servers).fill().map(function(u, i){
return {
type: 'node',
port: config.port + (i + 1)
}
}),
panic: 'https://' + config.IP + ':' + config.port
});
// Now lets divide our clients into "servers" and "browsers".
var servers = clients.filter('Node.js');
var browsers = clients.excluding(servers);
// Sweet! Now we can start the tests.
// PANIC works with Mocha and other testing libraries!
// So it is easy to use PANIC.
describe("Stress test GUN with SEA users causing PANIC!", function(){
this.timeout(10 * 60 * 1000);
it("Servers have joined!", function(){
return servers.atLeast(config.servers);
});
it("GUN has spawned!", function(){
// Once they are, we need to actually spin up the gun server.
var tests = [], i = 0;
servers.each(function(client){
// for each server peer, tell it to run this code:
tests.push(client.run(function(test){
// NOTE: Despite the fact this LOOKS like we're in a closure...
// it is not! This code is actually getting run
// in a DIFFERENT machine or process!
var env = test.props;
// As a result, we have to manually pass it scope.
test.async();
// Clean up from previous test.
try{ require('fs').unlinkSync(env.i+'data') }catch(e){ console.log("!!! WARNING !!!! MUST MANUALLY REMOVE OLD DATA!!!!, e") }
var purl = 'https://'+env.config.IP+':'+env.config.port;
require('gun/test/https/test')(env.config.port + env.i, env.i+'data', function(){
// This server peer is now done with the test!
// It has successfully launched.
test.done();
}, function(file){
file = file.toString();
if(0 >= file.indexOf('<script src="/gun.js"></script>')){ return }
file = file.replace('<script src="/gun.js"></script>',
"<script src='"+purl+"/panic.js'></script><script>panic.server('"+purl+"')</script><script src='/gun.js'></script><script>localStorage.clear();sessionStorage.clear();</script>");
return file;
});
}, {i: i += 1, config: config}));
});
// NOW, this is very important:
// Do not proceed to the next test until
// every single server (in different machines/processes)
// have ALL successfully launched.
return Promise.all(tests);
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN https://"+ config.IP +":"+ (config.port+1) +"/contact/index.html IN "+ config.browsers +" BROWSER(S)!");
return browsers.atLeast(config.browsers);
});
it("Use UI to create user.", function(){
var tests = [], ids = {}, i = 0;
browsers.each(function(client, id){
ids[id] = 1;
});
browsers.each(function(client, id){
tests.push(client.run(function(test){
var env = test.props;
test.async();
localStorage.clear();sessionStorage.clear();
$('#sign').find('input').first().val(env.id);
setTimeout(function(){
$('#sign').find('input').last().val('pass'+env.id+'phrase');
},750 * 1);
setTimeout(function(){
$('#sign').find('button').last().trigger('click');
},750 * 2);
setTimeout(function(){
c.tell("Wait until we are logged in...");
test.done();
},750 * 3);
}, {i: i += 1, id: id, ids: ids, config: config}));
});
return Promise.all(tests);
});
it("Load user UI once logged in.", function(){
var tests = [], ids = {}, i = 0;
browsers.each(function(client, id){
ids[id] = 1;
});
browsers.each(function(client, id){
tests.push(client.run(function(test){
var env = test.props;
test.async();
setTimeout(function waitlogin(){
console.log(user._.pub);
if(!user._.pub){ return setTimeout(waitlogin, 500) }
test.done();
}, 750);
}, {i: i += 1, id: id, ids: ids, config: config}));
});
return Promise.all(tests);
});
it("Update user's name!", function(){
var tests = [], ids = {}, i = 0;
browsers.each(function(client, id){
ids[id] = 1;
});
browsers.each(function(client, id){
tests.push(client.run(function(test){
var env = test.props;
test.async();
setTimeout(function(){
location.hash = 'person/' + user._.pub;
var $name = $('#person').find('h2').first(), t = 250;
setTimeout(function(){
$name.text(env.id.slice(0,1));
$name.trigger('keyup');
},t * 1);
setTimeout(function(){
$name.text(env.id.slice(0,2));
$name.trigger('keyup');
},t * 2);
setTimeout(function(){
$name.text(env.id.slice(0,3));
$name.trigger('keyup');
},t * 3);
setTimeout(function(){
$name.text(env.id.slice(0,4));
$name.trigger('keyup');
},t * 4);
setTimeout(function(){
$name.text(env.id.slice(0,5));
$name.trigger('keyup');
},t * 5);
setTimeout(function(){
location.hash = 'people';
test.done();
}, 2000);
}, 3000 * env.i);
}, {i: i += 1, id: id, ids: ids, config: config}));
});
return Promise.all(tests);
});
it("Reset", function(){
var tests = [], ids = {}, i = 0;
browsers.each(function(client, id){
ids[id] = 1;
});
browsers.each(function(client, id){
tests.push(client.run(function(test){
localStorage.clear();sessionStorage.clear();
setTimeout(function(){
location.hash = '#sign';
location.reload();
}, 9000);
}, {i: i += 1, id: id, ids: ids, config: config}));
});
return Promise.all(tests);
});
/* MODEL TEST
it("Browsers initialized gun!", function(){
var tests = [], ids = {}, i = 0;
browsers.each(function(client, id){
ids[id] = 1;
});
browsers.each(function(client, id){
tests.push(client.run(function(test){
// code here
}, {i: i += 1, id: id, ids: ids, config: config}));
});
return Promise.all(tests);
});
*/
it("All finished!", function(done){
console.log("Done! Cleaning things up...");
setTimeout(function(){
done();
}, 2000);
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
return servers.run(function(){
process.exit();
});
});
});