public
Last active

  • Download Gist
arango_performance.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
import sys
import time
 
from datetime import datetime
 
from arango import create
from arango.document import Document
from arango.clients import Client
 
try:
import simplejson as json
except ImportError:
import json
 
 
class Timer(object):
def __init__(self):
self.start = datetime.now()
self.end = None
 
@property
def result(self):
self.end = datetime.now()
t2 = self.end - self.start
return t2.seconds * 1000 + t2.microseconds * 0.001
 
@classmethod
def measure(cls, func):
timer = cls()
func()
return timer.result
 
 
def cleanup(conn):
try:
db_coll = getattr(conn, collection_name)
except:
print "Not connected to a database."
raise
 
created = db_coll.create()
 
if created and created.is_error:
try:
db_coll.delete()
time.sleep(4)
print "Deleted collection '%s'." % collection_name
except:
print "Could not delete collection '%s'." % collection_name
 
db_coll.create(collection_name)
time.sleep(3)
 
print "Created collection '%s'." % collection_name
 
return getattr(conn, collection_name)
 
 
def get_connection():
 
db_host = '127.0.0.1'
db_port = 8529
db_conn = None
 
try:
db_conn = create(host=db_host, port=db_port)
except:
print "Not connected to a database."
raise
 
return db_conn
 
 
collection_name = 'testdocs_py'
collection_items = int(sys.argv[1])
 
conn = get_connection()
documents = cleanup(conn).documents
 
 
def pycurl_client():
"""
Simple example
"""
import pycurl
 
for i in range(collection_items):
body = {"value": "test_%d" % i}
documents.create(body)
 
 
def urllib2_client():
"""
Simple example with urllib2
"""
from arango.clients.urllib2client import Urllib2Client
default_client = documents.connection.client
documents.connection.client = Urllib2Client
documents.connection.client.config(timeout=3)
 
for i in range(collection_items):
body = {"value": "test_%d" % i}
try:
documents.create(body)
except Exception, exc:
print "Connection timeout on {} iteration".format(i)
 
documents.connection.client = default_client
 
 
def requests_client():
"""
Simple example
"""
 
from arango.clients.requestsclient import RequestsClient
default_client = documents.connection.client
documents.connection.client = RequestsClient
 
for i in range(collection_items):
body = {"value": "test_%d" % i}
documents.create(body)
 
documents.connection.client = default_client
 
 
def plain_request():
"""
Use only core functionality, no abstraction
"""
conn = documents.connection
 
for i in range(collection_items):
body = {"value": "test_%d" % i}
conn.post(
conn.qs(
Document.DOCUMENT_PATH,
createCollection=False),
data=body,
_expect_raw=True)
 
 
def plain_request_urllib2():
"""
Use only core functionality, no abstraction
"""
conn = documents.connection
 
from arango.clients.urllib2client import Urllib2Client
default_client = documents.connection.client
documents.connection.client = Urllib2Client
documents.connection.client.config(timeout=3)
 
for i in range(collection_items):
body = {"value": "test_%d" % i}
conn.post(
conn.qs(
Document.DOCUMENT_PATH,
createCollection=False),
data=body,
_expect_raw=True)
 
documents.connection.client = default_client
 
 
def plain_request_requests():
"""
Use only core functionality, no abstraction
"""
conn = documents.connection
 
from arango.clients.requestsclient import RequestsClient
default_client = documents.connection.client
documents.connection.client = RequestsClient
 
for i in range(collection_items):
body = {"value": "test_%d" % i}
conn.post(
conn.qs(
Document.DOCUMENT_PATH,
createCollection=False),
data=body,
_expect_raw=True)
 
documents.connection.client = default_client
 
 
def pycurl_client_raw():
"""
PyCURL
"""
import StringIO
import pycurl
 
url = "http://127.0.0.1:8529/_api/document?collection=testdocs_py"
 
for i in range(collection_items):
c = pycurl.Curl()
 
body = {"value": "test_%d" % i}
c.setopt(pycurl.URL, url)
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, json.dumps(body))
 
b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, b.write)
c.perform()
c.close()
 
b.getvalue()
 
 
try:
print "SIMPLE (PYCURL/Default): "\
"Inserted: %d items, insertion time: %.3fms." % (
collection_items, Timer.measure(pycurl_client))
except ImportError:
print "SIMPLE: Install PyCURL binding to measure this kind of client"
 
documents = cleanup(conn).documents
print "SIMPLE (URLLIB2): Inserted: %d items, insertion time: %.3fms." % (
collection_items, Timer.measure(urllib2_client))
 
documents = cleanup(conn).documents
print "SIMPLE (REQUESTS): Inserted: %d items, insertion time: %.3fms." % (
collection_items, Timer.measure(requests_client))
 
documents = cleanup(conn).documents
print "REST API: Inserted: %d items, insertion time: %.3fms." % (
collection_items, Timer.measure(plain_request))
 
documents = cleanup(conn).documents
print "REST API (requests): Inserted: %d items, insertion time: %.3fms." % (
collection_items, Timer.measure(plain_request_requests))
 
documents = cleanup(conn).documents
print "REST API (urllib2): Inserted: %d items, insertion time: %.3fms." % (
collection_items, Timer.measure(plain_request_urllib2))
 
 
documents = cleanup(conn).documents
try:
print "PYCURL + REST API: Inserted: %d items, insertion time: %.3fms." % (
collection_items, Timer.measure(pycurl_client_raw))
except ImportError:
print "PyCURL NOT INSTALLED, IGNORE"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.