Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / cloudbench / cloudtest.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2010  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 # Run a series of simple test requests against S3 for gathering some basic
31 # performance numbers.
32
33 import boto, time
34 from boto.s3.connection import SubdomainCallingFormat
35 from boto.s3.key import Key
36 import azure
37
38 BUCKET_NAME = 'mvrable-benchmark'
39 SIZES = [64, 4096, 32 << 10, 256 << 10, 1 << 20, 4 << 20, 32 << 20]
40
41 class S3TestConnection:
42     def __init__(self):
43         self.conn = boto.connect_s3(is_secure=False,
44                                     calling_format=SubdomainCallingFormat())
45         self.bucket = self.conn.get_bucket(BUCKET_NAME)
46
47     def put_object(self, name, size):
48         buf = 'A' * size
49         k = Key(self.bucket, name)
50         start_time = time.time()
51         k.set_contents_from_string(buf)
52         print "%s: %f" % (name, time.time() - start_time)
53
54     def get_object(self, name):
55         k = Key(self.bucket, name)
56         start_time = time.time()
57         buf = k.get_contents_as_string()
58         print "%s: %f" % (name, time.time() - start_time)
59
60 class AzureTestConnection:
61     def __init__(self):
62         self.conn = azure.Connection()
63
64     def put_object(self, name, size):
65         buf = 'A' * size
66         start_time = time.time()
67         self.conn.make_request('/benchmark/' + name, 'PUT', buf,
68                                {'x-ms-blob-type': 'BlockBlob'})
69         print "%s: %f" % (name, time.time() - start_time)
70
71     def get_object(self, name):
72         start_time = time.time()
73         self.conn.make_request('/benchmark/' + name, 'GET')
74         print "%s: %f" % (name, time.time() - start_time)
75
76 def run_test():
77     print "==== S3 ===="
78     c = S3TestConnection()
79     for repeat in range(4):
80         for size in SIZES:
81             c.put_object('file-%d-%d' % (size, repeat), size)
82
83     c = S3TestConnection()
84     for repeat in range(4):
85         for size in SIZES:
86             c.get_object('file-%d-%d' % (size, repeat))
87
88     print "==== AZURE ===="
89     c = AzureTestConnection()
90     for repeat in range(4):
91         for size in SIZES:
92             c.put_object('file-%d-%d' % (size, repeat), size)
93
94     c = AzureTestConnection()
95     for repeat in range(4):
96         for size in SIZES:
97             c.get_object('file-%d-%d' % (size, repeat))
98
99 if __name__ == '__main__':
100     #run_test()
101     SIZES = [4096, 32 << 10, 256 << 10, 1 << 20, 4 << 20]
102     PRIME = (1 << 20) + (1 << 10)
103     c = AzureTestConnection()
104     for size in SIZES:
105         c.put_object('file-%d-%d' % (size, 0), size)
106     c.put_object('file-%d-%d' % (PRIME, 0), PRIME)
107
108     for size in SIZES:
109         for n in range(50):
110             c = AzureTestConnection()
111             c.get_object('file-%d-%d' % (PRIME, 0))
112             time.sleep(2.0)
113             c.get_object('file-%d-%d' % (size, 0))