-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -6,3 +6,4 @@ examples/**/* | ||
test/* | test/* | ||
!test/run-all | !test/run-all | ||
!test/*.cc | !test/*.cc | ||
benchmark |
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
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,66 @@ | |||
#include "beanstalk.hpp" | |||
#include <iostream> | |||
#include <assert.h> | |||
#include <stdexcept> | |||
#include <pthread.h> | |||
#include <sys/time.h> | |||
|
|||
using namespace std; | |||
using namespace Beanstalk; | |||
|
|||
typedef struct timeval timeval; | |||
|
|||
#define WINDOW 1000 | |||
|
|||
double duration(timeval *start, timeval *end) { | |||
return (double)((end->tv_sec * 1000000 + end->tv_usec) - (start->tv_sec * 1000000 + start->tv_usec)) / 1000000.0; | |||
} | |||
|
|||
void* producer(void *arg) { | |||
Client client("127.0.0.1", 11300); | |||
|
|||
timeval start, end; | |||
gettimeofday(&start, 0); | |||
for (int i = 0; i < 100000; i++) { | |||
client.put("hello world"); | |||
if (i > 0 && i % WINDOW == 0) { | |||
gettimeofday(&end, 0); | |||
printf("producer rate: %.lf\n", (double)WINDOW / duration(&start, &end)); | |||
memcpy(&start, &end, sizeof(end)); | |||
} | |||
} | |||
|
|||
return 0; | |||
} | |||
|
|||
void* consumer(void *arg) { | |||
Client client("127.0.0.1", 11300); | |||
|
|||
Job job; | |||
size_t jobs = 0; | |||
timeval start, end; | |||
gettimeofday(&start, 0); | |||
while (client.reserve(job)) { | |||
jobs++; | |||
client.del(job.id()); | |||
if (jobs % WINDOW == 0) { | |||
gettimeofday(&end, 0); | |||
printf("consumer rate: %.lf\n", (float)WINDOW / duration(&start, &end)); | |||
memcpy(&start, &end, sizeof(end)); | |||
} | |||
} | |||
|
|||
return 0; | |||
} | |||
|
|||
int main() { | |||
pthread_t producer_thread, consumer_thread; | |||
|
|||
pthread_create(&producer_thread, 0, producer, 0); | |||
pthread_create(&consumer_thread, 0, consumer, 0); | |||
|
|||
pthread_join(producer_thread, 0); | |||
pthread_join(consumer_thread, 0); | |||
|
|||
return 0; | |||
} |
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