Skip to content

Commit 4036834

Browse files
committed
added concurrency benchmark
1 parent 473316a commit 4036834

11 files changed

+166
-9
lines changed

benchmark/concurrency-trial.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
3+
module.exports = function (ourDbs, theirDbs, count, countPerCycle) {
4+
if (countPerCycle > 1000) {
5+
throw new Error('countPerCycle must be <= 1000');
6+
}
7+
var params = ['John Smith', 123456, null];
8+
if (count % countPerCycle !== 0) {
9+
count = count + countPerCycle - count % countPerCycle;
10+
}
11+
function callback0() {
12+
global.gc();
13+
ourTest(ourDbs, count, countPerCycle, params, callback1);
14+
}
15+
function callback1() {
16+
global.gc();
17+
theirTest(theirDbs, count, countPerCycle, params, callback2);
18+
}
19+
function callback2() {
20+
var closedCount = 0;
21+
ourDbs[0].on('close', closed).close();
22+
ourDbs[1].on('close', closed).close();
23+
theirDbs[0].close(closed);
24+
theirDbs[1].close(closed);
25+
function closed() {
26+
++closedCount === 4 && process.exit();
27+
}
28+
}
29+
setTimeout(callback0, 100);
30+
};
31+
32+
exports.data = undefined;
33+
34+
function ourTest(dbs, count, countPerCycle, params, done) {
35+
var requested = 0;
36+
var t0 = process.hrtime();
37+
(function request() {
38+
for (var i=0; i<countPerCycle; ++i) {
39+
if (i % 2) {
40+
exports.data = dbs[0].prepare('INSERT INTO entries VALUES (?, ?, ?)').run(params);
41+
} else {
42+
exports.data = dbs[1].prepare('SELECT name FROM entries WHERE rowid=?').pluck().get(i + 1);
43+
}
44+
}
45+
if ((requested += countPerCycle) < count) {
46+
setImmediate(request);
47+
} else {
48+
var td = process.hrtime(t0);
49+
report('better-sqlite3', count, td);
50+
done();
51+
}
52+
}());
53+
}
54+
function theirTest(dbs, count, countPerCycle, params, done) {
55+
var requested = 0;
56+
var completed = 0;
57+
var failures = 0;
58+
var t0 = process.hrtime();
59+
(function request() {
60+
for (var i=0; i<countPerCycle; ++i) {
61+
if (i % 2) {
62+
dbs[0].run('INSERT INTO entries VALUES (?, ?, ?)', params, callback);
63+
} else {
64+
dbs[1].get('SELECT name FROM entries WHERE rowid=?', i + 1, callback);
65+
}
66+
}
67+
if ((requested += countPerCycle) < count) {
68+
setImmediate(request);
69+
}
70+
}());
71+
function callback(err, data) {
72+
exports.data = data;
73+
if (err) {++failures;}
74+
if (++completed === count) {
75+
var td = process.hrtime(t0);
76+
report('node-sqlite3', count - failures, td);
77+
done();
78+
}
79+
}
80+
}
81+
82+
function report(name, count, time) {
83+
var ms = time[0] * 1000 + Math.round(time[1] / 1000000);
84+
console.log(name + '\t' + count + ' overlapping INSERT or SELECTs in ' + ms + 'ms');
85+
}

benchmark/dual-trial.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ module.exports = function (ourDb, theirDb, count, countPerCycle) {
1717
theirTest(theirDb, count, countPerCycle, params, callback2);
1818
}
1919
function callback2() {
20-
process.exit();
20+
var closedCount = 0;
21+
ourDb.on('close', closed).close();
22+
theirDb.close(closed);
23+
function closed() {
24+
++closedCount === 2 && process.exit();
25+
}
2126
}
2227
setTimeout(callback0, 100);
2328
};
@@ -54,7 +59,7 @@ function theirTest(db, count, countPerCycle, params, done) {
5459
if (i % 2) {
5560
db.run('INSERT INTO entries VALUES (?, ?, ?)', params, callback);
5661
} else {
57-
db.run('SELECT name FROM entries WHERE rowid=?', i + 1, callback);
62+
db.get('SELECT name FROM entries WHERE rowid=?', i + 1, callback);
5863
}
5964
}
6065
if ((requested += countPerCycle) < count) {

benchmark/get-db2.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
var path = require('path');
3+
var ours = require('../.');
4+
var theirs = require('sqlite3');
5+
6+
module.exports = function (name, callback) {
7+
var opened = 0;
8+
var ours1 = new ours(path.join('temp', name + '.ours.db')).on('open', open);
9+
var ours2 = new ours(path.join('temp', name + '.ours.db')).on('open', open);
10+
var theirs1 = new theirs.Database(path.join('temp', name + '.theirs.db'), open);
11+
var theirs2 = new theirs.Database(path.join('temp', name + '.theirs.db'), open);
12+
function open() {
13+
++opened === 4 && pragma();
14+
}
15+
function pragma() {
16+
ours1.pragma('cache_size = -16000;');
17+
ours2.pragma('cache_size = -16000;');
18+
theirs1.run('PRAGMA cache_size = -16000;', ranPragma);
19+
theirs2.run('PRAGMA cache_size = -16000;', ranPragma);
20+
var pragmaCount = 0;
21+
function ranPragma() {
22+
if (++pragmaCount === 2) {
23+
callback([ours1, ours2], [theirs1, theirs2]);
24+
}
25+
}
26+
}
27+
};

benchmark/insert-trial.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ module.exports = function (ourDb, theirDb, count, params) {
1010
theirTest(theirDb, count, params, callback2);
1111
}
1212
function callback2() {
13-
process.exit();
13+
var closedCount = 0;
14+
ourDb.on('close', closed).close();
15+
theirDb.close(closed);
16+
function closed() {
17+
++closedCount === 2 && process.exit();
18+
}
1419
}
1520
setTimeout(callback0, 100);
1621
};

