|
| 1 | +import uuid |
| 2 | + |
| 3 | +from aiohttp import web |
| 4 | +from piccolo.engine import engine_finder |
| 5 | +from piccolo.query import OrderByRaw |
| 6 | + |
| 7 | +from psqlpy_stress.influx_db_helpers import write_timings_to_influx |
| 8 | +from psqlpy_stress.models.piccolo import SomeBigTable, User |
| 9 | +from psqlpy_stress.piccolo_conf import ( |
| 10 | + ASYNCPG_PICCOLO_ENGINE, |
| 11 | + PSQLPY_PICCOLO_ENGINE, |
| 12 | +) |
| 13 | +from psqlpy_stress.settings import DriversEnum |
| 14 | + |
| 15 | + |
| 16 | +@write_timings_to_influx(DriversEnum.PSQLPY) |
| 17 | +async def psqlpy_simple_transaction_select_piccolo( |
| 18 | + _request: web.Request, |
| 19 | +) -> web.Response: |
| 20 | + async with PSQLPY_PICCOLO_ENGINE.transaction(): |
| 21 | + User._meta.db = PSQLPY_PICCOLO_ENGINE |
| 22 | + await User.select().order_by(OrderByRaw("random()")).run() |
| 23 | + return web.Response(status=200, text="Ok") |
| 24 | + |
| 25 | + |
| 26 | +@write_timings_to_influx(DriversEnum.ASYNCPG) |
| 27 | +async def asyncpg_simple_transaction_select_piccolo( |
| 28 | + _request: web.Request, |
| 29 | +) -> web.Response: |
| 30 | + async with ASYNCPG_PICCOLO_ENGINE.transaction(): |
| 31 | + User._meta.db = ASYNCPG_PICCOLO_ENGINE |
| 32 | + await User.select().order_by(OrderByRaw("random()")).run() |
| 33 | + return web.Response(status=200, text="Ok") |
| 34 | + |
| 35 | + |
| 36 | +@write_timings_to_influx(DriversEnum.PSQLPY) |
| 37 | +async def psqlpy_simple_connection_select_piccolo(_request: web.Request) -> web.Response: |
| 38 | + User._meta.db = PSQLPY_PICCOLO_ENGINE |
| 39 | + await User.select().order_by(OrderByRaw("random()")).run() |
| 40 | + return web.Response(status=200, text="Ok") |
| 41 | + |
| 42 | + |
| 43 | +@write_timings_to_influx(DriversEnum.ASYNCPG) |
| 44 | +async def asyncpg_simple_connection_select_piccolo( |
| 45 | + _request: web.Request, |
| 46 | +) -> web.Response: |
| 47 | + User._meta.db = ASYNCPG_PICCOLO_ENGINE |
| 48 | + await User.select().order_by(OrderByRaw("random()")).run() |
| 49 | + return web.Response(status=200, text="Ok") |
| 50 | + |
| 51 | + |
| 52 | +# --------------------------------------------- Hard queries handlers starting here --------------------------------------------- |
| 53 | + |
| 54 | + |
| 55 | +@write_timings_to_influx(DriversEnum.PSQLPY) |
| 56 | +async def psqlpy_hard_transaction_select_piccolo( |
| 57 | + _request: web.Request, |
| 58 | +) -> web.Response: |
| 59 | + async with PSQLPY_PICCOLO_ENGINE.transaction(): |
| 60 | + SomeBigTable._meta.db = PSQLPY_PICCOLO_ENGINE |
| 61 | + await SomeBigTable.select().order_by(OrderByRaw("random()")).run() |
| 62 | + return web.Response(status=200, text="Ok") |
| 63 | + |
| 64 | + |
| 65 | +@write_timings_to_influx(DriversEnum.ASYNCPG) |
| 66 | +async def asyncpg_hard_transaction_select_piccolo( |
| 67 | + _request: web.Request, |
| 68 | +) -> web.Response: |
| 69 | + engine_finder().set_engine(ASYNCPG_PICCOLO_ENGINE) |
| 70 | + async with ASYNCPG_PICCOLO_ENGINE.transaction(): |
| 71 | + SomeBigTable._meta.db = ASYNCPG_PICCOLO_ENGINE |
| 72 | + await SomeBigTable.select().order_by(OrderByRaw("random()")).run() |
| 73 | + return web.Response(status=200, text="Ok") |
| 74 | + |
| 75 | + |
| 76 | +@write_timings_to_influx(DriversEnum.PSQLPY) |
| 77 | +async def psqlpy_hard_connection_select_piccolo(_request: web.Request) -> web.Response: |
| 78 | + SomeBigTable._meta.db = PSQLPY_PICCOLO_ENGINE |
| 79 | + await SomeBigTable.select().order_by(OrderByRaw("random()")).run() |
| 80 | + return web.Response(status=200, text="Ok") |
| 81 | + |
| 82 | + |
| 83 | +@write_timings_to_influx(DriversEnum.ASYNCPG) |
| 84 | +async def asyncpg_hard_connection_select_piccolo( |
| 85 | + _request: web.Request, |
| 86 | +) -> web.Response: |
| 87 | + SomeBigTable._meta.db = ASYNCPG_PICCOLO_ENGINE |
| 88 | + await SomeBigTable.select().order_by(OrderByRaw("random()")).run() |
| 89 | + return web.Response(status=200, text="Ok") |
| 90 | + |
| 91 | + |
| 92 | +# --------------------------------------------- Combined queries (select + insert) handlers starting here --------------------------------------------- |
| 93 | + |
| 94 | + |
| 95 | +@write_timings_to_influx(DriversEnum.PSQLPY) |
| 96 | +async def psqlpy_combined_transaction_query_piccolo( |
| 97 | + _request: web.Request, |
| 98 | +) -> web.Response: |
| 99 | + async with PSQLPY_PICCOLO_ENGINE.transaction(): |
| 100 | + User._meta.db = PSQLPY_PICCOLO_ENGINE |
| 101 | + await User.insert(User(username=str(uuid.uuid4()))).run() |
| 102 | + await User.select().order_by(OrderByRaw("random()")).run() |
| 103 | + return web.Response(status=200, text="Ok") |
| 104 | + |
| 105 | + |
| 106 | +@write_timings_to_influx(DriversEnum.ASYNCPG) |
| 107 | +async def asyncpg_combined_transaction_query_piccolo( |
| 108 | + _request: web.Request, |
| 109 | +) -> web.Response: |
| 110 | + engine_finder().set_engine(ASYNCPG_PICCOLO_ENGINE) |
| 111 | + async with ASYNCPG_PICCOLO_ENGINE.transaction(): |
| 112 | + User._meta.db = ASYNCPG_PICCOLO_ENGINE |
| 113 | + await User.insert(User(username=str(uuid.uuid4()))).run() |
| 114 | + await User.select().order_by(OrderByRaw("random()")).run() |
| 115 | + return web.Response(status=200, text="Ok") |
| 116 | + |
| 117 | + |
| 118 | +@write_timings_to_influx(DriversEnum.PSQLPY) |
| 119 | +async def psqlpy_combined_connection_query_piccolo( |
| 120 | + _request: web.Request, |
| 121 | +) -> web.Response: |
| 122 | + User._meta.db = PSQLPY_PICCOLO_ENGINE |
| 123 | + await User.insert(User(username=str(uuid.uuid4()))).run() |
| 124 | + await User.select().order_by(OrderByRaw("random()")).run() |
| 125 | + return web.Response(status=200, text="Ok") |
| 126 | + |
| 127 | + |
| 128 | +@write_timings_to_influx(DriversEnum.ASYNCPG) |
| 129 | +async def asyncpg_combined_connection_query_piccolo( |
| 130 | + _request: web.Request, |
| 131 | +) -> web.Response: |
| 132 | + User._meta.db = ASYNCPG_PICCOLO_ENGINE |
| 133 | + await User.insert(User(username=str(uuid.uuid4()))).run() |
| 134 | + await User.select().order_by(OrderByRaw("random()")).run() |
| 135 | + return web.Response(status=200, text="Ok") |
| 136 | + |
| 137 | + |
| 138 | +PICCOLO_QUERY_ROUTES = [ |
| 139 | + web.get( |
| 140 | + "/asyncpg-simple-transaction-select-piccolo", |
| 141 | + asyncpg_simple_transaction_select_piccolo, |
| 142 | + ), |
| 143 | + web.get( |
| 144 | + "/psqlpy-simple-transaction-select-piccolo", |
| 145 | + psqlpy_simple_transaction_select_piccolo, |
| 146 | + ), |
| 147 | + web.get( |
| 148 | + "/asyncpg-simple-connection-select-piccolo", |
| 149 | + asyncpg_simple_connection_select_piccolo, |
| 150 | + ), |
| 151 | + web.get( |
| 152 | + "/psqlpy-simple-connection-select-piccolo", |
| 153 | + psqlpy_simple_connection_select_piccolo, |
| 154 | + ), |
| 155 | + web.get( |
| 156 | + "/asyncpg-hard-transaction-select-piccolo", |
| 157 | + asyncpg_hard_transaction_select_piccolo, |
| 158 | + ), |
| 159 | + web.get( |
| 160 | + "/psqlpy-hard-transaction-select-piccolo", |
| 161 | + psqlpy_hard_transaction_select_piccolo, |
| 162 | + ), |
| 163 | + web.get( |
| 164 | + "/asyncpg-hard-connection-select-piccolo", |
| 165 | + asyncpg_hard_connection_select_piccolo, |
| 166 | + ), |
| 167 | + web.get( |
| 168 | + "/psqlpy-hard-connection-select-piccolo", |
| 169 | + psqlpy_hard_connection_select_piccolo, |
| 170 | + ), |
| 171 | + web.get( |
| 172 | + "/asyncpg-combined-transaction-query-piccolo", |
| 173 | + asyncpg_combined_transaction_query_piccolo, |
| 174 | + ), |
| 175 | + web.get( |
| 176 | + "/psqlpy-combined-transaction-query-piccolo", |
| 177 | + psqlpy_combined_transaction_query_piccolo, |
| 178 | + ), |
| 179 | + web.get( |
| 180 | + "/asyncpg-combined-connection-query-piccolo", |
| 181 | + asyncpg_combined_connection_query_piccolo, |
| 182 | + ), |
| 183 | + web.get( |
| 184 | + "/psqlpy-combined-connection-query-piccolo", |
| 185 | + psqlpy_combined_connection_query_piccolo, |
| 186 | + ), |
| 187 | +] |
0 commit comments