File: benchmark_update.py

package info (click to toggle)
python-asyncmy 0.2.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 676 kB
  • sloc: python: 3,528; makefile: 40
file content (102 lines) | stat: -rw-r--r-- 2,470 bytes parent folder | download
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
import asyncio
import time
from rich.pretty import pprint

import aiomysql
import asyncmy
import MySQLdb
import pymysql

from benchmark import COUNT, connection_kwargs
from benchmark.decorators import cleanup, fill_data

count = int(COUNT / 5)


@cleanup
@fill_data
async def update_asyncmy():
    conn = await asyncmy.connect(**connection_kwargs)
    async with conn.cursor() as cur:
        t = time.time()
        for i in range(count):
            await cur.execute(
                "update test.asyncmy set `string`=%s where `id` = %s",
                (
                    "update",
                    i + 1,
                ),
            )
        return time.time() - t


@cleanup
@fill_data
async def update_aiomysql():
    conn = await aiomysql.connect(**connection_kwargs)
    async with conn.cursor() as cur:
        t = time.time()
        for i in range(count):
            await cur.execute(
                "update test.asyncmy set `string`=%s where `id` = %s",
                (
                    "update",
                    i + 1,
                ),
            )
        return time.time() - t


@cleanup
@fill_data
def update_mysqlclient():
    conn = MySQLdb.connect(**connection_kwargs)
    cur = conn.cursor()
    t = time.time()
    for i in range(count):
        cur.execute(
            "update test.asyncmy set `string`=%s where `id` = %s",
            (
                "update",
                i + 1,
            ),
        )
    return time.time() - t


@cleanup
@fill_data
def update_pymysql():
    conn = pymysql.connect(**connection_kwargs)
    cur = conn.cursor()
    t = time.time()
    for i in range(count):
        cur.execute(
            "update test.asyncmy set `string`=%s where `id` = %s",
            (
                "update",
                i + 1,
            ),
        )
    return time.time() - t


def benchmark_update():
    loop = asyncio.get_event_loop()
    update_mysqlclient_ret = update_mysqlclient()
    update_asyncmy_ret = loop.run_until_complete(update_asyncmy())
    update_pymysql_ret = update_pymysql()
    update_aiomysql_ret = loop.run_until_complete(update_aiomysql())
    return sorted(
        {
            "mysqlclient": update_mysqlclient_ret,
            "asyncmy": update_asyncmy_ret,
            "pymysql": update_pymysql_ret,
            "aiomysql": update_aiomysql_ret,
        }.items(),
        key=lambda x: x[1],
    )


if __name__ == "__main__":
    pprint(benchmark_update())