--- /dev/null
+/* Simple benchmark for Amazon S3: measures download speeds for
+ * differently-sized objects and with a variable number of parallel
+ * connections. */
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <pthread.h>
+#include <time.h>
+#include <unistd.h>
+#include <math.h>
+
+#include "libs3.h"
+
+FILE *statsfile;
+
+S3BucketContext bucket;
+
+struct thread_state {
+ pthread_t thread;
+ int thread_num;
+ long long timestamp;
+
+ // Time when first bytes of the response were received
+ long long first_byte_timestamp;
+
+ // Statistics for computing mean and standard deviation
+ int n;
+ size_t bytes_sent;
+ double sum_x, sum_x2;
+
+ double sum_f;
+};
+
+struct callback_state {
+ struct thread_state *ts;
+ size_t bytes_remaining;
+};
+
+#define MAX_THREADS 128
+struct thread_state threads[MAX_THREADS];
+
+int experiment_threads, experiment_size, experiment_objects;
+
+pthread_mutex_t barrier_mutex;
+pthread_cond_t barrier_cond;
+int barrier_val;
+
+pthread_mutex_t wait_mutex;
+pthread_cond_t wait_cond;
+int wait_val = 0;
+
+enum phase { LAUNCH, MEASURE, TERMINATE };
+volatile enum phase test_phase;
+
+void barrier_signal()
+{
+ pthread_mutex_lock(&barrier_mutex);
+ barrier_val--;
+ printf("Barrier: %d left\n", barrier_val);
+ if (barrier_val == 0)
+ pthread_cond_signal(&barrier_cond);
+ pthread_mutex_unlock(&barrier_mutex);
+}
+
+long long get_ns()
+{
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+
+ return ts.tv_sec * 1000000000LL + ts.tv_nsec;
+}
+
+static S3Status data_callback(int bufferSize, const char *buffer,
+ void *callbackData)
+{
+ struct callback_state *state = (struct callback_state *)callbackData;
+ state->bytes_remaining -= bufferSize;
+ if (state->ts->first_byte_timestamp == 0)
+ state->ts->first_byte_timestamp = get_ns();
+ return S3StatusOK;
+}
+
+static S3Status properties_callback(const S3ResponseProperties *properties,
+ void *callbackData)
+{
+ return S3StatusOK;
+}
+
+static void complete_callback(S3Status status,
+ const S3ErrorDetails *errorDetails,
+ void *callbackData)
+{
+}
+
+static void do_get(const char *key, size_t bytes, struct thread_state *ts)
+{
+ struct callback_state state;
+ struct S3GetObjectHandler handler;
+
+ state.bytes_remaining = bytes;
+ state.ts = ts;
+ handler.responseHandler.propertiesCallback = properties_callback;
+ handler.responseHandler.completeCallback = complete_callback;
+ handler.getObjectDataCallback = data_callback;
+
+ S3_get_object(&bucket, key, NULL, 0, 0, NULL, &handler, &state);
+}
+
+void *benchmark_thread(void *arg)
+{
+ struct thread_state *ts = (struct thread_state *)arg;
+ char namebuf[64];
+
+ printf("Warming up...\n");
+ do_get("file-1048576-0", 0, ts);
+ printf("Ready.\n");
+ barrier_signal();
+
+ pthread_mutex_lock(&wait_mutex);
+ while (wait_val == 0)
+ pthread_cond_wait(&wait_cond, &wait_mutex);
+ pthread_mutex_unlock(&wait_mutex);
+
+ ts->timestamp = get_ns();
+ sprintf(namebuf, "file-%d-%d", experiment_size, ts->thread_num);
+ ts->first_byte_timestamp = 0;
+ do_get(namebuf, experiment_size, ts);
+ long long timestamp = get_ns();
+ long long elapsed = timestamp - ts->timestamp;
+
+ barrier_signal();
+
+ printf("Thread %d: %f elapsed\n", ts->thread_num, elapsed / 1e9);
+
+ return NULL;
+}
+
+void launch_thread(int n)
+{
+ threads[n].thread_num = n;
+ if (pthread_create(&threads[n].thread, NULL, benchmark_thread, &threads[n]) != 0) {
+ fprintf(stderr, "Error launching thread!\n");
+ exit(1);
+ }
+}
+
+void wait_thread(int n)
+{
+ void *result;
+ pthread_join(threads[n].thread, &result);
+}
+
+void launch_test(int thread_count)
+{
+ int i;
+
+ barrier_val = thread_count;
+ assert(thread_count <= MAX_THREADS);
+
+ for (i = 0; i < thread_count; i++)
+ launch_thread(i);
+
+ /* Wait until all threads are ready. */
+ pthread_mutex_lock(&barrier_mutex);
+ while (barrier_val > 0) {
+ pthread_cond_wait(&barrier_cond, &barrier_mutex);
+ }
+ pthread_mutex_unlock(&barrier_mutex);
+
+ sleep(2);
+ barrier_val = thread_count;
+ pthread_mutex_lock(&wait_mutex);
+ printf("Launching test\n");
+ long long start_time = get_ns();
+ wait_val = 1;
+ pthread_cond_broadcast(&wait_cond);
+ pthread_mutex_unlock(&wait_mutex);
+
+ /* Wait until all threads are ready. */
+ pthread_mutex_lock(&barrier_mutex);
+ while (barrier_val > 0) {
+ pthread_cond_wait(&barrier_cond, &barrier_mutex);
+ }
+ pthread_mutex_unlock(&barrier_mutex);
+
+ long long end_time = get_ns();
+
+ printf("Elapsed time: %f\n", (end_time - start_time) / 1e9);
+ fprintf(statsfile, "%d\t%d\t%f\n", experiment_threads, experiment_size,
+ (end_time - start_time) / 1e9);
+}
+
+int main(int argc, char *argv[])
+{
+ statsfile = fopen("readlatency.data", "a");
+ if (statsfile == NULL) {
+ perror("open stats file");
+ return 1;
+ }
+
+ S3_initialize(NULL, S3_INIT_ALL);
+
+ bucket.bucketName = "mvrable-benchmark";
+ bucket.protocol = S3ProtocolHTTP;
+ bucket.uriStyle = S3UriStylePath;
+ bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
+ bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
+
+ pthread_mutex_init(&barrier_mutex, NULL);
+ pthread_cond_init(&barrier_cond, NULL);
+ pthread_mutex_init(&wait_mutex, NULL);
+ pthread_cond_init(&wait_cond, NULL);
+
+ if (argc < 3) {
+ fprintf(stderr, "Usage: %s <threads> <size>\n", argv[0]);
+ return 1;
+ }
+
+ experiment_threads = atoi(argv[1]);
+ experiment_size = atoi(argv[2]);
+ launch_test(experiment_threads);
+
+ printf("Done.\n");
+ fclose(statsfile);
+
+ return 0;
+}
--- /dev/null
+#!/usr/bin/gnuplot -persist
+#
+#
+# G N U P L O T
+# Version 4.2 patchlevel 5
+# last modified Mar 2009
+# System: Linux 2.6.31-20-generic
+#
+# Copyright (C) 1986 - 1993, 1998, 2004, 2007 - 2009
+# Thomas Williams, Colin Kelley and many others
+#
+# Type `help` to access the on-line reference manual.
+# The gnuplot FAQ is available from http://www.gnuplot.info/faq/
+#
+# Send bug reports and suggestions to <http://sourceforge.net/projects/gnuplot>
+#
+# set terminal wxt 0
+# set output
+unset clip points
+set clip one
+unset clip two
+set bar 1.000000
+set border 31 front linetype -1 linewidth 1.000
+set xdata
+set ydata
+set zdata
+set x2data
+set y2data
+set timefmt x "%d/%m/%y,%H:%M"
+set timefmt y "%d/%m/%y,%H:%M"
+set timefmt z "%d/%m/%y,%H:%M"
+set timefmt x2 "%d/%m/%y,%H:%M"
+set timefmt y2 "%d/%m/%y,%H:%M"
+set timefmt cb "%d/%m/%y,%H:%M"
+set boxwidth
+set style fill empty border
+set style rectangle back fc lt -3 fillstyle solid 1.00 border -1
+set dummy x,y
+set format x "% g"
+set format y "% g"
+set format x2 "% g"
+set format y2 "% g"
+set format z "% g"
+set format cb "% g"
+set angles radians
+set grid nopolar
+set grid xtics nomxtics ytics nomytics noztics nomztics \
+ nox2tics nomx2tics noy2tics nomy2tics nocbtics nomcbtics
+set grid layerdefault linetype 0 linewidth 1.000, linetype 0 linewidth 1.000
+set key title ""
+set key inside right bottom vertical Right noreverse enhanced autotitles nobox
+set key noinvert samplen 4 spacing 1 width 0 height 0
+unset label
+unset arrow
+set style increment default
+unset style line
+unset style arrow
+set style histogram clustered gap 2 title offset character 0, 0, 0
+unset logscale
+set offsets 0, 0, 0, 0
+set pointsize 1
+set encoding default
+unset polar
+unset parametric
+unset decimalsign
+set view 60, 30, 1, 1
+set view
+set samples 100, 100
+set isosamples 10, 10
+set surface
+unset contour
+set clabel '%8.3g'
+set mapping cartesian
+set datafile separator whitespace
+unset hidden3d
+set cntrparam order 4
+set cntrparam linear
+set cntrparam levels auto 5
+set cntrparam points 5
+set size ratio 0 1,1
+set origin 0,0
+set style data points
+set style function lines
+set xzeroaxis linetype -2 linewidth 1.000
+set yzeroaxis linetype -2 linewidth 1.000
+set zzeroaxis linetype -2 linewidth 1.000
+set x2zeroaxis linetype -2 linewidth 1.000
+set y2zeroaxis linetype -2 linewidth 1.000
+set ticslevel 0.5
+set mxtics default
+set mytics default
+set mztics default
+set mx2tics default
+set my2tics default
+set mcbtics default
+set xtics border in scale 1,0.5 mirror norotate offset character 0, 0, 0
+set xtics autofreq norangelimit
+set ytics border in scale 1,0.5 mirror norotate offset character 0, 0, 0
+set ytics autofreq norangelimit
+set ztics border in scale 1,0.5 nomirror norotate offset character 0, 0, 0
+set ztics autofreq norangelimit
+set nox2tics
+set noy2tics
+set cbtics border in scale 1,0.5 mirror norotate offset character 0, 0, 0
+set cbtics autofreq norangelimit
+set title "Time to Fetch 1 MB of Data"
+set title offset character 0, 0, 0 font "" norotate
+set timestamp bottom
+set timestamp ""
+set timestamp offset character 0, 0, 0 font "" norotate
+set rrange [ * : * ] noreverse nowriteback # (currently [0.00000:10.0000] )
+set trange [ * : * ] noreverse nowriteback # (currently [-5.00000:5.00000] )
+set urange [ * : * ] noreverse nowriteback # (currently [-5.00000:5.00000] )
+set vrange [ * : * ] noreverse nowriteback # (currently [-5.00000:5.00000] )
+set xlabel "Completion Time (s)"
+set xlabel offset character 0, 0, 0 font "" textcolor lt -1 norotate
+set x2label ""
+set x2label offset character 0, 0, 0 font "" textcolor lt -1 norotate
+set xrange [ * : * ] noreverse nowriteback # (currently [-10.0000:10.0000] )
+set x2range [ * : * ] noreverse nowriteback # (currently [-10.0000:10.0000] )
+set ylabel "Fraction of Requests"
+set ylabel offset character 0, 0, 0 font "" textcolor lt -1 rotate by 90
+set y2label ""
+set y2label offset character 0, 0, 0 font "" textcolor lt -1 rotate by 90
+set yrange [ * : * ] noreverse nowriteback # (currently [-10.0000:10.0000] )
+set y2range [ * : * ] noreverse nowriteback # (currently [-10.0000:10.0000] )
+set zlabel ""
+set zlabel offset character 0, 0, 0 font "" textcolor lt -1 norotate
+set zrange [ * : * ] noreverse nowriteback # (currently [-10.0000:10.0000] )
+set cblabel ""
+set cblabel offset character 0, 0, 0 font "" textcolor lt -1 rotate by 90
+set cbrange [ * : * ] noreverse nowriteback # (currently [-10.0000:10.0000] )
+set zero 1e-08
+set lmargin -1
+set bmargin -1
+set rmargin -1
+set tmargin -1
+set locale "C"
+set pm3d explicit at s
+set pm3d scansautomatic
+set pm3d interpolate 1,1 flush begin noftriangles nohidden3d corners2color mean
+set palette positive nops_allcF maxcolors 0 gamma 1.5 color model RGB
+set palette rgbformulae 7, 5, 15
+set colorbox default
+set colorbox vertical origin screen 0.9, 0.2, 0 size screen 0.05, 0.6, 0 front bdefault
+set loadpath
+set fontpath
+set fit noerrorvariables
+GNUTERM = "wxt"
+plot [0:3] [0:1] "readlatency-32.cdf" using 1:($2/100.0) with lines title "32 Parallel Connections", "readlatency-1M.cdf" using 1:($2/100.0) with lines title "1 Connection", "readlatency-1.cdf" using 1:($2/200.0) with lines title "Single 32 kB Request"
+# EOF