Last active
March 25, 2016 12:31
-
-
Save funny-falcon/9512b47270c3bebb473c to your computer and use it in GitHub Desktop.
bench pipe vs socket
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
$ echo $((16*1024*1024)) | sudo tee /proc/sys/fs/pipe-max-size | |
$ gcc -ggdb3 -DN=256 -O1 bench-unixsocket-pipe.c | |
yura@falcon-new:~/tmp$ taskset -c 2,3 ./a.out 1000000000 $((16*1024*1024)) | |
pipe lasts: 3.121495 sec, iter/sec: 320359349.183537 | |
sock lasts: 5.402942 sec, iter/sec: 185084353.868363 | |
pipe lasts: 3.287446 sec, iter/sec: 304187536.025462 | |
sock lasts: 5.444722 sec, iter/sec: 183664098.170782 | |
$ gcc -ggdb3 -DN=512 -O1 bench-unixsocket-pipe.c | |
$ taskset -c 2,3 ./a.out 1000000000 $((16*1024*1024)) | |
pipe lasts: 1.790609 sec, iter/sec: 558469073.197397 | |
sock lasts: 2.750309 sec, iter/sec: 363595473.689393 | |
pipe lasts: 1.836527 sec, iter/sec: 544505908.943003 | |
sock lasts: 2.745830 sec, iter/sec: 364188561.563109 | |
$ gcc -ggdb3 -DN=2048 -O1 bench-unixsocket-pipe.c | |
$ taskset -c 2,3 ./a.out 1000000000 $((16*1024*1024)) | |
pipe lasts: 0.729964 sec, iter/sec: 1369930904.096325 | |
sock lasts: 0.728934 sec, iter/sec: 1371865572.857027 | |
pipe lasts: 0.686415 sec, iter/sec: 1456843713.974395 | |
sock lasts: 0.719664 sec, iter/sec: 1389537428.464841 | |
$ gcc -ggdb3 -DN=20480 -O1 bench-unixsocket-pipe.c | |
$ taskset -c 2,3 ./a.out 1000000000 $((16*1024*1024)) | |
pipe lasts: 0.239564 sec, iter/sec: 4174254156.268338 | |
sock lasts: 0.127470 sec, iter/sec: 7844970947.640639 | |
pipe lasts: 0.241605 sec, iter/sec: 4138993031.587193 | |
sock lasts: 0.128974 sec, iter/sec: 7753475456.606051 | |
$ gcc -ggdb3 -DN=204800 -O1 bench-unixsocket-pipe.c | |
$ taskset -c 2,3 ./a.out 5000000000 $((16*1024*1024)) | |
pipe lasts: 1.094997 sec, iter/sec: 4566223902.796374 | |
sock lasts: 0.410690 sec, iter/sec: 12174630503.981833 | |
pipe lasts: 1.093797 sec, iter/sec: 4571230275.116221 | |
sock lasts: 0.415574 sec, iter/sec: 12031541031.321995 |
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
#include <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <time.h> | |
#include <inttypes.h> | |
#include <fcntl.h> | |
//#include <linux/fcntl.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#ifndef N | |
#define N (512*4) | |
#endif | |
static char buf[N + sizeof(int)] = {}; | |
int | |
fullwrite(int fd, void* buf, int sz) | |
{ | |
while (sz > 0) { | |
int r = write(fd, buf, sz); | |
if (r <= 0) { | |
_exit(1); | |
} | |
buf += r; | |
sz -= r; | |
} | |
return 1; | |
} | |
void | |
writer(int fd, int64_t total) | |
{ | |
int r; | |
while (total > 0) { | |
int rand = random() % N; | |
#if 0 | |
fullwrite(fd, &rand, sizeof(rand)); | |
fullwrite(fd, buf, rand + 4); | |
#else | |
*(int*)buf = rand; | |
fullwrite(fd, buf, rand + 4); | |
#endif | |
total -= rand; | |
} | |
close(fd); | |
} | |
int | |
fullread(int fd, void* buf, int sz) | |
{ | |
while (sz > 0) { | |
int r = read(fd, buf, sz); | |
if (r <= 0) { | |
return 0; | |
} | |
buf += r; | |
sz -= r; | |
} | |
return 1; | |
} | |
void | |
reader(int fd) | |
{ | |
int sz; | |
char buf[N]; | |
int r; | |
while (fullread(fd, &sz, sizeof(sz))) { | |
if (!fullread(fd, buf, sz)) exit(1); | |
} | |
close(fd); | |
} | |
#define F_SETPIPE_SZ 1031 | |
int main(int argc, char** argv) | |
{ | |
int64_t total = 1000000000; | |
int pipebuf = 65*1024; | |
if (argc > 1) total = atoll(argv[1]); | |
if (argc > 2) pipebuf = atoi(argv[2]); | |
for (int kk=2; kk; kk--) { | |
int r; | |
int pipefd[2]; | |
r = pipe(pipefd); (void)r; | |
r = fcntl(pipefd[1], F_SETPIPE_SZ, pipebuf); | |
if (r != 0) { | |
//printf("fcntl: %d\n", r); | |
} | |
int sockfd[2]; | |
r = socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sockfd); (void)r; | |
struct timespec ts = {}, tstop; | |
double d; | |
clock_gettime(CLOCK_MONOTONIC, &ts); | |
if (fork()) { | |
close(pipefd[1]); | |
reader(pipefd[0]); | |
} else { | |
writer(pipefd[1], total); | |
_exit(0); | |
} | |
clock_gettime(CLOCK_MONOTONIC, &tstop); | |
d = (tstop.tv_sec - ts.tv_sec) + (tstop.tv_nsec - ts.tv_nsec) * 1e-9; | |
printf("pipe lasts: %f sec, iter/sec: %f\n", d, total/d); | |
clock_gettime(CLOCK_MONOTONIC, &ts); | |
if (fork()) { | |
close(sockfd[1]); | |
reader(sockfd[0]); | |
} else { | |
writer(sockfd[1], total); | |
_exit(0); | |
} | |
clock_gettime(CLOCK_MONOTONIC, &tstop); | |
d = (tstop.tv_sec - ts.tv_sec) + (tstop.tv_nsec - ts.tv_nsec) * 1e-9; | |
printf("sock lasts: %f sec, iter/sec: %f\n", d, total/d); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment