Created
January 30, 2014 16:08
-
-
Save emilisto/8712053 to your computer and use it in GitHub Desktop.
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
/* | |
* | |
* Simple benchmark of `pipe` (https://github.com/wowus/pipe) library | |
* | |
* $ gcc -O3 -pthread -o test pipe.c test.c | |
* $ ./test | |
* Consuming 2000000 items from the queue... | |
* Received 2000000 items in 0.52 seconds, average throughput of 3878081 items/s | |
* | |
*/ | |
#include <stdio.h> | |
#include <time.h> | |
#include <time.h> | |
#include <sys/time.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
#include "pipe.h" | |
#define N_ITEMS (int)2e6 | |
#define TIMEDIFF(START, END) \ | |
((double)(END.tv_sec - START.tv_sec) + \ | |
(double)(END.tv_usec - START.tv_usec) * 1e-6) | |
void *producer_thread(void *arg) | |
{ | |
pipe_producer_t *producer = (pipe_producer_t *) arg; | |
srand(time(NULL)); | |
int r = (int)(rand() / 1e3); | |
int elems[] = { r }; | |
while(1) { | |
pipe_push(producer, elems, 1); | |
} | |
return NULL; | |
} | |
void *consumer_threat(void *arg) | |
{ | |
pipe_consumer_t *consumer = (pipe_consumer_t *) arg; | |
int BUFSIZE = 1; | |
int *elems = malloc(sizeof(int) * BUFSIZE); | |
int i = 0; | |
struct timeval start; | |
struct timeval end; | |
printf("Consuming %d items from the queue...\n", N_ITEMS); | |
gettimeofday(&start, NULL); | |
while(1) { | |
int n = pipe_pop(consumer, elems, BUFSIZE); | |
if(i >= N_ITEMS) { | |
gettimeofday(&end, NULL); | |
double seconds = TIMEDIFF(start, end); | |
printf("Received %d items in %.2f seconds, average throughput of %.0f items/s\n", | |
N_ITEMS, seconds, N_ITEMS / seconds); | |
break; | |
} | |
i++; | |
} | |
return NULL; | |
} | |
int main() | |
{ | |
pthread_t t1, t2; | |
pipe_t* p = pipe_new(sizeof(int), 0); | |
pipe_producer_t* producer = pipe_producer_new(p); | |
pipe_consumer_t* consumer = pipe_consumer_new(p); | |
pthread_create(&t1, NULL, producer_thread, producer); | |
pthread_create(&t2, NULL, consumer_threat, consumer); | |
pthread_join(t2, NULL); | |
pthread_cancel(t1); | |
pipe_free(p); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment