summaryrefslogtreecommitdiffstats
path: root/tests/scripts/bench-411.py
blob: 82ea451d8f86e09d3317558e01f5f5c746bdb07a (plain)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import os
import sys
import time
import random
import asyncio
import logging
from enum import Enum
from typing import Any, Dict, List, Generator
from argparse import ArgumentParser, Namespace
from contextlib import contextmanager

logger = logging.getLogger()
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(message)s",
)


class Driver(str, Enum):
    psycopg2 = "psycopg2"
    psycopg = "psycopg"
    psycopg_async = "psycopg_async"
    asyncpg = "asyncpg"


ids: List[int] = []
data: List[Dict[str, Any]] = []


def main() -> None:

    args = parse_cmdline()

    ids[:] = range(args.ntests)
    data[:] = [
        dict(
            id=i,
            name="c%d" % i,
            description="c%d" % i,
            q=i * 10,
            p=i * 20,
            x=i * 30,
            y=i * 40,
        )
        for i in ids
    ]

    # Must be done just on end
    drop_at_the_end = args.drop
    args.drop = False

    for i, name in enumerate(args.drivers):
        if i == len(args.drivers) - 1:
            args.drop = drop_at_the_end

        if name == Driver.psycopg2:
            import psycopg2  # type: ignore

            run_psycopg2(psycopg2, args)

        elif name == Driver.psycopg:
            import psycopg

            run_psycopg(psycopg, args)

        elif name == Driver.psycopg_async:
            import psycopg

            if sys.platform == "win32":
                if hasattr(asyncio, "WindowsSelectorEventLoopPolicy"):
                    asyncio.set_event_loop_policy(
                        asyncio.WindowsSelectorEventLoopPolicy()
                    )

            asyncio.run(run_psycopg_async(psycopg, args))

        elif name == Driver.asyncpg:
            import asyncpg  # type: ignore

            asyncio.run(run_asyncpg(asyncpg, args))

        else:
            raise AssertionError(f"unknown driver: {name!r}")

        # Must be done just on start
        args.create = False


table = """
CREATE TABLE customer (
        id SERIAL NOT NULL,
        name VARCHAR(255),
        description VARCHAR(255),
        q INTEGER,
        p INTEGER,
        x INTEGER,
        y INTEGER,
        z INTEGER,
        PRIMARY KEY (id)
)
"""
drop = "DROP TABLE IF EXISTS customer"

insert = """
INSERT INTO customer (id, name, description, q, p, x, y) VALUES
(%(id)s, %(name)s, %(description)s, %(q)s, %(p)s, %(x)s, %(y)s)
"""

select = """
SELECT customer.id, customer.name, customer.description, customer.q,
    customer.p, customer.x, customer.y, customer.z
FROM customer
WHERE customer.id = %(id)s
"""


@contextmanager
def time_log(message: str) -> Generator[None, None, None]:
    start = time.monotonic()
    yield
    end = time.monotonic()
    logger.info(f"Run {message} in {end-start} s")


def run_psycopg2(psycopg2: Any, args: Namespace) -> None:
    logger.info("Running psycopg2")

    if args.create:
        logger.info(f"inserting {args.ntests} test records")
        with psycopg2.connect(args.dsn) as conn:
            with conn.cursor() as cursor:
                cursor.execute(drop)
                cursor.execute(table)
                cursor.executemany(insert, data)
            conn.commit()

    logger.info(f"running {args.ntests} queries")
    to_query = random.choices(ids, k=args.ntests)
    with psycopg2.connect(args.dsn) as conn:
        with time_log("psycopg2"):
            for id_ in to_query:
                with conn.cursor() as cursor:
                    cursor.execute(select, {"id": id_})
                    cursor.fetchall()
                # conn.rollback()

    if args.drop:
        logger.info("dropping test records")
        with psycopg2.connect(args.dsn) as conn:
            with conn.cursor() as cursor:
                cursor.execute(drop)
            conn.commit()


