Skip to content

Commit

Permalink
add simple benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
deepfryed committed Mar 15, 2012
1 parent bb54992 commit 6696e8a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ examples/**/*
test/* test/*
!test/run-all !test/run-all
!test/*.cc !test/*.cc
benchmark
7 changes: 7 additions & 0 deletions beanstalk.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ int bs_reserve_job(int fd, char *command, BSJ **result) {
BSJ *job; BSJ *job;
BSM *message; BSM *message;


// XXX: debug
// struct timeval start, end;
// gettimeofday(&start, 0);

BS_SEND(fd, command, strlen(command)); BS_SEND(fd, command, strlen(command));
message = bs_recv_message(fd, BS_MESSAGE_HAS_BODY); message = bs_recv_message(fd, BS_MESSAGE_HAS_BODY);
BS_CHECK_MESSAGE(message); BS_CHECK_MESSAGE(message);
Expand All @@ -372,6 +376,9 @@ int bs_reserve_job(int fd, char *command, BSJ **result) {
message->data = 0; message->data = 0;
bs_free_message(message); bs_free_message(message);


// XXX: debug
// gettimeofday(&end, 0);
// printf("elapsed: %lu\n", (end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec));
return BS_STATUS_OK; return BS_STATUS_OK;
} }


Expand Down
66 changes: 66 additions & 0 deletions benchmark.cc
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;
}
5 changes: 4 additions & 1 deletion makefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LDFLAGS = -L. -lbeanstalk
CC = gcc CC = gcc
CPP = g++ CPP = g++


all: $(CEXAMPLES) $(CPPEXAMPLES) all: $(CEXAMPLES) $(CPPEXAMPLES) benchmark


test: $(TESTS) test: $(TESTS)
test/run-all test/run-all
Expand All @@ -23,6 +23,9 @@ $(TESTS): test/%:test/%.o $(SHAREDLIB)
test/%.o: test/%.cc test/%.o: test/%.cc
$(CPP) $(CFLAGS) -c -o $@ $< $(CPP) $(CFLAGS) -c -o $@ $<


benchmark: benchmark.cc libbeanstalk.so
$(CPP) $(CFLAGS) -o benchmark benchmark.cc $(LDFLAGS) -lpthread

$(CEXAMPLES): examples/c/%:examples/c/%.o libbeanstalk.so $(CEXAMPLES): examples/c/%:examples/c/%.o libbeanstalk.so
$(CC) -o $@ $< $(LDFLAGS) $(CC) -o $@ $< $(LDFLAGS)


Expand Down

0 comments on commit 6696e8a

Please sign in to comment.