Add Paul Querna's io benchmarks

This commit is contained in:
Ryan Dahl 2010-08-17 08:43:26 -07:00
Родитель cf2b206a8e
Коммит 41ad33910f
2 изменённых файлов: 212 добавлений и 0 удалений

107
benchmark/io.c Normal file
Просмотреть файл

@ -0,0 +1,107 @@
/**
* gcc -o iotest io.c
*/
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int tsize = 1000 * 1048576;
const char *path = "/tmp/wt.dat";
char* bufit(size_t l)
{
char *p = malloc(l);
memset(p, '!', l);
return p;
}
void writetest(int size, size_t bsize)
{
int i;
char *buf = bufit(bsize);
clock_t start, end;
double elapsed;
double mbps;
int fd = open(path, O_CREAT|O_WRONLY, 0644);
if (fd < 0) {
perror("open failed");
exit(254);
}
start = clock();
for (i = 0; i < size; i += bsize) {
int rv = write(fd, buf, bsize);
if (rv < 0) {
perror("write failed");
exit(254);
}
}
#ifdef __linux__
fdatasync(fd);
#else
fsync(fd);
#endif
close(fd);
end = clock();
elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
mbps = ((tsize/elapsed)) / 1048576;
fprintf(stderr, "Wrote %d bytes in %03fs using %d byte buffers: %03fmB/s\n", size, elapsed, bsize, mbps);
free(buf);
}
void readtest(int size, size_t bsize)
{
int i;
char *buf = bufit(bsize);
clock_t start, end;
double elapsed;
double mbps;
int fd = open(path, O_RDONLY, 0644);
if (fd < 0) {
perror("open failed");
exit(254);
}
start = clock();
for (i = 0; i < size; i += bsize) {
int rv = read(fd, buf, bsize);
if (rv < 0) {
perror("write failed");
exit(254);
}
}
close(fd);
end = clock();
elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
mbps = ((tsize/elapsed)) / 1048576;
fprintf(stderr, "Read %d bytes in %03fs using %d byte buffers: %03fmB/s\n", size, elapsed, bsize, mbps);
free(buf);
}
void cleanup() {
unlink(path);
}
int main()
{
int i;
int bsizes[] = {1024, 4096, 8192, 16384, 32768, 65536, 0};
for (i = 0; bsizes[i] != 0; i++) {
writetest(tsize, bsizes[i]);
}
for (i = 0; bsizes[i] != 0; i++) {
readtest(tsize, bsizes[i]);
}
atexit(cleanup);
return 0;
}

105
benchmark/io.js Normal file
Просмотреть файл

@ -0,0 +1,105 @@
var fs = require('fs');
var Buffer = require('buffer').Buffer;
var path = "/tmp/wt.dat";
var tsize = 1000 * 1048576;
var bsizes = [1024, 4096, 8192, 16384, 32768, 65536];
function bufit(size) {
var buf = new Buffer(size);
for (var i = 0; i <buf.length ; i += 1) {
buf[i] = 33;
}
return buf;
}
function once(emitter, name, cb) {
function incb() {
cb.apply(undefined, arguments);
emitter.removeListener(name, incb);
}
emitter.addListener(name, incb);
}
function writetest(size, bsize) {
var s = fs.createWriteStream(path, {'flags': 'w', 'mode': 0644});
var remaining = size;
var buf = bufit(bsize);
function dowrite() {
var rv = s.write(buf);
remaining -= buf.length;
if (remaining > 0) {
//if (remaining % 90000 == 0) console.error("remaining: %d", remaining);
//process.nextTick(dowrite);
} else {
s.emit('done')
s.end();
}
}
s.on('drain', function () {
dowrite();
});
dowrite();
return s;
}
function readtest(size, bsize) {
var s = fs.createReadStream(path, {'flags': 'r', 'encoding': 'binary', 'mode': 0644, 'bufferSize': bsize});
s.addListener("data", function (chunk) {
// got a chunk...
});
return s;
}
function wt(tsize, bsize, done) {
var start = Date.now();
s = writetest(tsize, bsizes[0]);
s.addListener('close', function() {
var end = Date.now();
var diff = end - start;
console.log('Wrote '+ tsize +' bytes in '+ diff/1000 +'s using '+ bsize +' byte buffers: '+ ((tsize/(diff/1000)) / 1048576) +' mB/s');
done();
});
}
function rt(tsize, bsize, done) {
var start = Date.now();
s = readtest(tsize, bsizes[0]);
s.addListener('close', function() {
var end = Date.now();
var diff = end - start;
console.log('Read '+ tsize +' bytes in '+ diff/1000 +'s using '+ bsize +' byte buffers: '+ ((tsize/(diff/1000)) / 1048576) +' mB/s');
done();
});
}
var bs= 0;
function nextwt() {
if (bsizes.length <= bs) {
bs = 0;
nextrt();
return;
}
wt(tsize, bsizes[bs], nextwt);
bs += 1;
}
function nextrt() {
if (bsizes.length <= bs) {
fs.unlink(path, function (err) {
if (err) throw err;
console.log('All done!');
});
return;
}
rt(tsize, bsizes[bs], nextrt);
bs += 1;
}
nextwt();