def run_psycopg(psycopg: Any, args: Namespace) -> None:
    logger.info("Running psycopg sync")

    if args.create:
        logger.info(f"inserting {args.ntests} test records")
        with psycopg.connect(args.dsn) as conn:
            with conn.cursor() as cursor:
                cursor.execute(drop)
                cursor.execute(table)
                cursor.executemany(insert, data)
            conn.commit()

    logger.info(f"running {args.ntests} queries")
    to_query = random.choices(ids, k=args.ntests)
    with psycopg.connect(args.dsn) as conn:
        with time_log("psycopg"):
            for id_ in to_query:
                with conn.cursor() as cursor:
                    cursor.execute(select, {"id": id_})
                    cursor.fetchall()
                # conn.rollback()

    if args.drop:
        logger.info("dropping test records")
        with psycopg.connect(args.dsn) as conn:
            with conn.cursor() as cursor:
                cursor.execute(drop)
            conn.commit()


async def run_psycopg_async(psycopg: Any, args: Namespace) -> None:
    logger.info("Running psycopg async")

    conn: Any

    if args.create:
        logger.info(f"inserting {args.ntests} test records")
        async with await psycopg.AsyncConnection.connect(args.dsn) as conn:
            async with conn.cursor() as cursor:
                await cursor.execute(drop)
                await cursor.execute(table)
                await cursor.executemany(insert, data)
            await conn.commit()

    logger.info(f"running {args.ntests} queries")
    to_query = random.choices(ids, k=args.ntests)
    async with await psycopg.AsyncConnection.connect(args.dsn) as conn:
        with time_log("psycopg_async"):
            for id_ in to_query:
                cursor = await conn.execute(select, {"id": id_})
                await cursor.fetchall()
                await cursor.close()
                # await conn.rollback()

    if args.drop:
        logger.info("dropping test records")
        async with await psycopg.AsyncConnection.connect(args.dsn) as conn:
            async with conn.cursor() as cursor:
                await cursor.execute(drop)
            await conn.commit()


async def run_asyncpg(asyncpg: Any, args: Namespace) -> None:
    logger.info("Running asyncpg")

    places = dict(id="$1", name="$2", description="$3", q="$4", p="$5", x="$6", y="$7")
    a_insert = insert % places
    a_select = select % {"id": "$1"}

    conn: Any

    if args.create:
        logger.info(f"inserting {args.ntests} test records")
        conn = await asyncpg.connect(args.dsn)
        async with conn.transaction():
            await conn.execute(drop)
            await conn.execute(table)
            await conn.executemany(a_insert, [tuple(d.values()) for d in data])
        await conn.close()

    logger.info(f"running {args.ntests} queries")
    to_query = random.choices(ids, k=args.ntests)
    conn = await asyncpg.connect(args.dsn)
    with time_log("asyncpg"):
        for id_ in to_query:
            tr = conn.transaction()
            await tr.start()
            await conn.fetch(a_select, id_)
            # await tr.rollback()
    await conn.close()

    if args.drop:
        logger.info("dropping test records")
        conn = await asyncpg.connect(args.dsn)
        async with conn.transaction():
            await conn.execute(drop)
        await conn.close()


def parse_cmdline() -> Namespace:
    parser = ArgumentParser(description=__doc__)
    parser.add_argument(
        "drivers",
        nargs="+",
        metavar="DRIVER",
        type=Driver,
        help=f"the drivers to test [choices: {', '.join(d.value for d in Driver)}]",
    )

    parser.add_argument(
        "--ntests",
        type=int,
        default=10_000,
        help="number of tests to perform [default: %(default)s]",
    )

    parser.add_argument(
        "--dsn",
        default=os.environ.get("PSYCOPG_TEST_DSN", ""),
        help="database connection string"
        " [default: %(default)r (from PSYCOPG_TEST_DSN env var)]",
    )

    parser.add_argument(
        "--no-create",
        dest="create",
        action="store_false",
        default="True",
        help="skip data creation before tests (it must exist already)",
    )

    parser.add_argument(
        "--no-drop",
        dest="drop",
        action="store_false",
        default="True",
        help="skip data drop after tests",
    )

    opt = parser.parse_args()

    return opt


if __name__ == "__main__":
    main()