-
-
Notifications
You must be signed in to change notification settings - Fork 630
/
Copy pathbench-insert-select-parallel.js
81 lines (72 loc) · 2.08 KB
/
bench-insert-select-parallel.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
'use strict';
const common = require('../test/common');
const connection = common.createConnection();
const table = 'insert_test';
//const text = "本日は晴天なり";
const text = 'test abc xyz';
connection.query(`drop table ${table}`).on('error', () => {});
connection.query(
[
`CREATE TABLE \`${table}\` (`,
'`id` int(11) unsigned NOT NULL AUTO_INCREMENT,',
'`title` varchar(255) NOT NULL,',
'PRIMARY KEY (`id`)',
') ENGINE=InnoDB DEFAULT CHARSET=utf8',
].join('\n')
);
function benchmarkInsert(numLeft, callback) {
connection.query(`INSERT INTO ${table} SET title="${text}"`, (err) => {
if (err) throw err;
if (numLeft > 1) benchmarkInsert(numLeft - 1, callback);
else callback();
});
}
function benchmarkInserts(n, cb) {
const numInsert = 50000;
const start = process.hrtime();
benchmarkInsert(numInsert, () => {
const end = process.hrtime();
const diff = common.hrdiff(start, end);
console.log(`${(numInsert * 1e9) / diff} inserts/sec`);
if (n > 1) benchmarkInserts(n - 1, cb);
else cb();
});
}
function benchmarkParallelSelects(n, size, cb) {
const start = process.hrtime();
let numRunning = 0;
function commandDone() {
console.log(numRunning);
numRunning--;
if (numRunning > 0) return;
const end = process.hrtime();
const diff = common.hrdiff(start, end);
console.log(
`${size} rows: ${(n * 1e9) / diff} results/sec, ${(size * n * 1e9) / diff} rows/sec`
);
cb();
}
const connections = new Array(n);
for (let i = 0; i < n; ++i) {
numRunning++;
connections[i] = common.createConnection();
const cmd = connections[i].execute(
`select * from ${table} limit ${size}`,
[]
);
cmd.on('end', commandDone);
}
}
module.exports = function (done) {
const testStart = process.hrtime();
benchmarkInserts(1, () => {
benchmarkParallelSelects(8, 50000, () => {
const testEnd = process.hrtime();
console.log('total time: ', common.hrdiff(testStart, testEnd) / 1e9);
if (done) done();
});
});
};
if (require.main === module) {
module.exports();
}