ref: 3c04d813ab19bd5b08cf7857b17976517de2ee71
dir: /rdwr.c/
#include <u.h>
#include <libc.h>
#include "bench.h"
void
benchsysr1(B *b)
{
int i;
extern int sysr1(void);
for(i = 0; i < b->N; i++)
sysr1();
}
void
benchread(B *b, char *path, int n)
{
int i, fd;
char buf[128*IOUNIT];
if((fd = open(path, OREAD)) == -1)
sysfatal("open %s: %r", path);
for(i = 0; i < b->N; i++)
pread(fd, buf, n, 0);
close(fd);
}
void
benchwrite(B *b, char *path, int n)
{
int i, fd;
char buf[128*IOUNIT];
if((fd = open(path, OWRITE)) == -1)
sysfatal("open %s: %r", path);
for(i = 0; i < b->N; i++)
pwrite(fd, buf, n, 0);
close(fd);
}
void
benchpipe(B *b, int n)
{
char buf[128*IOUNIT];
int i, pfd[2];
if(pipe(pfd) == -1)
sysfatal("pipe: %r");
switch(rfork(RFPROC)){
case -1:
sysfatal("fork: %r");
case 0:
for(i = 0; i < b->N; i++)
if(write(pfd[0], buf, n) == -1)
sysfatal("write: %r");
exits(nil);
break;
default:
for(i = 0; i < b->N; i++)
if(read(pfd[1], buf, n) == -1)
sysfatal("read: %r");
free(wait());
close(pfd[0]);
close(pfd[1]);
break;
}
}
void benchreadzero(B *b) { benchread(b, "/dev/zero", 4); }
void benchwritenull(B *b) { benchwrite(b, "/dev/null", 4); }
void benchreadmordor(B *b) { benchread(b, "/dev/mordor", 4); }
void benchwritemordor(B *b) { benchwrite(b, "/dev/mordor", 4); }
void benchpipe1(B *b) { benchpipe(b, 1); }
void benchpipe16(B *b) { benchpipe(b, 16); }
void benchpipe256(B *b) { benchpipe(b, 256); }
void benchpipe4096(B *b) { benchpipe(b, 4096); }
void benchpipe4097(B *b) { benchpipe(b, 4097); }
void benchpipe32768(B *b) { benchpipe(b, 32768); }
void
main(int argc, char **argv)
{
benchinit(argc, argv);
print("== nop io ==\n");
BM(benchsysr1);
BM(benchreadzero);
BM(benchwritenull);
BM(benchreadmordor);
BM(benchwritemordor);
print("== pipe io ==\n");
BM(benchpipe1);
BM(benchpipe16);
BM(benchpipe256);
BM(benchpipe4096);
BM(benchpipe4097);
BM(benchpipe32768);
exits(nil);
}