/
odbc.js
132 lines (107 loc) · 3.05 KB
/
odbc.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
/**
* Copyright (C) 2012, Oleg Efimov and other contributors
*
* See license text in LICENSE file
*/
"use strict";
function benchmark() {
// Require modules
var
OdbcDatabase,
helper = require('./helper'),
conn;
function fetchAllAsyncBenchmark(results, callback, cfg) {
var start_hrtime;
start_hrtime = process.hrtime();
conn.query(cfg.select_query, function(error, result) {
if (error) {
console.error(error);
process.exit();
}
results.selects = Math.round(result.length / helper.hrtimeDeltaInSeconds(start_hrtime));
// Finish benchmark
callback(results);
});
}
function insertAsyncBenchmark(results, callback, cfg) {
var start_hrtime, i = 0;
start_hrtime = process.hrtime();
function insertAsync() {
i += 1;
if (i <= cfg.insert_rows_count) {
conn.query(cfg.insert_query, function(error, result) {
if (error) {
console.error(error);
process.exit();
}
insertAsync();
});
} else {
results.inserts = Math.round(cfg.insert_rows_count / helper.hrtimeDeltaInSeconds(start_hrtime));
setTimeout(function () {
fetchAllAsyncBenchmark(results, callback, cfg);
}, cfg.delay_before_select);
}
}
insertAsync();
}
function initBenchmark(results, callback, cfg) {
var
start_hrtime,
options_string;
start_hrtime = process.hrtime();
conn = new OdbcDatabase();
options_string = "DRIVER={MySQL};DATABASE=" + cfg.database
+ ";USER=" + cfg.user
+ ";PASSWORD=" + cfg.password
+ ";SERVER=" + cfg.host;
conn.open(options_string, function(error) {
if (error) {
results.error = error.message;
callback(results);
return;
}
conn.query("DROP TABLE IF EXISTS " + cfg.test_table, function(error, rows) {
if (error) {
results.error = error.message;
callback(results);
return;
}
conn.query(cfg.create_table_query, function(error, rows) {
if (error) {
results.error = error.message;
callback(results);
return;
}
results.init = helper.roundWithPrecision(helper.hrtimeDeltaInSeconds(start_hrtime), 3);
insertAsyncBenchmark(results, callback, cfg);
});
});
});
}
var cfg = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
cfg += data;
});
process.stdin.on('end', function() {
var results = {},
callback = function() {
process.stdout.write(JSON.stringify(results));
};
try {
OdbcDatabase = require('odbc').Database;
} catch (e) {
callback(results);
return;
}
initBenchmark(results, callback, JSON.parse(cfg));
});
process.stdin.resume();
}
if (!module.parent) {
benchmark();
}
exports.run = function (callback, cfg) {
require('./helper').spawnBenchmark('node', [__filename], callback, cfg);
};