1 | 'use strict';
|
2 |
|
3 | const spawn = require('child_process').spawn;
|
4 | const spawnSync = require('child_process').spawnSync;
|
5 | const thenRequest = require('then-request');
|
6 | const syncRequest = require('../');
|
7 |
|
8 | const server = spawn(process.execPath, [require.resolve('./benchmark-server.js')]);
|
9 |
|
10 | setTimeout(() => {
|
11 | let asyncDuration, syncDuration;
|
12 | let ready = Promise.resolve(null);
|
13 | const startAsync = Date.now();
|
14 | for (let i = 0; i < 1000; i++) {
|
15 | ready = ready.then(function () {
|
16 | return thenRequest('get', 'http://localhost:3045');
|
17 | });
|
18 | }
|
19 | ready.then(function () {
|
20 | const endAsync = Date.now();
|
21 | asyncDuration = endAsync - startAsync;
|
22 | console.log('1000 async requests in: ' + asyncDuration);
|
23 | const startSync = Date.now();
|
24 | for (let i = 0; i < 500; i++) {
|
25 | syncRequest('get', 'http://localhost:3045');
|
26 | }
|
27 | const endSync = Date.now();
|
28 | syncDuration = endSync - startSync;
|
29 | console.log('1000 sync requests in: ' + syncDuration);
|
30 | }).then(() => {
|
31 | server.kill();
|
32 | if (syncDuration > (asyncDuration * 10)) {
|
33 | console.error('This is more than 10 times slower than using async requests, that is not good enough.');
|
34 | process.exit(1);
|
35 | }
|
36 | process.exit(0);
|
37 | }, function (err) {
|
38 | console.error(err.stack);
|
39 | process.exit(1);
|
40 | });
|
41 | ready = null;
|
42 | }, 1000);
|