Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 141 lines (118 sloc) 5.057 kB
fff4806 @ovidiucp Initial commit.
authored
1 # Author: Ovidiu Predescu
2 # Date: July 2011
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 """
16 A simple benchmark for various asynchronous Python MySQL client libraries.
17 """
18 import time
19
20 import tornado.options
21 from tornado.options import define, options
22
23 define("mysql_host", default="127.0.0.1", help="Database host")
24 define("mysql_user", default="", help="MySQL user to use")
25 define("mysql_passwd", default="", help="MySQL password")
26 define("mysql_db", default="test", help="MySQL database to use")
27
28 define("use_tornado", default=False,
29 help="Use tornado twisted reactor instead of twisted's reactor")
30
31 define("use_txmysql", default=False, help="Use txMySQL database module")
32 define("use_adbapi", default=False, help="Use twisted's adbapi module")
33
34 define("pool_size", default=10, help="Database connection pool size")
35
36 class DbBenchmark:
37 def __init__(self):
38 self._pool = None
39
40 def run(self):
41 d = self._createTable()
42 d.addCallback(self._startBenchmark)
43 d.addErrback(self._dbError)
44
45 def _dbError(self, error):
46 print "Error accessing the database: %s" % error
47
48 def _createTable(self):
49 print 'creating benchmark table'
50 return self._pool.runOperation(
51 """drop table if exists benchmark;
52 create table benchmark(
53 id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
54 data VARCHAR(100)
55 )
56 """)
57
58 def _startBenchmark(self, ignored):
59 print "Starting benchmark %s" % self.__class__
60 self._start_time = time.time()
61 self._totalNumInserts = 100000
62 self._numDone = 0
63 for i in xrange(self._totalNumInserts):
64 d = self._doInsert(i)
65 d.addCallback(self._insertDone)
66 d.addErrback(self._dbError)
67
68 def _doInsert(self, i):
69 raise NotImplementedError
70
71 def _insertDone(self, ignored):
72 self._numDone += 1
73 if self._numDone == self._totalNumInserts:
74 d = self._pool.runQuery("select count(*) from benchmark")
75 d.addCallback(self._allInsertsDone)
76 d.addErrback(self._dbError)
77
78 def _allInsertsDone(self, results):
79 self._end_time = time.time()
80 row = results[0]
81 num = row[0]
82 print "inserted %d records, time taken = %f seconds" %\
83 (num, (self._end_time - self._start_time))
84 reactor.stop()
85
86 class TwistedDbAPI(DbBenchmark):
87 def __init__(self):
88 from twisted.enterprise import adbapi
89 print "Creating twisted adbapi ConnectionPool"
90 self._pool = adbapi.ConnectionPool(dbapiName="MySQLdb",
91 host=options.mysql_host,
92 port=3306,
93 unix_socket='',
94 user=options.mysql_user,
95 passwd=options.mysql_passwd,
96 db=options.mysql_db,
97 cp_min=options.pool_size,
98 cp_max=options.pool_size)
99 self._numRuns = 0
100
101 def _doInsert(self, i):
102 return self._pool.runOperation(
103 "insert benchmark(data) values (%d)" % i)
104
105
106 class TxMySQL(DbBenchmark):
107 def __init__(self):
108 from txmysql import client
109 print "Creating txMySQL ConnectionPool"
110 self._pool = client.ConnectionPool(hostname=options.mysql_host,
111 username=options.mysql_user,
112 password=options.mysql_passwd,
113 database=options.mysql_db,
114 num_connections=options.pool_size)
115
116 def _doInsert(self, i):
117 return self._pool.runOperation(
118 "insert benchmark(data) values (%d)" % i)
119
120
121 if __name__ == "__main__":
122 tornado.options.parse_command_line()
123 if options.use_tornado:
124 import tornado.platform.twistedreactor
125 tornado.platform.twistedreactor.install()
126 from twisted.internet import reactor
127 print "Using reactor %s" % reactor
128
129 reactor.suggestThreadPoolSize(options.pool_size)
130
131 if options.use_adbapi:
132 benchmark = TwistedDbAPI()
133 benchmark.run()
134 elif options.use_txmysql:
135 benchmark = TxMySQL()
136 benchmark.run()
137 else:
138 print 'At least one of --use_adbapi or --use_txmysql should be '\
139 'specified'
140 reactor.run()
Something went wrong with that request. Please try again.