Skip to content

Commit

Permalink
Start of benchmark script
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeard0531 committed Nov 18, 2009
1 parent c40e7c6 commit 6870e7b
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,6 +5,7 @@
.scon*

test_*
benchmark

tags

Expand Down
20 changes: 16 additions & 4 deletions SConstruct
Expand Up @@ -16,6 +16,12 @@ AddOption('--c99',
action='store_true',
help='Compile with c99 (recommended for gcc)')

AddOption('--d',
dest='optimize',
default=True,
action='store_false',
help='disable optimizations')

import os
import sys

Expand All @@ -27,6 +33,9 @@ if "darwin" == os.sys.platform or "linux2" == os.sys.platform:
env.Append( LIBPATH=["/opt/local/lib/"] )
if GetOption('use_c99'):
env.Append( CPPFLAGS=" -std=c99 " )
if GetOption('optimize'):
env.Append( CPPFLAGS=" -O3 " )
# -O3 benchmarks *significantly* faster than -O2 when disabling networking


#we shouldn't need these options in c99 mode
Expand Down Expand Up @@ -66,11 +75,14 @@ b = env.Library( "bson" , coreFiles + [ "src/bson.c"] )

env.Default( env.Alias( "lib" , [ m[0] , b[0] ] ) )

testEnv = env.Clone()
benchmarkEnv = env.Clone()
benchmarkEnv.Append( CFLAGS=[r'-DTEST_SERVER=\"%s\"'%GetOption('test_server')] )
benchmarkEnv.Append( LIBS=[m, b] )
benchmarkEnv.Prepend( LIBPATH=["."] )
benchmarkEnv.Program( "benchmark" , coreFiles + [ "test/benchmark.c"] )

testEnv = benchmarkEnv.Clone()
testEnv.Append( LIBS=["json"] )
testEnv.Append( LIBS=[m, b] )
testEnv.Append( CFLAGS=['-DTEST_SERVER=\\"%s\\"'%GetOption('test_server')] )
testEnv.Prepend( LIBPATH=["."] )

testCoreFiles = [ "test/md5.c" ]

Expand Down
232 changes: 232 additions & 0 deletions test/benchmark.c
@@ -0,0 +1,232 @@
/* test.c */

#include "mongo.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>

