-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: add a few more fs benchmarks
- Loading branch information
Showing
3 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
if (process.platform === 'win32') | ||
throw new Error('This benchmark is not compatible with Windows'); | ||
|
||
const common = require('../common.js'); | ||
const fs = require('fs'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: ['buf', 'asc', 'utf'], | ||
dur: [5], | ||
size: [1024, 4096, 65535, 1024 * 1024] | ||
}); | ||
|
||
function main(conf) { | ||
const dur = +conf.dur * 1000; | ||
const hwm = +conf.size; | ||
var bytes = 0; | ||
var encoding; | ||
|
||
switch (conf.type) { | ||
case 'buf': | ||
encoding = null; | ||
break; | ||
case 'asc': | ||
encoding = 'ascii'; | ||
break; | ||
case 'utf': | ||
encoding = 'utf8'; | ||
break; | ||
default: | ||
throw new Error('invalid type'); | ||
} | ||
|
||
setTimeout(() => { | ||
// MB/sec | ||
bench.end(bytes / (1024 * 1024)); | ||
process.exit(0); | ||
}, dur); | ||
|
||
fs.createReadStream('/dev/zero', { | ||
highWaterMark: hwm, | ||
encoding: encoding | ||
}).on('open', function() { | ||
bench.start(); | ||
}).on('data', function(chunk) { | ||
bytes += chunk.length; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const common = require('../common.js'); | ||
const filename = path.resolve(__dirname, '.removeme-benchmark-garbage'); | ||
const fs = require('fs'); | ||
const writeFile = fs.writeFile; | ||
|
||
const bench = common.createBenchmark(main, { | ||
dur: [5], | ||
file: [''], | ||
type: ['buf', 'asc', 'utf'], | ||
size: [2, 1024, 64 * 1024, 1024 * 1024] | ||
}); | ||
|
||
function main(conf) { | ||
const dur = +conf.dur * 1000; | ||
const size = +conf.size; | ||
const file = (conf.file.length === 0 ? filename : conf.file); | ||
var writes = 0; | ||
var ended = false; | ||
var encoding; | ||
var data; | ||
|
||
try { fs.unlinkSync(file); } catch (e) {} | ||
process.on('exit', function() { | ||
try { fs.unlinkSync(file); } catch (e) {} | ||
}); | ||
|
||
setTimeout(function() { | ||
bench.end(writes); | ||
ended = true; | ||
}, dur); | ||
|
||
switch (conf.type) { | ||
case 'buf': | ||
data = Buffer.alloc(size, 'b'); | ||
(function() { | ||
function afterWrite(err) { | ||
if (err) | ||
throw err; | ||
if (ended) | ||
return; | ||
++writes; | ||
writeFile(file, data, afterWrite); | ||
} | ||
bench.start(); | ||
writeFile(file, data, afterWrite); | ||
})(); | ||
return; | ||
case 'asc': | ||
data = new Array(size + 1).join('a'); | ||
encoding = 'ascii'; | ||
break; | ||
case 'utf': | ||
data = new Array(Math.ceil(size / 2) + 1).join('ü'); | ||
encoding = 'utf8'; | ||
break; | ||
default: | ||
throw new Error('invalid type'); | ||
} | ||
|
||
(function() { | ||
function afterWrite(err) { | ||
if (err) | ||
throw err; | ||
if (ended) | ||
return; | ||
++writes; | ||
writeFile(file, data, encoding, afterWrite); | ||
} | ||
bench.start(); | ||
writeFile(file, data, encoding, afterWrite); | ||
})(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const common = require('../common.js'); | ||
const filename = path.resolve(__dirname, '.removeme-benchmark-garbage'); | ||
const fs = require('fs'); | ||
const writeFileSync = fs.writeFileSync; | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e5], | ||
file: [''], | ||
type: ['buf', 'asc', 'utf'], | ||
size: [2, 1024, 64 * 1024, 1024 * 1024] | ||
}); | ||
|
||
function main(conf) { | ||
const n = +conf.n; | ||
const size = +conf.size; | ||
const file = (conf.file.length === 0 ? filename : conf.file); | ||
var encoding; | ||
var data; | ||
var i; | ||
|
||
try { fs.unlinkSync(file); } catch (e) {} | ||
process.on('exit', function() { | ||
try { fs.unlinkSync(file); } catch (e) {} | ||
}); | ||
|
||
switch (conf.type) { | ||
case 'buf': | ||
data = Buffer.alloc(size, 'b'); | ||
bench.start(); | ||
for (i = 0; i < n; ++i) | ||
writeFileSync(file, data); | ||
bench.end(n); | ||
return; | ||
case 'asc': | ||
data = new Array(size + 1).join('a'); | ||
encoding = 'ascii'; | ||
break; | ||
case 'utf': | ||
data = new Array(Math.ceil(size / 2) + 1).join('ü'); | ||
encoding = 'utf8'; | ||
break; | ||
default: | ||
throw new Error('invalid type'); | ||
} | ||
|
||
bench.start(); | ||
for (i = 0; i < n; ++i) | ||
writeFileSync(file, data, encoding); | ||
bench.end(n); | ||
} |