Permalink
Browse files

add simple benchmark

  • Loading branch information...
1 parent bb54992 commit 6696e8a7c3ed0b20106332b30cfcb7f2dd3ae317 @deepfryed committed Mar 15, 2012
Showing with 78 additions and 1 deletion.
  1. +1 −0 .gitignore
  2. +7 −0 beanstalk.c
  3. +66 −0 benchmark.cc
  4. +4 −1 makefile
View
@@ -6,3 +6,4 @@ examples/**/*
test/*
!test/run-all
!test/*.cc
+benchmark
View
@@ -356,6 +356,10 @@ int bs_reserve_job(int fd, char *command, BSJ **result) {
BSJ *job;
BSM *message;
+// XXX: debug
+// struct timeval start, end;
+// gettimeofday(&start, 0);
+
BS_SEND(fd, command, strlen(command));
message = bs_recv_message(fd, BS_MESSAGE_HAS_BODY);
BS_CHECK_MESSAGE(message);
@@ -372,6 +376,9 @@ int bs_reserve_job(int fd, char *command, BSJ **result) {
message->data = 0;
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;
}
View
@@ -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;
+}
View
@@ -12,7 +12,7 @@ LDFLAGS = -L. -lbeanstalk
CC = gcc
CPP = g++
-all: $(CEXAMPLES) $(CPPEXAMPLES)
+all: $(CEXAMPLES) $(CPPEXAMPLES) benchmark
test: $(TESTS)
test/run-all
@@ -23,6 +23,9 @@ $(TESTS): test/%:test/%.o $(SHAREDLIB)
test/%.o: test/%.cc
$(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
$(CC) -o $@ $< $(LDFLAGS)

0 comments on commit 6696e8a

Please sign in to comment.