cc82c7afb37a130d16f6eb740a40edf442baa833
[bluesky.git] / cloudbench / readdelay.c
1 /* Simple benchmark for Amazon S3: measures download times to fetch files over
2  * a single connection, with variable amounts of delay between requests.  This
3  * is intended to test whether the TCP congestion control state gets reset and
4  * slow start needs to restart, depending on how long the connection was left
5  * idle. */
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 #include <string.h>
12 #include <pthread.h>
13 #include <time.h>
14 #include <unistd.h>
15 #include <math.h>
16
17 #include "libs3.h"
18
19 const char *key = NULL;
20
21 S3BucketContext bucket;
22
23 struct callback_state {
24     //struct thread_state *ts;
25     size_t bytes_remaining;
26 };
27
28 int64_t get_ns()
29 {
30     struct timespec ts;
31     clock_gettime(CLOCK_MONOTONIC, &ts);
32
33     return ts.tv_sec * 1000000000LL + ts.tv_nsec;
34 }
35
36 void do_sleep(int64_t delay)
37 {
38     if (delay <= 0)
39         return;
40
41     struct timespec t;
42     t.tv_sec = delay / 1000000000;
43     t.tv_nsec = delay % 1000000000;
44     nanosleep(&t, NULL);
45 }
46
47 static S3Status data_callback(int bufferSize, const char *buffer,
48                               void *callbackData)
49 {
50     struct callback_state *state = (struct callback_state *)callbackData;
51     state->bytes_remaining -= bufferSize;
52     /*
53     if (state->ts->first_byte_timestamp == 0)
54         state->ts->first_byte_timestamp = get_ns(); */
55     return S3StatusOK;
56 }
57
58 static S3Status properties_callback(const S3ResponseProperties *properties,
59                                      void *callbackData)
60 {
61     return S3StatusOK;
62 }
63
64 static void complete_callback(S3Status status,
65                               const S3ErrorDetails *errorDetails,
66                               void *callbackData)
67 {
68 }
69
70 static void do_get(const char *key, size_t bytes)
71 {
72     struct callback_state state;
73     struct S3GetObjectHandler handler;
74
75     state.bytes_remaining = bytes;
76     //state.ts = ts;
77     handler.responseHandler.propertiesCallback = properties_callback;
78     handler.responseHandler.completeCallback = complete_callback;
79     handler.getObjectDataCallback = data_callback;
80
81     S3_get_object(&bucket, key, NULL, 0, 0, NULL, &handler, &state);
82 }
83
84 void run_test(int64_t delay_ns)
85 {
86     for (int i = 0; i <= 25; i++) {
87         int64_t start, finish;
88         start = get_ns();
89         do_get(key, 0);
90         finish = get_ns();
91
92         int64_t elapsed = finish - start;
93         if (i > 0) {
94             printf("%f\n", elapsed / 1e9);
95             fflush(stdout);
96         }
97
98         do_sleep(delay_ns);
99     }
100 }
101
102 int main(int argc, char *argv[])
103 {
104     S3_initialize(NULL, S3_INIT_ALL, NULL);
105
106     bucket.bucketName = "mvrable-benchmark";
107     bucket.protocol = S3ProtocolHTTP;
108     bucket.uriStyle = S3UriStyleVirtualHost;
109     bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
110     bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
111
112     if (argc != 3) {
113         fprintf(stderr, "Usage: %s <file> <delay>\n", argv[0]);
114         return 1;
115     }
116
117     key = argv[1];
118     double inter_request_delay = atof(argv[2]);
119     run_test(inter_request_delay * 1e9);
120
121     return 0;
122 }