Skip to content

Instantly share code, notes, and snippets.

@cawa-93
Last active February 4, 2020 11:55
Show Gist options
  • Save cawa-93/20332d77ec4af35564771b0b467fb0e2 to your computer and use it in GitHub Desktop.
Save cawa-93/20332d77ec4af35564771b0b467fb0e2 to your computer and use it in GitHub Desktop.
Тестирование работы V8 — https://habr.com/ru/post/486162/
module.exports = function(keys) {
for (let key of keys) this[key] = 42;
}
const createObject = require('./createObject.js');
const getKeys = require('./getKeys.js');
const {performance} = require('perf_hooks');
module.exports = function dynamicTest(length = 1000) {
for (let i = 0; i < length; i++) {
const entries = getKeys();
performance.mark('start');
const obj = new createObject(entries);
performance.mark('end');
performance.measure(`${length}`, 'start', 'end');
}
};
const makeid = require('./makeid.js');
module.exports = function getKeys(length = 20) {
let keys = [];
for (let i = 0; i < length; i++) {
keys[i] = makeid();
}
return keys;
}
const {promises} = require('fs');
const path = require('path');
const {fork} = require('child_process');
function getPromise(fork) {
return new Promise((resolve, reject) => {
fork.on('exit', code => {
if (code) {
reject(code);
} else {
resolve();
}
});
});
}
let res = {};
function onData({iterations, time, type}) {
console.log({type, iterations, time});
if (!res[iterations]) {
res[iterations] = {[type]: time};
return;
}
res[iterations][type] = time;
}
async function worker(start, end, step, type) {
for (let i = start; i <= end; i += step) {
const child = fork('./test.js');
child.on('message', onData);
const promise = getPromise(child);
child.send({count: i, type});
await promise;
}
}
(async () => {
await worker(100, 10_000, 100, 'dynamic');
await worker(100, 10_000, 100, 'mixed');
await worker(100, 10_000, 100, 'static');
const rows = [];
for (let i in res) {
rows.push([i, res[i].static, res[i].mixed, res[i].dynamic].join(','));
}
const resPath = path.resolve(__dirname, 'result.csv');
await promises.writeFile(resPath, rows.join('\n'), {flag: 'w', encoding: 'utf8'});
})();
module.exports = function makeid(length = 20) {
let result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzЙЦУКЕНГШЩЗХЪЁЭЖДЛОРПАВЫФЯЧСМИТЬБЮйцукенгшщзхъёэждлорпавыфячсмитьбю1234567890';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
const createObject = require('./createObject.js');
const getKeys = require('./getKeys.js');
const {performance} = require('perf_hooks');
module.exports = function mixedTest(length = 1000) {
const staticKeys = getKeys(10);
for (let i = 0; i < length; i++) {
const keys = [...staticKeys, getKeys(10)];
performance.mark('start');
const obj = new createObject(keys);
performance.mark('end');
performance.measure(`${length}`, 'start', 'end');
}
};
1 0.070516 0.074512 0.078131
2 0.020479 0.03387 0.057106
3 0.009605 0.017182 0.052582
4 0.007616 0.01342 0.044155
5 0.007051 0.015202 0.042519
6 0.006862 0.011752 0.048664
7 0.007956 0.011936 0.043132
8 0.006844 0.01076 0.042017
9 0.007012 0.011615 0.043953
10 0.007691 0.012169 0.04688
11 0.007136 0.010599 0.063022
12 0.009965 0.013751 0.043428
13 0.006768 0.010658 0.040818
14 0.006374 0.010481 0.064483
15 0.006754 0.01126 0.047264
16 0.00649 0.010241 0.041625
17 0.006418 0.010402 0.047642
18 0.006423 0.010414 0.044126
19 0.006504 0.010284 0.044458
20 0.006577 0.013739 0.086163
21 0.00627 0.010542 0.043992
22 0.006734 0.010396 0.043683
23 0.006606 0.010394 0.060491
24 0.006275 0.010369 0.04887
25 0.006137 0.010769 0.046446
26 0.006625 0.0138 0.040876
27 0.006063 0.02414 0.048217
28 0.006204 0.041077 0.044125
29 0.00645 0.014503 0.041934
30 0.006209 0.012044 0.045942
31 0.006031 0.015589 0.039576
32 0.006267 0.011065 0.039907
33 0.006434 0.010771 0.049029
34 0.006162 0.014278 0.043955
35 0.006402 0.011084 0.039943
36 0.006419 0.010562 0.048466
37 0.006015 0.010607 0.039335
38 0.006581 0.014728 0.044637
39 0.006055 0.010574 0.038907
40 0.006094 0.010221 0.037453
41 0.006022 0.010699 0.068153
42 0.006065 0.014031 0.038619
43 0.005999 0.010205 0.038532
44 0.006175 0.013359 0.043943
45 0.00611 0.010492 0.038878
46 0.006448 0.027557 0.041499
47 0.006001 0.013473 0.041018
48 0.006276 0.011702 0.038071
49 0.006049 0.015167 0.04198
50 0.006247 0.010455 0.041792
51 0.006173 0.010156 0.03814
52 0.00616 0.010627 0.042273
53 0.006551 0.010762 0.04575
54 0.006689 0.010018 0.043447
55 0.006077 0.010257 0.040493
56 0.006056 0.013623 0.045899
57 0.005943 0.012145 0.046196
58 0.006345 0.010042 0.043093
59 0.006348 0.010504 0.044705
60 0.00645 0.01095 0.046662
61 0.00623 0.013279 0.045054
62 0.006058 0.010696 0.04844
63 0.005912 0.010148 0.040374
64 0.006226 0.014169 0.047138
65 0.006155 0.014124 0.044725
66 0.00634 0.010195 0.041987
67 0.006393 0.010631 0.043145
68 0.006329 0.01006 0.045764
69 0.006131 0.010202 0.040506
70 0.006211 0.009904 0.043146
71 0.00635 0.010045 0.04558
72 0.006313 0.012921 0.044262
73 0.006262 0.011154 0.045277
74 0.006423 0.010128 0.04262
75 0.006 0.01073 0.044064
76 0.006062 0.009976 0.046093
77 0.005908 0.01008 0.043238
78 0.006218 0.009569 0.039595
79 0.006118 0.010088 0.045791
80 0.006443 0.010004 0.046145
81 0.00611 0.010205 0.043394
82 0.005986 0.010184 0.043677
83 0.005899 0.009571 0.045742
84 0.005929 0.01266 0.045922
85 0.005944 0.009807 0.040106
86 0.006694 0.009545 0.061924
87 0.005987 0.009924 0.046457
88 0.005928 0.009695 0.04199
89 0.006082 0.009702 0.046261
90 0.005872 0.00986 0.039748
91 0.005982 0.009889 0.038427
92 0.005895 0.00978 0.044389
93 0.006053 0.013343 0.039946
94 0.005971 0.011072 0.03962
95 0.006 0.009652 0.045866
96 0.005916 0.010476 0.038063
97 0.006083 0.010548 0.04228
98 0.005793 0.009845 0.042403
99 0.006056 0.009727 0.068
100 0.005919 0.009484 0.097835
100 0.746334 1.28602 4.749314
200 1.367427 2.3701 8.68656
300 2.516429 3.587406 14.475089
400 3.094934 4.766019 17.590636
500 4.050165 5.967468 21.27615
600 4.700201 7.146879 25.526627
700 5.078174 8.512543 30.168137
800 4.82584 9.671826 33.899309
900 5.312768 10.824226 37.934505
1000 6.335683 12.056051 39.113308
1100 7.085298 12.41659 43.738001
1200 6.966162 14.979097 48.206768
1300 8.045883 14.613864 53.556167
1400 8.57082 16.491824 57.199703
1500 8.957278 17.880392 59.348648
1600 9.483584 18.071832 64.718858
1700 10.148022 18.850772 71.58447
1800 10.41871 19.829112 75.16745
1900 11.44134 20.068933 80.241606
2000 11.210945 22.095337 83.294692
2100 11.825698 22.778076 86.617296
2200 12.842728 23.717162 91.657715
2300 12.211329 24.472508 93.570758
2400 12.084739 24.895062 96.196754
2500 11.967634 26.910906 102.088493
2600 13.457991 27.831991 105.812472
2700 15.081118 28.010473 126.713938
2800 15.129705 28.164192 151.925003
2900 15.797326 28.981119 129.303417
3000 16.406157 32.823417 152.482452
3100 16.454162 31.46171 158.807887
3200 16.858241 32.644069 132.169779
3300 17.149584 32.149721 136.451894
3400 18.111303 33.965343 169.015973
3500 18.503657 34.367465 142.175513
3600 15.941716 35.957534 148.437537
3700 16.357307 36.109945 151.691485
3800 17.162691 36.531456 158.63412
3900 18.809379 41.623622 159.575129
4000 18.406013 39.63293 163.566926
4100 20.675249 55.775127 168.030241
4200 19.856078 48.395186 172.826546
4300 20.612909 79.505245 182.16897
4400 20.300609 61.090867 188.181415
4500 20.654471 54.548554 206.812246
4600 20.505822 67.101701 190.751202
4700 20.254685 47.057305 195.252113
4800 19.279691 56.526382 201.754623
4900 20.198436 46.422286 201.528683
5000 21.701091 47.008542 210.057374
5100 23.45207 51.927668 215.20178
5200 22.294861 46.974469 213.856039
5300 23.016526 48.73407 222.10545
5400 24.083761 48.697516 217.843559
5500 26.948069 49.479517 227.511143
5600 29.870365 50.066914 225.342229
5700 27.283051 53.294128 236.454041
5800 25.184811 53.026884 249.019075
5900 25.721527 52.461297 251.653526
6000 27.58663 61.229427 246.522381
6100 26.35897 57.006252 256.445066
6200 26.94551 62.549175 262.948402
6300 26.658132 61.517989 259.249039
6400 26.916074 78.661619 270.334309
6500 27.218135 80.236457 270.948293
6600 27.84208 73.981696 275.419066
6700 28.537935 59.952568 288.423357
6800 29.630254 61.821303 294.49305
6900 29.349273 67.720427 287.394301
7000 28.89699 67.56924 296.622703
7100 29.175293 63.46472 304.723181
7200 29.520907 65.659488 312.744592
7300 30.044577 81.780671 315.794409
7400 30.950321 89.544344 320.921789
7500 31.596396 92.506289 314.001592
7600 31.361302 104.521908 321.744349
7700 30.107738 93.589571 326.175583
7800 31.150362 75.893319 336.930927
7900 32.058823 86.320825 341.49997
8000 32.641055 86.688438 355.843624
8100 33.249796 82.033383 350.565772
8200 35.155293 73.758274 358.853134
8300 33.166279 72.924312 367.489579
8400 35.830797 76.658705 374.630028
8500 34.259911 90.109449 370.657868
8600 33.855245 110.521979 370.300604
8700 35.132415 117.446192 377.795957
8800 36.796464 107.185078 387.742946
8900 36.149213 115.338647 381.470536
9000 36.564622 118.842288 385.727076
9100 35.810603 123.174832 391.647376
9200 38.526223 106.360383 393.981079
9300 38.011408 124.443577 411.950764
9400 37.407888 124.962288 434.616664
9500 37.538268 108.462239 423.044489
9600 37.045504 103.176439 412.101761
9700 39.163105 107.168415 413.499956
9800 38.923913 87.203774 431.009231
9900 39.881353 96.036974 435.935511
10000 38.184989 101.840954 428.29386
const getKeys = require('./getKeys.js');
const {performance} = require('perf_hooks');
const createObject = require('./createObject.js');
module.exports = function staticTest(length = 1000) {
const keys = getKeys();
for (let i = 0; i < length; i++) {
performance.mark(`start`);
const obj = new createObject(keys);
performance.mark(`end`);
performance.measure(`${length}`, `start`, `end`);
}
};
const {PerformanceObserver} = require('perf_hooks');
process.on('message', ({count, type}) => {
const test =
type === 'static'
? require('./staticTest.js')
: type === 'mixed'
? require('./mixedTest.js') : require('./dynamicTest.js');
let totalTime = 0;
const obs = new PerformanceObserver((items) => {
totalTime += items.getEntries()[0].duration;
});
obs.observe({entryTypes: ['measure']});
test(count);
process.send({iterations: count, time: totalTime, type});
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment