Another update to the Azure test code.
authorMichael Vrable <mvrable@cs.ucsd.edu>
Mon, 3 May 2010 04:51:22 +0000 (21:51 -0700)
committerMichael Vrable <mvrable@cs.ucsd.edu>
Mon, 3 May 2010 04:51:22 +0000 (21:51 -0700)
azure/azure.py

index dd0173e..584b22b 100755 (executable)
@@ -15,7 +15,7 @@ def uri_decode(s):
     # TODO
     return s
 
-def add_auth_header(headers, method, path):
+def add_auth_headers(headers, method, path):
     header_order = ['Content-Encoding', 'Content-Language', 'Content-Length',
                     'Content-MD5', 'Content-Type', 'Date', 'If-Modified-Since',
                     'If-Match', 'If-None-Match', 'If-Unmodified-Since',
@@ -69,26 +69,36 @@ def add_auth_header(headers, method, path):
     signature = base64.b64encode(h.digest())
     headers['Authorization'] = "SharedKey %s:%s" % (account_name, signature)
 
-def generate_request(path, method='GET', body="", headers={}):
-    host = os.environ['AZURE_ACCOUNT_NAME'] + ".blob.core.windows.net"
-    #auth_path = conn.calling_format.build_auth_path(self.bucket, key)
-
-    headers = headers.copy()
-    headers['Content-Length'] = str(len(body))
-    if len(body) > 0:
-        headers['Content-MD5'] = base64.b64encode(hashlib.md5(body).digest())
-    add_auth_header(headers, method, path)
-
-    req = "%s %s HTTP/1.1\r\nHost: %s\r\n" % (method, path, host)
-    req = req + ''.join("%s: %s\r\n" % h for h in headers.items()) + "\r\n"
-    print req
-
-    conn = httplib.HTTPConnection(host)
-    conn.request(method, path, body, headers)
-    response = conn.getresponse()
-    print "Response:", response.status
-    print "Headers:", response.getheaders()
-    print "Body:", response.read()
-
-if False:
-    generate_request("/?comp=list")
+class Connection:
+    def __init__(self):
+        self.host = os.environ['AZURE_ACCOUNT_NAME'] + ".blob.core.windows.net"
+        self.conn = httplib.HTTPConnection(self.host)
+
+    def make_request(self, path, method='GET', body="", headers={}):
+        headers = headers.copy()
+        headers['Content-Length'] = str(len(body))
+        if len(body) > 0:
+            headers['Content-MD5'] \
+                = base64.b64encode(hashlib.md5(body).digest())
+        add_auth_headers(headers, method, path)
+
+        # req = "%s %s HTTP/1.1\r\nHost: %s\r\n" % (method, path, host)
+        # req = req + ''.join("%s: %s\r\n" % h for h in headers.items()) + "\r\n"
+        # print req
+
+        self.conn.request(method, path, body, headers)
+        response = self.conn.getresponse()
+        print "Response:", response.status
+        print "Headers:", response.getheaders()
+        print "Body:", response.read()
+
+if __name__ == '__main__':
+    # generate_request("/?comp=list")
+    buf = 'A' * 8192
+    conn = Connection()
+    for i in range(16):
+        conn.make_request('/benchmark/file-' + str(i), 'PUT', buf,
+                          {'x-ms-blob-type': 'BlockBlob'})
+
+    for i in range(16):
+        conn.make_request('/benchmark/file-' + str(i), 'GET')