31c188ab589cc92b5d54da9abff145f3ea6f26d6
[bluesky.git] / cloudbench / cloudtest.py
1 #!/usr/bin/python
2 #
3 # Run a series of simple test requests against S3 for gathering some basic
4 # performance numbers.
5
6 import boto, time
7 from boto.s3.connection import SubdomainCallingFormat
8 from boto.s3.key import Key
9 import azure
10
11 BUCKET_NAME = 'mvrable-benchmark'
12 SIZES = [64, 4096, 32 << 10, 256 << 10, 1 << 20, 4 << 20, 32 << 20]
13
14 class S3TestConnection:
15     def __init__(self):
16         self.conn = boto.connect_s3(is_secure=False,
17                                     calling_format=SubdomainCallingFormat())
18         self.bucket = self.conn.get_bucket(BUCKET_NAME)
19
20     def put_object(self, name, size):
21         buf = 'A' * size
22         k = Key(self.bucket, name)
23         start_time = time.time()
24         k.set_contents_from_string(buf)
25         print "%s: %f" % (name, time.time() - start_time)
26
27     def get_object(self, name):
28         k = Key(self.bucket, name)
29         start_time = time.time()
30         buf = k.get_contents_as_string()
31         print "%s: %f" % (name, time.time() - start_time)
32
33 class AzureTestConnection:
34     def __init__(self):
35         self.conn = azure.Connection()
36
37     def put_object(self, name, size):
38         buf = 'A' * size
39         start_time = time.time()
40         self.conn.make_request('/benchmark/' + name, 'PUT', buf,
41                                {'x-ms-blob-type': 'BlockBlob'})
42         print "%s: %f" % (name, time.time() - start_time)
43
44     def get_object(self, name):
45         start_time = time.time()
46         self.conn.make_request('/benchmark/' + name, 'GET')
47         print "%s: %f" % (name, time.time() - start_time)
48
49 def run_test():
50     print "==== S3 ===="
51     c = S3TestConnection()
52     for repeat in range(4):
53         for size in SIZES:
54             c.put_object('file-%d-%d' % (size, repeat), size)
55
56     c = S3TestConnection()
57     for repeat in range(4):
58         for size in SIZES:
59             c.get_object('file-%d-%d' % (size, repeat))
60
61     print "==== AZURE ===="
62     c = AzureTestConnection()
63     for repeat in range(4):
64         for size in SIZES:
65             c.put_object('file-%d-%d' % (size, repeat), size)
66
67     c = AzureTestConnection()
68     for repeat in range(4):
69         for size in SIZES:
70             c.get_object('file-%d-%d' % (size, repeat))
71
72 if __name__ == '__main__':
73     #run_test()
74     SIZES = [4096, 32 << 10, 256 << 10, 1 << 20, 4 << 20]
75     PRIME = (1 << 20) + (1 << 10)
76     c = AzureTestConnection()
77     for size in SIZES:
78         c.put_object('file-%d-%d' % (size, 0), size)
79     c.put_object('file-%d-%d' % (PRIME, 0), PRIME)
80
81     for size in SIZES:
82         for n in range(50):
83             c = AzureTestConnection()
84             c.get_object('file-%d-%d' % (PRIME, 0))
85             time.sleep(2.0)
86             c.get_object('file-%d-%d' % (size, 0))