forked from julien-duponchelle/python-mysql-replication
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark.py
66 lines (54 loc) · 1.56 KB
/
benchmark.py
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
# This is a sample script in order to make benchmark
# on library speed.
#
#
import pymysql
import time
import os
from pymysqlreplication import BinLogStreamReader
from pymysqlreplication.row_event import *
def execute(con, query):
c = con.cursor()
c.execute(query)
return c
def consume_events():
stream = BinLogStreamReader(
connection_settings=database,
server_id=3,
resume_stream=False,
blocking=True,
only_events=[UpdateRowsEvent],
only_tables=["test"],
)
start = time.perf_counter()
i = 0.0
for binlogevent in stream:
i += 1.0
if i % 1000 == 0:
print(f"{i / (time.perf_counter()- start)} event by seconds ({i} total)")
stream.close()
database = {
"host": "localhost",
"user": "root",
"passwd": "",
"use_unicode": True,
"charset": "utf8",
"db": "pymysqlreplication_test",
}
conn = pymysql.connect(**database)
execute(conn, "DROP DATABASE IF EXISTS pymysqlreplication_test")
execute(conn, "CREATE DATABASE pymysqlreplication_test")
conn = pymysql.connect(**database)
execute(conn, "CREATE TABLE test (i INT) ENGINE = MEMORY")
execute(conn, "INSERT INTO test VALUES(1)")
execute(conn, "CREATE TABLE test2 (i INT) ENGINE = MEMORY")
execute(conn, "INSERT INTO test2 VALUES(1)")
execute(conn, "RESET MASTER")
if os.fork() != 0:
print("Start insert data")
while True:
execute(conn, "UPDATE test SET i = i + 1;")
execute(conn, "UPDATE test2 SET i = i + 1;")
else:
consume_events()
# cProfile.run('consume_events()')