benchmark/select-all-trial.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ exports = module.exports = function (ourDb, theirDb, count, small) {
1313
theirTest(theirDb, count, SQL, small, callback2);
1414
}
1515
function callback2() {
16-
process.exit();
16+
var closedCount = 0;
17+
ourDb.on('close', closed).close();
18+
theirDb.close(closed);
19+
function closed() {
20+
++closedCount === 2 && process.exit();
21+
}
1722
}
1823
setTimeout(callback0, 100);
1924
};

benchmark/select-each-trial.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ exports = module.exports = function (ourDb, theirDb, count, small) {
1313
theirTest(theirDb, count, SQL, small, callback2);
1414
}
1515
function callback2() {
16-
process.exit();
16+
var closedCount = 0;
17+
ourDb.on('close', closed).close();
18+
theirDb.close(closed);
19+
function closed() {
20+
++closedCount === 2 && process.exit();
21+
}
1722
}
1823
setTimeout(callback0, 100);
1924
};

benchmark/select-trial.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ exports = module.exports = function (ourDb, theirDb, count, small) {
1313
theirTest(theirDb, count, SQL, small, callback2);
1414
}
1515
function callback2() {
16-
process.exit();
16+
var closedCount = 0;
17+
ourDb.on('close', closed).close();
18+
theirDb.close(closed);
19+
function closed() {
20+
++closedCount === 2 && process.exit();
21+
}
1722
}
1823
setTimeout(callback0, 100);
1924
};

benchmark/transaction-trial.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ module.exports = function (ourDb, theirDb, count, params) {
1010
theirTest(theirDb, count, params, callback2);
1111
}
1212
function callback2() {
13-
process.exit();
13+
var closedCount = 0;
14+
ourDb.on('close', closed).close();
15+
theirDb.close(closed);
16+
function closed() {
17+
++closedCount === 2 && process.exit();
18+
}
1419
}
1520
setTimeout(callback0, 100);
1621
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
require('../get-db2')('insert-db', function (ourDbs, theirDbs) {
3+
ourDbs[0].pragma('journal_mode = WAL');
4+
ourDbs[1].pragma('journal_mode = WAL');
5+
ourDbs[0].pragma('synchronous = 1');
6+
ourDbs[1].pragma('synchronous = 1');
7+
theirDbs[0].exec('PRAGMA journal_mode = WAL; PRAGMA synchronous = 1;', setPragma);
8+
theirDbs[1].exec('PRAGMA journal_mode = WAL; PRAGMA synchronous = 1;', setPragma);
9+
var pragmaCount = 0;
10+
function setPragma() {
11+
if (++pragmaCount === 2) {
12+
require('../concurrency-trial')(ourDbs, theirDbs, 10000, 10);
13+
}
14+
}
15+
});

benchmark/trials/transaction.sync.small.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require('../get-db')('insert-db', function (ourDb, theirDb) {
33
ourDb.pragma('journal_mode = DELETE');
44
ourDb.pragma('synchronous = 2');
55
theirDb.exec('PRAGMA journal_mode = DELETE; PRAGMA synchronous = 2;', function () {
6-
require('../transaction-trial')(ourDb, theirDb, 500, [
6+
require('../transaction-trial')(ourDb, theirDb, 5000, [
77
'John Smith',
88
123456,
99
null

0 commit comments

Comments
 (0)