forked from ronomon/direct-io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.js
246 lines (231 loc) · 6.49 KB
/
benchmark.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
var Node = {
fs: require('fs'),
path: require('path')
};
var Queue = require('@ronomon/queue');
var binding = require('./binding.node');
const ALIGNED = 1;
const AMORTIZED_FDATASYNC = 2;
const AMORTIZED_FSYNC = 4;
const BUFFERED = 8;
const FDATASYNC = 16;
const FSYNC = 32;
const O_DIRECT = 64;
const O_DSYNC = 128;
const O_SYNC = 256;
const PATH_DEFAULT = Node.path.resolve(module.filename, '..', 'file');
const SIZE = 128 * 1024 * 1024;
const BLOCKS = [
4096,
8192,
16384,
32768,
65536,
131072,
262144,
524288,
1048576,
2097152,
4194304,
8388608,
16777216,
33554432
];
const FLAGS = [
BUFFERED,
BUFFERED | ALIGNED,
O_DIRECT,
AMORTIZED_FDATASYNC,
AMORTIZED_FDATASYNC | ALIGNED,
AMORTIZED_FDATASYNC | O_DIRECT,
AMORTIZED_FSYNC,
AMORTIZED_FSYNC | ALIGNED,
AMORTIZED_FSYNC | O_DIRECT,
FDATASYNC,
FDATASYNC | ALIGNED,
FDATASYNC | O_DIRECT,
FSYNC,
FSYNC | ALIGNED,
FSYNC | O_DIRECT,
O_DSYNC,
O_DSYNC | ALIGNED,
O_DSYNC | O_DIRECT,
O_SYNC,
O_SYNC | ALIGNED,
O_SYNC | O_DIRECT
];
var BLOCK_MIN = BLOCKS[0];
var BLOCK_MAX = BLOCKS[BLOCKS.length - 1];
var args = process.argv.slice(2);
var argsIndex = 0;
while (argsIndex < args.length) {
var arg = args[argsIndex];
if (/^--/.test(arg)) {
if (/^--block-(max|min)=\d+$/.test(arg)) {
var value = parseInt(arg.split('=')[1], 10);
if (value < BLOCK_MIN) throw new Error(arg + ' < BLOCK_MIN=' + BLOCK_MIN);
if (value > BLOCK_MAX) throw new Error(arg + ' > BLOCK_MAX=' + BLOCK_MAX);
if (/^--block-max=\d+$/.test(arg)) {
BLOCK_MAX = value;
} else {
BLOCK_MIN = value;
}
args.splice(argsIndex, 1);
} else {
throw new Error('unsupported arg: ' + arg);
}
} else {
argsIndex++;
}
}
var path = args.pop() || PATH_DEFAULT;
function open(path, options, end) {
var flags = Node.fs.constants.O_RDWR;
var type = parseType(path);
if (type === 1) flags |= Node.fs.constants.O_CREAT;
if (options.flags & O_DIRECT) flags |= binding.O_DIRECT;
if (options.flags & O_DSYNC) flags |= binding.O_DSYNC;
if (options.flags & O_SYNC) flags |= binding.O_SYNC;
if (type === 0) {
if (process.platform === 'darwin') {
flags |= binding.O_EXLOCK;
} else if (process.platform !== 'win32') {
flags |= binding.O_EXCL;
}
}
Node.fs.open(path, flags,
function(error, fd) {
if (error) return end(error);
function close(error) {
Node.fs.closeSync(fd);
end(error);
}
if ((options.flags & O_DIRECT) && process.platform === 'darwin') {
binding.setF_NOCACHE(fd, 1,
function(error) {
if (error) return close(error);
end(undefined, fd);
}
);
} else if (type === 0 && process.platform === 'win32') {
binding.setFSCTL_LOCK_VOLUME(fd, 1,
function(error) {
if (error) return close(error);
end(undefined, fd);
}
);
} else {
end(undefined, fd);
}
}
);
}
function padL(value, length) {
var string = String(value);
while (string.length < length) string = ' ' + string;
return string;
}
function padR(value, length) {
var string = String(value);
while (string.length < length) string += ' ';
return string;
}
function parseType(path) {
if (process.platform === 'win32') {
if (/^\\\\\.\\.+/.test(path)) return 0;
return 1;
} else {
if (/^\/dev\/.+/i.test(path)) return 0;
return 1;
}
}
var bufferUnaligned = Buffer.alloc(1 + SIZE, 255).slice(1, 1 + SIZE);
var bufferAligned = binding.getAlignedBuffer(SIZE, 4096);
var length = bufferAligned.length;
while (length--) bufferAligned[length] = 255;
var queue = new Queue();
queue.onData = function(options, end) {
var type = parseType(path);
open(path, options,
function(error, fd) {
if (error) return end(error);
if (options.flags & (ALIGNED | O_DIRECT)) {
var buffer = bufferAligned;
} else {
var buffer = bufferUnaligned;
}
var position = 0;
var previous = 0;
var blocks = Math.ceil(SIZE / options.block);
if (options.flags & (FDATASYNC | FSYNC | O_DSYNC | O_SYNC)) {
blocks = Math.min(options.block / 32, blocks);
}
var now = Date.now();
while (blocks--) {
position += Node.fs.writeSync(
fd,
buffer,
0,
options.block,
position
);
if (position - previous !== options.block) {
throw new Error('position did not advance by a block');
}
previous = position;
if (options.flags & FDATASYNC) Node.fs.fdatasyncSync(fd);
if (options.flags & FSYNC) Node.fs.fsyncSync(fd);
}
if (options.flags & AMORTIZED_FDATASYNC) Node.fs.fdatasyncSync(fd);
if (options.flags & AMORTIZED_FSYNC) Node.fs.fsyncSync(fd);
var time = Date.now() - now;
var throughput = ((position / (1024 * 1024)) / (time / 1000)).toFixed(2);
Node.fs.fdatasyncSync(fd);
Node.fs.closeSync(fd);
var result = [];
result.push(padL(options.block, 10));
result.push(Node.path.basename(path) === 'file' ? 'file' : path);
var type = [];
if (options.flags & AMORTIZED_FDATASYNC) type.push('AMORTIZED_FDATASYNC');
if (options.flags & AMORTIZED_FSYNC) type.push('AMORTIZED_FSYNC');
if (options.flags & BUFFERED) type.push('BUFFERED');
if (options.flags & FDATASYNC) type.push('FDATASYNC');
if (options.flags & FSYNC) type.push('FSYNC');
if (options.flags & O_DSYNC) type.push('O_DSYNC');
if (options.flags & O_SYNC) type.push('O_SYNC');
if (options.flags & O_DIRECT) type.push('O_DIRECT');
if (options.flags & ALIGNED) type.push('ALIGNED');
result.push(padR(type.join(' + '), 38));
result.push(padL(throughput, 8) + ' MB/s');
console.log(result.join(' | '));
end();
}
);
};
queue.onEnd = function(error) {
if (path === PATH_DEFAULT) {
try {
Node.fs.unlinkSync(path);
} catch (unlinkError) {
if (!error) throw unlinkError;
}
}
if (error) throw error;
};
BLOCKS.forEach(
function(block) {
if (block < BLOCK_MIN) return;
if (block > BLOCK_MAX) return;
FLAGS.forEach(
function(flags) {
if (flags & O_DIRECT) {
if (!binding.O_DIRECT && process.platform !== 'darwin') return;
}
if ((flags & O_DSYNC) && !binding.O_DSYNC) return;
if ((flags & O_SYNC) && !binding.O_SYNC) return;
queue.push({ block: block, path: path, flags: flags });
}
);
}
);
queue.end();