|
@@ -1,24 +1,21 @@
|
|
|
import asyncio
|
|
|
-from typing import Optional, Any
|
|
|
-
|
|
|
-import asyncpg
|
|
|
import os
|
|
|
-import jinja2
|
|
|
-from asyncpg import Pool
|
|
|
-from starlite import Starlite, get, MediaType
|
|
|
-from random import randint
|
|
|
from operator import itemgetter
|
|
|
+from random import randint, sample
|
|
|
+from typing import Any
|
|
|
|
|
|
-READ_ROW_SQL = 'SELECT "randomnumber", "id" FROM "world" WHERE id = $1'
|
|
|
-WRITE_ROW_SQL = 'UPDATE "world" SET "randomnumber"=$1 WHERE id=$2'
|
|
|
-ADDITIONAL_ROW = [0, 'Additional fortune added at request time.']
|
|
|
+import uvloop
|
|
|
+from asyncpg import create_pool
|
|
|
+from jinja2 import Template
|
|
|
+from starlite import MediaType, Starlite, get
|
|
|
|
|
|
-connection_pool: Pool
|
|
|
+asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
|
|
|
+connection_pool: Any = None
|
|
|
|
|
|
|
|
|
-async def setup_database():
|
|
|
+async def init_connection_pool() -> None:
|
|
|
global connection_pool
|
|
|
- connection_pool = await asyncpg.create_pool(
|
|
|
+ connection_pool = await create_pool(
|
|
|
user=os.getenv('PGUSER', 'benchmarkdbuser'),
|
|
|
password=os.getenv('PGPASS', 'benchmarkdbpass'),
|
|
|
database='hello_world',
|
|
@@ -27,54 +24,49 @@ async def setup_database():
|
|
|
)
|
|
|
|
|
|
|
|
|
-def load_fortunes_template():
|
|
|
+def normalize_queries(value: str | None) -> int:
|
|
|
+ queries = int(value) if value and value.isnumeric() else 1
|
|
|
+ if queries > 500:
|
|
|
+ return 500
|
|
|
+ if queries < 1:
|
|
|
+ return 1
|
|
|
+ return queries
|
|
|
+
|
|
|
+
|
|
|
+def load_fortunes_template() -> "Template":
|
|
|
path = os.path.join('templates', 'fortune.html')
|
|
|
with open(path, 'r') as template_file:
|
|
|
template_text = template_file.read()
|
|
|
- return jinja2.Template(template_text)
|
|
|
+ return Template(template_text)
|
|
|
|
|
|
|
|
|
-def get_num_queries(queries: Any):
|
|
|
- if queries:
|
|
|
- try:
|
|
|
- query_count = int(queries)
|
|
|
- except (ValueError, TypeError):
|
|
|
- return 1
|
|
|
- if query_count < 1:
|
|
|
- return 1
|
|
|
- if query_count > 500:
|
|
|
- return 500
|
|
|
- return query_count
|
|
|
- return 1
|
|
|
-
|
|
|
-
|
|
|
-sort_fortunes_key = itemgetter(1)
|
|
|
-template = load_fortunes_template()
|
|
|
+fortune_template = load_fortunes_template()
|
|
|
|
|
|
|
|
|
@get(path='/json')
|
|
|
-async def json_serialization() -> dict[str, str]:
|
|
|
+def json_serialization() -> dict[str, str]:
|
|
|
return {'message': 'Hello, world!'}
|
|
|
|
|
|
|
|
|
@get(path='/db')
|
|
|
async def single_database_query() -> dict[str, int]:
|
|
|
row_id = randint(1, 10000)
|
|
|
-
|
|
|
async with connection_pool.acquire() as connection:
|
|
|
- number = await connection.fetchval(READ_ROW_SQL, row_id)
|
|
|
+ number = await connection.fetchval(
|
|
|
+ 'SELECT "randomnumber", "id" FROM "world" WHERE id = $1',
|
|
|
+ row_id
|
|
|
+ )
|
|
|
|
|
|
return {'id': row_id, 'randomNumber': number}
|
|
|
|
|
|
|
|
|
@get(path='/queries')
|
|
|
-async def multiple_database_queries(queries: Any = None) -> list[dict[str, int]]:
|
|
|
- num_queries = get_num_queries(queries)
|
|
|
- row_ids = [randint(1, 10000) for _ in range(num_queries)]
|
|
|
+async def multiple_database_queries(queries: None | str = None) -> list[dict[str, int]]:
|
|
|
+ row_ids = sample(range(1, 10000), normalize_queries(queries))
|
|
|
worlds = []
|
|
|
|
|
|
async with connection_pool.acquire() as connection:
|
|
|
- statement = await connection.prepare(READ_ROW_SQL)
|
|
|
+ statement = await connection.prepare('SELECT "randomnumber", "id" FROM "world" WHERE id = $1')
|
|
|
for row_id in row_ids:
|
|
|
number = await statement.fetchval(row_id)
|
|
|
worlds.append({'id': row_id, 'randomNumber': number})
|
|
@@ -87,39 +79,43 @@ async def render_fortunes_template() -> str:
|
|
|
async with connection_pool.acquire() as connection:
|
|
|
fortunes = await connection.fetch('SELECT * FROM Fortune')
|
|
|
|
|
|
- fortunes.append(ADDITIONAL_ROW)
|
|
|
- fortunes.sort(key=sort_fortunes_key)
|
|
|
- return template.render(fortunes=fortunes)
|
|
|
+ fortunes.append([0, 'Additional fortune added at request time.'])
|
|
|
+ fortunes.sort(key=itemgetter(1))
|
|
|
+ return fortune_template.render(fortunes=fortunes)
|
|
|
|
|
|
|
|
|
@get(path='/updates')
|
|
|
-async def database_updates(queries: Any = None) -> list[dict[str, int]]:
|
|
|
- num_queries = get_num_queries(queries)
|
|
|
- updates = [(randint(1, 10000), randint(1, 10000)) for _ in range(num_queries)]
|
|
|
- worlds = [{'id': row_id, 'randomNumber': number} for row_id, number in updates]
|
|
|
+async def database_updates(queries: None | str = None) -> list[dict[str, int]]:
|
|
|
+ num_queries = normalize_queries(queries)
|
|
|
+ updates = list(zip(sorted(sample(range(1, 10000 + 1), num_queries)), sample(range(1, 10000), num_queries)))
|
|
|
+
|
|
|
+ worlds = [
|
|
|
+ {"id": row_id, "randomNumber": number} for row_id, number in updates
|
|
|
+ ]
|
|
|
|
|
|
async with connection_pool.acquire() as connection:
|
|
|
- statement = await connection.prepare(READ_ROW_SQL)
|
|
|
- for row_id, number in updates:
|
|
|
+ statement = await connection.prepare('SELECT "id", "randomnumber" FROM "world" WHERE id = $1')
|
|
|
+ for row_id, _ in updates:
|
|
|
await statement.fetchval(row_id)
|
|
|
- await connection.executemany(WRITE_ROW_SQL, updates)
|
|
|
+ await connection.executemany('UPDATE "world" SET "randomnumber"=$1 WHERE id=$2', updates)
|
|
|
|
|
|
return worlds
|
|
|
|
|
|
|
|
|
@get(path='/plaintext', media_type=MediaType.TEXT)
|
|
|
-async def plaintext() -> bytes:
|
|
|
+def plaintext() -> bytes:
|
|
|
return b'Hello, world!'
|
|
|
|
|
|
|
|
|
app = Starlite(
|
|
|
route_handlers=[
|
|
|
+ database_updates,
|
|
|
json_serialization,
|
|
|
- single_database_query,
|
|
|
multiple_database_queries,
|
|
|
+ plaintext,
|
|
|
render_fortunes_template,
|
|
|
- database_updates,
|
|
|
- plaintext
|
|
|
+ single_database_query,
|
|
|
],
|
|
|
- on_startup=[setup_database]
|
|
|
+ on_startup=[init_connection_pool],
|
|
|
+ openapi_config=None,
|
|
|
)
|