Skip to content

Commit 916de59

Browse files
Small optimization
1 parent 151f9b6 commit 916de59

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

pymysqlreplication/column.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def serializable_data(self):
9292
return self.data
9393

9494
def __getattr__(self, item):
95-
if item in self.data:
95+
try:
9696
return self.data[item]
97-
else:
97+
except KeyError:
9898
raise AttributeError("{0} not found".format(item))

pymysqlreplication/table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ def __init__(self, column_schemas, table_id, schema, table, columns, primary_key
2222
}
2323

2424
def __getattr__(self, item):
25-
if item in self.data:
25+
try:
2626
return self.data[item]
27-
else:
27+
except KeyError:
2828
raise AttributeError
2929

3030
def __eq__(self, other):

pymysqlreplication/tests/benchmark.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,29 @@
1111
import os
1212
from pymysqlreplication import BinLogStreamReader
1313
from pymysqlreplication.row_event import *
14+
import cProfile
1415

1516

1617
def execute(con, query):
1718
c = con.cursor()
1819
c.execute(query)
1920
return c
2021

22+
def consume_events():
23+
stream = BinLogStreamReader(connection_settings=database,
24+
server_id=3,
25+
resume_stream=False,
26+
blocking=True,
27+
only_events = [UpdateRowsEvent])
28+
start = time.clock()
29+
i = 0.0
30+
for binlogevent in stream:
31+
i += 1.0
32+
if i % 1000 == 0:
33+
print("%d event by seconds (%d total)" % (i / (time.clock() - start), i))
34+
stream.close()
35+
36+
2137
database = {
2238
"host": "localhost",
2339
"user": "root",
@@ -36,22 +52,11 @@ def execute(con, query):
3652
execute(conn, "INSERT INTO test VALUES(1)")
3753
execute(conn, "RESET MASTER")
3854

55+
3956
if os.fork() != 0:
4057
print("Start insert data")
4158
while True:
4259
execute(conn, "UPDATE test SET i = i + 1;")
4360
else:
44-
stream = BinLogStreamReader(connection_settings=database,
45-
server_id=3,
46-
resume_stream=False,
47-
blocking=True,
48-
only_events = [UpdateRowsEvent])
49-
start = time.clock()
50-
i = 0.0
51-
for binlogevent in stream:
52-
i += 1.0
53-
if i % 1000 == 0:
54-
print("%d event by seconds (%d total)" % (i / (time.clock() - start), i))
55-
stream.close()
56-
61+
cProfile.run('consume_events()')
5762

0 commit comments

Comments
 (0)