-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstress-test.js
214 lines (138 loc) · 4.3 KB
/
stress-test.js
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
require('dotenv-up')({
override : false,
deep : 1,
}, true, 'tests');
const fs = require('fs');
const path = require('path');
const querystring = require('querystring');
const log = require('inspc');
const delay = require('nlab/delay');
const trim = require('nlab/trim');
const pdfgen = require('./lib/pdf-generator');
const sha256 = require('nlab/sha256');
const dir = path.resolve(__dirname, 'pdfs-generated');
const fakedb = (function () {
const file = path.resolve(__dirname, 'fakedb.json');
function getall(deletefile = false) {
let obj = {};
if (fs.existsSync(file)) { // warning: if under dirfile there will be broken link (pointing to something nonexisting) then this function will return false even if link DO EXIST but it's broken
try {
obj = JSON.parse(fs.readFileSync(file, 'utf8').toString());
}
catch (e) {
log.dump({
json_parse_file: file,
error: e
});
}
deletefile && fs.unlinkSync(file);
}
return obj;
}
return {
get: url => {
const obj = getall();
if ( ! url ) {
return obj;
}
return obj[url];
},
set: async (url, data) => {
const obj = getall();
obj[url] = data;
fs.writeFileSync(file, JSON.stringify(obj, null, 4));
}
};
}());
pdfgen.setup({
server : process.env.PROTECTED_PDF_GENERATOR_ENDPOINT,
user : process.env.PROTECTED_PDF_GENERATOR_BASIC_USER,
pass : process.env.PROTECTED_PDF_GENERATOR_BASIC_PASS,
timeoutms : 20 * 1000,
time_tolerance_sec : 3,
dir,
urlgenerate : url => {
const r = decodeURIComponent(decodeURIComponent(url));
const slug = r.split('?')[0].split('/').pop();
const p = slug.split(/^(.)(.*)$/).splice(1,2);
p[1] += '.pdf';
return {
url,
subdir: p[0],
filename: p[1],
};
},
});
const file = path.resolve(__dirname, 'stress-test.txt');
if ( ! fs.existsSync(file) ) { // warning: if under dirfile there will be broken link (pointing to something nonexisting) then this function will return false even if link DO EXIST but it's broken
throw new Error(`file '${file}' doesn't exists`);
}
let content = fs.readFileSync(file, 'utf8').toString();
const reg = /^https?:\/\//
content = content.split("\n").map(a => trim(a)).filter(Boolean).filter(a => reg.test(a));
const next = (function (c) {
let i = -1;
return () => {
i += 1;
if ( ! content[i] ) {
i = 0;
}
return content[i];
}
}(content));
const maxconcurrent = 3;
let buff = [];
const run = async target => {
try {
console.log("");
process.stdout.write('buff: ');
buff.forEach(n => {
process.stdout.write(String(content.indexOf(n) + 1) + ' ');
});
const data = await pdfgen({
last_db_mtime_sec_utc: (function (db = {}) {
const utc = db.mtime || (new Date()).toISOString();
return parseInt(parseInt((new Date(utc)).getTime(), 10) / 1000, 10);
}(fakedb.get(target))),
urlgenerateargs: [
target
],
debugfn: data => {
const {
url,
...rest
} = data;
fakedb.set(url, rest);
}
});
// if (data.mode !== 'not-expired') {
log.dump({
stress_test_file_generated: data,
}, 20)
// }
}
catch (e) {
log.dump({
stress_general_error: e,
})
}
process.stdout.write('-');
buff = buff.filter(u => u !== target);
// await delay(500);
// trigger();
};
function trigger() {
for ( let i = 0, l = content.length ; i < l ; i += 1 ) {
if (buff.length >= maxconcurrent) {
break;
}
const n = next();
if (buff.find(b => b === n)) {
break;
}
process.stdout.write('+')
buff.push(n);
run(n);
}
}
trigger();