0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/benchmark/io.c

111 lines
2.2 KiB
C
Raw Normal View History

2010-08-17 17:43:26 +02:00
/**
* gcc -o iotest io.c
*/
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
2010-09-24 11:21:17 +02:00
#include <sys/time.h>
#include <assert.h>
2010-08-17 17:43:26 +02:00
#include <stdlib.h>
#include <stdio.h>
int tsize = 1000 * 1048576;
const char *path = "/tmp/wt.dat";
2010-09-24 11:21:17 +02:00
int c = 0;
2010-08-17 17:43:26 +02:00
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);
2010-09-24 11:21:17 +02:00
struct timeval start, end;
2010-08-17 17:43:26 +02:00
double elapsed;
double mbps;
int fd = open(path, O_CREAT|O_WRONLY, 0644);
if (fd < 0) {
perror("open failed");
exit(254);
}
2010-09-24 11:21:17 +02:00
assert(0 == gettimeofday(&start, NULL));
2010-08-17 17:43:26 +02:00
for (i = 0; i < size; i += bsize) {
int rv = write(fd, buf, bsize);
2010-09-24 11:21:17 +02:00
if (c++ % 2000 == 0) fprintf(stderr, ".");
2010-08-17 17:43:26 +02:00
if (rv < 0) {
perror("write failed");
exit(254);
}
}
#ifdef __linux__
fdatasync(fd);
#else
fsync(fd);
#endif
close(fd);
2010-09-24 11:21:17 +02:00
assert(0 == gettimeofday(&end, NULL));
elapsed = (end.tv_sec - start.tv_sec) + ((double)(end.tv_usec - start.tv_usec))/100000.;
2010-08-17 17:43:26 +02:00
mbps = ((tsize/elapsed)) / 1048576;
2010-09-24 11:21:17 +02:00
fprintf(stderr, "\nWrote %d bytes in %03fs using %ld byte buffers: %03fmB/s\n", size, elapsed, bsize, mbps);
2010-08-17 17:43:26 +02:00
free(buf);
}
void readtest(int size, size_t bsize)
{
int i;
char *buf = bufit(bsize);
2010-09-24 11:21:17 +02:00
struct timeval start, end;
2010-08-17 17:43:26 +02:00
double elapsed;
double mbps;
int fd = open(path, O_RDONLY, 0644);
if (fd < 0) {
perror("open failed");
exit(254);
}
2010-09-24 11:21:17 +02:00
assert(0 == gettimeofday(&start, NULL));
2010-08-17 17:43:26 +02:00
for (i = 0; i < size; i += bsize) {
int rv = read(fd, buf, bsize);
if (rv < 0) {
perror("write failed");
exit(254);
}
}
close(fd);
2010-09-24 11:21:17 +02:00
assert(0 == gettimeofday(&end, NULL));
elapsed = (end.tv_sec - start.tv_sec) + ((double)(end.tv_usec - start.tv_usec))/100000.;
2010-08-17 17:43:26 +02:00
mbps = ((tsize/elapsed)) / 1048576;
2010-09-24 11:21:17 +02:00
fprintf(stderr, "Read %d bytes in %03fs using %ld byte buffers: %03fmB/s\n", size, elapsed, bsize, mbps);
2010-08-17 17:43:26 +02:00
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;
}