Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / cloudbench / readdelay.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2011  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30
31 /* Simple benchmark for Amazon S3: measures download times to fetch files over
32  * a single connection, with variable amounts of delay between requests.  This
33  * is intended to test whether the TCP congestion control state gets reset and
34  * slow start needs to restart, depending on how long the connection was left
35  * idle. */
36
37 #include <assert.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stdint.h>
41 #include <string.h>
42 #include <pthread.h>
43 #include <time.h>
44 #include <unistd.h>
45 #include <math.h>
46
47 #include "libs3.h"
48
49 const char *key = NULL;
50
51 S3BucketContext bucket;
52
53 struct callback_state {
54     //struct thread_state *ts;
55     size_t bytes_remaining;
56 };
57
58 int64_t get_ns()
59 {
60     struct timespec ts;
61     clock_gettime(CLOCK_MONOTONIC, &ts);
62
63     return ts.tv_sec * 1000000000LL + ts.tv_nsec;
64 }
65
66 void do_sleep(int64_t delay)
67 {
68     if (delay <= 0)
69         return;
70
71     struct timespec t;
72     t.tv_sec = delay / 1000000000;
73     t.tv_nsec = delay % 1000000000;
74     nanosleep(&t, NULL);
75 }
76
77 static S3Status data_callback(int bufferSize, const char *buffer,
78                               void *callbackData)
79 {
80     struct callback_state *state = (struct callback_state *)callbackData;
81     state->bytes_remaining -= bufferSize;
82     /*
83     if (state->ts->first_byte_timestamp == 0)
84         state->ts->first_byte_timestamp = get_ns(); */
85     return S3StatusOK;
86 }
87
88 static S3Status properties_callback(const S3ResponseProperties *properties,
89                                      void *callbackData)
90 {
91     return S3StatusOK;
92 }
93
94 static void complete_callback(S3Status status,
95                               const S3ErrorDetails *errorDetails,
96                               void *callbackData)
97 {
98 }
99
100 static void do_get(const char *key, size_t bytes)
101 {
102     struct callback_state state;
103     struct S3GetObjectHandler handler;
104
105     state.bytes_remaining = bytes;
106     //state.ts = ts;
107     handler.responseHandler.propertiesCallback = properties_callback;
108     handler.responseHandler.completeCallback = complete_callback;
109     handler.getObjectDataCallback = data_callback;
110
111     S3_get_object(&bucket, key, NULL, 0, 0, NULL, &handler, &state);
112 }
113
114 void run_test(int64_t delay_ns)
115 {
116     for (int i = 0; i <= 25; i++) {
117         int64_t start, finish;
118         start = get_ns();
119         do_get(key, 0);
120         finish = get_ns();
121
122         int64_t elapsed = finish - start;
123         if (i > 0) {
124             printf("%f\n", elapsed / 1e9);
125             fflush(stdout);
126         }
127
128         do_sleep(delay_ns);
129     }
130 }
131
132 int main(int argc, char *argv[])
133 {
134     S3_initialize(NULL, S3_INIT_ALL, NULL);
135
136     bucket.bucketName = "mvrable-benchmark";
137     bucket.protocol = S3ProtocolHTTP;
138     bucket.uriStyle = S3UriStyleVirtualHost;
139     bucket.accessKeyId = getenv("AWS_ACCESS_KEY_ID");
140     bucket.secretAccessKey = getenv("AWS_SECRET_ACCESS_KEY");
141
142     if (argc != 3) {
143         fprintf(stderr, "Usage: %s <file> <delay>\n", argv[0]);
144         return 1;
145     }
146
147     key = argv[1];
148     double inter_request_delay = atof(argv[2]);
149     run_test(inter_request_delay * 1e9);
150
151     return 0;
152 }