#define ASSERT(x) \
do{ \
if(!(x)){ \
printf("failed assert (%d): %s\n", __LINE__, #x); \
exit(1); \
}\
}while(0)

/* supports preprocessor concatenation */
#define DB "benchmarks"

#define PER_TRIAL 5000
#define BATCH_SIZE 100

static mongo_connection conn;

static void make_small(bson * out, int i){
bson_buffer bb;
bson_buffer_init(&bb);
bson_append_new_oid(&bb, "_id");
bson_append_int(&bb, "x", i);
bson_from_buffer(out, &bb);
}

static void make_medium(bson * out, int i){
bson_buffer bb;
bson_buffer_init(&bb);
bson_append_new_oid(&bb, "_id");
bson_append_int(&bb, "x", i);
bson_append_int(&bb, "integer", 5);
bson_append_double(&bb, "number", 5.05);
bson_append_bool(&bb, "boolean", 0);

bson_append_start_array(&bb, "array");
bson_append_string(&bb, "0", "test");
bson_append_string(&bb, "1", "benchmark");
bson_append_finish_object(&bb);

bson_from_buffer(out, &bb);
}

static const char *words[14] =
{"10gen","web","open","source","application","paas",
"platform-as-a-service","technology","helps",
"developers","focus","building","mongodb","mongo"};

static void make_large(bson * out, int i){
int num;
char numstr[4];
bson_buffer bb;
bson_buffer_init(&bb);

bson_append_new_oid(&bb, "_id");
bson_append_int(&bb, "x", i);
bson_append_string(&bb, "base_url", "http://www.example.com/test-me");
bson_append_int(&bb, "total_word_count", 6743);
bson_append_int(&bb, "access_time", 999); /*TODO use date*/

bson_append_start_object(&bb, "meta_tags");
bson_append_string(&bb, "description", "i am a long description string");
bson_append_string(&bb, "author", "Holly Man");
bson_append_string(&bb, "dynamically_created_meta_tag", "who know\n what");
bson_append_finish_object(&bb);

bson_append_start_object(&bb, "page_structure");
bson_append_int(&bb, "counted_tags", 3450);
bson_append_int(&bb, "no_of_js_attached", 10);
bson_append_int(&bb, "no_of_images", 6);
bson_append_finish_object(&bb);


bson_append_start_array(&bb, "harvested_words");
for (num=0; num < 14*20; num++){
sprintf(numstr, "%d", num);
bson_append_string(&bb, numstr, words[i%14]);
}
bson_append_finish_object(&bb);

bson_from_buffer(out, &bb);
}

static void single_insert_small_test(){
int i;
bson b;
for (i=0; i<PER_TRIAL; i++){
make_small(&b, i);
mongo_insert(&conn, DB ".single.small", &b);
bson_destroy(&b);
}
}

static void single_insert_medium_test(){
int i;
bson b;
for (i=0; i<PER_TRIAL; i++){
make_medium(&b, i);
mongo_insert(&conn, DB ".single.medium", &b);
bson_destroy(&b);
}
}

static void single_insert_large_test(){
int i;
bson b;
for (i=0; i<PER_TRIAL; i++){
make_large(&b, i);
mongo_insert(&conn, DB ".single.large", &b);
bson_destroy(&b);
}
}

static void batch_insert_small_test(){
int i, j;
bson b[BATCH_SIZE];
bson *bp[BATCH_SIZE];
for (j=0; j < BATCH_SIZE; j++)
bp[j] = &b[j];

for (i=0; i < (PER_TRIAL / BATCH_SIZE); i++){
for (j=0; j < BATCH_SIZE; j++)
make_small(&b[j], i);

mongo_insert_batch(&conn, DB ".batch.small", bp, BATCH_SIZE);

for (j=0; j < BATCH_SIZE; j++)
bson_destroy(&b[j]);
}
}

static void batch_insert_medium_test(){
int i, j;
bson b[BATCH_SIZE];
bson *bp[BATCH_SIZE];
for (j=0; j < BATCH_SIZE; j++)
bp[j] = &b[j];

for (i=0; i < (PER_TRIAL / BATCH_SIZE); i++){
for (j=0; j < BATCH_SIZE; j++)
make_medium(&b[j], i);

mongo_insert_batch(&conn, DB ".batch.medium", bp, BATCH_SIZE);

for (j=0; j < BATCH_SIZE; j++)
bson_destroy(&b[j]);
}
}

static void batch_insert_large_test(){
int i, j;
bson b[BATCH_SIZE];
bson *bp[BATCH_SIZE];
for (j=0; j < BATCH_SIZE; j++)
bp[j] = &b[j];

for (i=0; i < (PER_TRIAL / BATCH_SIZE); i++){
for (j=0; j < BATCH_SIZE; j++)
make_large(&b[j], i);

mongo_insert_batch(&conn, DB ".batch.large", bp, BATCH_SIZE);

for (j=0; j < BATCH_SIZE; j++)
bson_destroy(&b[j]);
}
}

typedef void(*nullary)();
static void time_it(nullary func, const char* name){
struct timeval start, end;
double timer;
double ops;

gettimeofday(&start, NULL);
func();
ASSERT(!mongo_cmd_get_last_error(&conn, DB, NULL));
gettimeofday(&end, NULL);

timer = end.tv_sec - start.tv_sec;
timer *= 1000000;
timer += end.tv_usec - start.tv_usec;

ops = PER_TRIAL / timer;
ops *= 1000000;

printf("%-45s\t%15f\n", name, ops);
}

#define TIME(func) (time_it(func , #func))

static void clean(){
bson b;
if (!mongo_cmd_drop_db(&conn, DB)){
printf("failed to drop db\n");
exit(1);
}

/* create the db */
mongo_insert(&conn, DB ".creation", bson_empty(&b));
ASSERT(!mongo_cmd_get_last_error(&conn, DB, NULL));
}

int main(){
mongo_connection_options opts;

strncpy(opts.host, TEST_SERVER, 255);
opts.host[254] = '\0';
opts.port = 27017;

if (mongo_connect(&conn, &opts )){
printf("failed to connect\n");
exit(1);
}

clean();

TIME(single_insert_small_test);
TIME(single_insert_medium_test);
TIME(single_insert_large_test);

TIME(batch_insert_small_test);
TIME(batch_insert_medium_test);
TIME(batch_insert_large_test);

return 0;
}

0 comments on commit 6870e7b

Please sign in to comment.