Skip to content

Commit 9394401

Browse files
authored
Update and fix sanic test (#9304)
1 parent 68e4d14 commit 9394401

File tree

4 files changed

+37
-33
lines changed

4 files changed

+37
-33
lines changed

frameworks/Python/sanic/app.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import sanic
1212
from sanic import response
1313

14+
from orjson import dumps
15+
1416

1517
logger = getLogger(__name__)
1618

@@ -41,23 +43,26 @@ def get_num_queries(queries):
4143
return query_count
4244

4345

44-
connection_pool = None
4546
sort_fortunes_key = itemgetter(1)
4647
template = load_fortunes_template()
4748

48-
app = sanic.Sanic(name=__name__)
49+
app = sanic.Sanic(name=__name__, dumps=dumps)
4950

5051

5152
@app.listener('before_server_start')
5253
async def setup_database(app, loop):
53-
global connection_pool
54-
connection_pool = await asyncpg.create_pool(
55-
user=os.getenv('PGUSER', 'benchmarkdbuser'),
56-
password=os.getenv('PGPASS', 'benchmarkdbpass'),
57-
database='hello_world',
58-
host='tfb-database',
59-
port=5432
60-
)
54+
app.ctx.pool = await asyncpg.create_pool(
55+
user=os.getenv('PGUSER', 'benchmarkdbuser'),
56+
password=os.getenv('PGPASS', 'benchmarkdbpass'),
57+
database='hello_world',
58+
host='tfb-database',
59+
port=5432
60+
)
61+
62+
63+
@app.listener('after_server_stop')
64+
async def close_database(app, loop):
65+
app.ctx.pool.close()
6166

6267

6368
@app.get('/json')
@@ -69,7 +74,7 @@ def json_view(request):
6974
async def single_database_query_view(request):
7075
row_id = randint(1, 10000)
7176

72-
async with connection_pool.acquire() as connection:
77+
async with request.app.ctx.pool.acquire() as connection:
7378
number = await connection.fetchval(READ_ROW_SQL, row_id)
7479

7580
return response.json(
@@ -84,7 +89,7 @@ async def multiple_database_queries_view(request):
8489
row_ids = sample(range(1, 10000), num_queries)
8590
worlds = []
8691

87-
async with connection_pool.acquire() as connection:
92+
async with request.app.ctx.pool.acquire() as connection:
8893
statement = await connection.prepare(READ_ROW_SQL)
8994
for row_id in row_ids:
9095
number = await statement.fetchval(row_id)
@@ -100,7 +105,7 @@ async def multiple_database_queries_view(request):
100105

101106
@app.get('/fortunes')
102107
async def fortunes_view(request):
103-
async with connection_pool.acquire() as connection:
108+
async with request.app.ctx.pool.acquire() as connection:
104109
fortunes = await connection.fetch('SELECT * FROM Fortune')
105110

106111
fortunes.append(ADDITIONAL_ROW)
@@ -112,22 +117,21 @@ async def fortunes_view(request):
112117

113118
@app.get('/updates')
114119
async def database_updates_view(request):
115-
worlds = []
116-
updates = set()
117120
queries = request.args.get('queries', 1)
121+
num_queries = get_num_queries(queries)
122+
# To avoid deadlock
123+
ids = sorted(sample(range(1, 10000 + 1), num_queries))
124+
numbers = sorted(sample(range(1, 10000), num_queries))
125+
updates = list(zip(ids, numbers))
118126

119-
async with connection_pool.acquire() as connection:
120-
statement = await connection.prepare(READ_ROW_SQL_TO_UPDATE)
121-
122-
for row_id in sample(range(1, 10000), get_num_queries(queries)):
123-
record = await statement.fetchrow(row_id)
124-
world = dict(
125-
id=record['id'], randomNumber=record['randomnumber']
126-
)
127-
world['randomNumber'] = randint(1, 10000)
128-
worlds.append(world)
129-
updates.add((world['id'], world['randomNumber']))
127+
worlds = [
128+
{"id": row_id, "randomNumber": number} for row_id, number in updates
129+
]
130130

131+
async with request.app.ctx.pool.acquire() as connection:
132+
statement = await connection.prepare(READ_ROW_SQL)
133+
for row_id, _ in updates:
134+
await statement.fetchval(row_id)
131135
await connection.executemany(WRITE_ROW_SQL, updates)
132136

133137
return response.json(worlds, headers=get_headers())

frameworks/Python/sanic/benchmark_config.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
"database_os": "Linux",
2424
"display_name": "Sanic",
2525
"notes": "",
26-
"versus": "None",
27-
"tags": ["broken"]
26+
"versus": "None"
2827
}
2928
}
3029
]
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
asyncpg==0.25.0
1+
asyncpg==0.29.0
22
Jinja2==3.1.4
3-
sanic==22.6.1
4-
uvloop==0.16.0
3+
sanic==24.6.0
4+
uvloop==0.20.0
5+
orjson==3.10.7

frameworks/Python/sanic/sanic.dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
FROM python:3.8
1+
FROM python:3.12
22

33
ADD ./requirements.txt /sanic/requirements.txt
44

5-
RUN pip3 install cython==0.29.13 && \
5+
RUN pip3 install cython==3.0.11 && \
66
pip3 install -r /sanic/requirements.txt
77

88
ADD ./ /sanic

0 commit comments

Comments
 (0)