1 /* Simple benchmark for Amazon S3: measures download speeds for
2 * differently-sized objects and with a variable number of parallel
19 S3BucketContext bucket;
26 // Time when first bytes of the response were received
27 long long first_byte_timestamp;
29 // Statistics for computing mean and standard deviation
37 struct callback_state {
38 struct thread_state *ts;
39 size_t bytes_remaining;
42 #define MAX_THREADS 128
43 struct thread_state threads[MAX_THREADS];
45 int experiment_threads, experiment_size, experiment_objects;
47 pthread_mutex_t barrier_mutex;
48 pthread_cond_t barrier_cond;
51 enum phase { LAUNCH, MEASURE, TERMINATE };
52 volatile enum phase test_phase;
56 pthread_mutex_lock(&barrier_mutex);
58 printf("Barrier: %d left\n", barrier_val);
60 pthread_cond_signal(&barrier_cond);
61 pthread_mutex_unlock(&barrier_mutex);
67 clock_gettime(CLOCK_MONOTONIC, &ts);
69 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
72 static S3Status data_callback(int bufferSize, const char *buffer,
75 struct callback_state *state = (struct callback_state *)callbackData;
76 state->bytes_remaining -= bufferSize;
77 if (state->ts->first_byte_timestamp == 0)
78 state->ts->first_byte_timestamp = get_ns();
82 static S3Status properties_callback(const S3ResponseProperties *properties,
88 static void complete_callback(S3Status status,
89 const S3ErrorDetails *errorDetails,
94 static void do_get(const char *key, size_t bytes, struct thread_state *ts)
96 struct callback_state state;
97 struct S3GetObjectHandler handler;
99 state.bytes_remaining = bytes;
101 handler.responseHandler.propertiesCallback = properties_callback;
102 handler.responseHandler.completeCallback = complete_callback;
103 handler.getObjectDataCallback = data_callback;
105 S3_get_object(&bucket, key, NULL, 0, 0, NULL, &handler, &state);
108 void *benchmark_thread(void *arg)
110 struct thread_state *ts = (struct thread_state *)arg;
117 ts->sum_x = ts->sum_x2 = ts->sum_f = 0.0;
120 ts->timestamp = get_ns();
121 while (test_phase != TERMINATE) {
122 int object = random() % experiment_objects;
123 sprintf(namebuf, "file-%d-%d", experiment_size, object);
124 ts->first_byte_timestamp = 0;
125 do_get(namebuf, experiment_size, ts);
126 long long timestamp = get_ns();
127 long long elapsed = timestamp - ts->timestamp;
129 printf("Elapsed[%d-%d]: %lld ns\n", ts->thread_num, i, elapsed);
130 printf(" first data after: %lld ns\n",
131 ts->first_byte_timestamp - ts->timestamp);
132 if (measuring && test_phase == MEASURE) {
133 double e = elapsed / 1e9;
134 double f = (ts->first_byte_timestamp - ts->timestamp) / 1e9;
139 ts->bytes_sent += experiment_size;
143 if (stage == 0 && i > 2) {
146 } else if (stage == 1 && ts->n >= 2) {
151 ts->timestamp = timestamp;
152 if (test_phase == MEASURE)
159 void launch_thread(int n)
161 threads[n].thread_num = n;
162 if (pthread_create(&threads[n].thread, NULL, benchmark_thread, &threads[n]) != 0) {
163 fprintf(stderr, "Error launching thread!\n");
168 void wait_thread(int n)
171 pthread_join(threads[n].thread, &result);
174 void launch_test(int thread_count)
177 long long start_time = get_ns();
180 barrier_val = thread_count;
181 assert(thread_count <= MAX_THREADS);
183 printf("Launching...\n");
185 for (i = 0; i < thread_count; i++)
188 /* Wait until all threads are ready. */
189 pthread_mutex_lock(&barrier_mutex);
190 while (barrier_val > 0) {
191 pthread_cond_wait(&barrier_cond, &barrier_mutex);
193 pthread_mutex_unlock(&barrier_mutex);
195 printf("Measuring...\n");
196 barrier_val = thread_count;
197 test_phase = MEASURE;
199 /* Ensure all threads have measured some activity, then a bit more. */
200 pthread_mutex_lock(&barrier_mutex);
201 while (barrier_val > 0) {
202 pthread_cond_wait(&barrier_cond, &barrier_mutex);
204 pthread_mutex_unlock(&barrier_mutex);
205 printf("Data in from all threads...\n");
208 printf("Terminating...\n");
209 test_phase = TERMINATE;
211 for (i = 0; i < thread_count; i++)
215 double sum_x = 0.0, sum_x2 = 0.0, sum_f = 0.0;
216 double bandwidth = 0.0;
217 for (i = 0; i < thread_count; i++) {
219 sum_x += threads[i].sum_x;
220 sum_x2 += threads[i].sum_x2;
221 sum_f += threads[i].sum_f;
222 bandwidth += threads[i].bytes_sent / threads[i].sum_x;
225 double elapsed = (get_ns() - start_time) / 1e9;
226 printf("*** %d threads, %d byte objects\n",
227 experiment_threads, experiment_size);
228 printf("Elapsed: %f s\n", elapsed);
229 printf("Data points: %d\n", n);
230 double mx = sum_x / n;
231 double sx = sqrt((sum_x2 - 2*sum_x*mx + n*mx*mx) / (n - 1));
232 printf("Time: %f ± %f s\n", mx, sx);
233 printf("Latency to first byte: %f\n", sum_f / n);
234 printf("Bandwidth: %f B/s\n", bandwidth);
236 fprintf(statsfile, "%d\t%d\t%f\t%d\t%f\t%f\t%f\n",
237 experiment_threads, experiment_size, elapsed, n,
240 printf("Finished.\n");
243 int main(int argc, char *argv[])
245 statsfile = fopen("readbench.data", "a");
246 if (statsfile == NULL) {
247 perror("open stats file");
251 S3_initialize(NULL, S3_INIT_ALL);
253 bucket.bucketName = "mvrable-benchmark";
254 bucket.protocol = S3ProtocolHTTP;
255 bucket.uriStyle = S3UriStylePath;
256 bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
257 bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
259 pthread_mutex_init(&barrier_mutex, NULL);
260 pthread_cond_init(&barrier_cond, NULL);
263 fprintf(stderr, "Usage: %s <threads> <size> <object-count>\n", argv[0]);
267 experiment_threads = atoi(argv[1]);
268 experiment_size = atoi(argv[2]);
269 experiment_objects = atoi(argv[3]);
270 assert(experiment_objects > 0);
271 launch_test(experiment_threads);