-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
511 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import asyncio | ||
import multiprocessing | ||
import random | ||
import string | ||
import sys | ||
from concurrent import futures | ||
|
||
import orjson | ||
|
||
try: | ||
IS_FREETHREADING = not sys._is_gil_enabled() # type: ignore | ||
except Exception: | ||
IS_FREETHREADING = False | ||
|
||
CHARS = string.ascii_lowercase + string.ascii_uppercase | ||
|
||
NUM_THREADS = min(multiprocessing.cpu_count(), 16) | ||
|
||
MULTIPLIER = int(sys.argv[1]) if len(sys.argv) == 2 else 1 | ||
|
||
|
||
def random_string(): | ||
return "".join((random.choice(CHARS) for _ in range(0, 32))) | ||
|
||
|
||
def per_thread_func(data): | ||
serialized = orjson.dumps(data) | ||
deserialized = orjson.loads(serialized) | ||
assert deserialized == data | ||
|
||
|
||
async def loads_test(): | ||
TEST_MESSAGE = f"concurrent serialization test running ..." | ||
|
||
sys.stdout.write(TEST_MESSAGE) | ||
sys.stdout.flush() | ||
|
||
sys.stdout.write(f"\r{TEST_MESSAGE} creating tasks\n") | ||
|
||
unique_items = 1000 | ||
|
||
keys_per_dictionary = 16 | ||
|
||
# must force key map cache eviction | ||
assert keys_per_dictionary * unique_items > 5000 | ||
|
||
num = 250 * MULTIPLIER | ||
|
||
data = [] * num | ||
for _ in range(unique_items): | ||
prefix = random_string() | ||
data.append( | ||
{ | ||
f"{prefix}_{i}": [True, False, None, "", "🐈", []] | ||
for i in range(keys_per_dictionary) | ||
} | ||
) | ||
|
||
tasks = [] | ||
for _ in range(num): | ||
tasks.extend( | ||
list( | ||
( | ||
asyncio.create_task(asyncio.to_thread(per_thread_func, each)) | ||
for each in data | ||
) | ||
) | ||
) | ||
|
||
sys.stdout.write(f"\r{TEST_MESSAGE} running {len(tasks):,} tasks\n") | ||
await asyncio.gather(*tasks) | ||
|
||
sys.stdout.write(f"\r{TEST_MESSAGE} ok\n") | ||
|
||
|
||
async def list_mutation_test(): | ||
TEST_MESSAGE = f"concurrent list mutation test running ..." | ||
|
||
sys.stdout.write(TEST_MESSAGE) | ||
sys.stdout.flush() | ||
num = 1000 * MULTIPLIER | ||
fixture = [None] * num | ||
|
||
tasks = [] | ||
for _ in range(num): | ||
tasks.append(asyncio.create_task(asyncio.to_thread(orjson.dumps, fixture))) | ||
tasks.append(asyncio.create_task(asyncio.to_thread(fixture.pop))) | ||
|
||
await asyncio.gather(*tasks) | ||
|
||
assert len(fixture) == 0 | ||
|
||
sys.stdout.write(f"\r{TEST_MESSAGE} ok\n") | ||
|
||
|
||
async def dict_mutation_test(): | ||
TEST_MESSAGE = f"concurrent dict mutation test running ..." | ||
|
||
sys.stdout.write(TEST_MESSAGE) | ||
sys.stdout.flush() | ||
num = 1000 * MULTIPLIER | ||
fixture = {f"key_{i}": None for i in range(num)} | ||
|
||
tasks = [] | ||
for i in reversed(range(num)): | ||
tasks.append(asyncio.create_task(asyncio.to_thread(orjson.dumps, fixture))) | ||
tasks.append(asyncio.create_task(asyncio.to_thread(fixture.pop, f"key_{i}"))) | ||
|
||
await asyncio.gather(*tasks) | ||
|
||
assert len(fixture) == 0 | ||
|
||
sys.stdout.write(f"\r{TEST_MESSAGE} ok\n") | ||
|
||
|
||
async def main(): | ||
asyncio.get_running_loop().set_default_executor( | ||
futures.ThreadPoolExecutor(max_workers=NUM_THREADS) | ||
) | ||
sys.stdout.write( | ||
f"concurrent tests running with free-threading {str(IS_FREETHREADING).lower()} on {NUM_THREADS} threads ...\n" | ||
) | ||
|
||
await list_mutation_test() | ||
await dict_mutation_test() | ||
await loads_test() | ||
|
||
|
||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
flask;sys_platform!="win" | ||
gunicorn;sys_platform!="win" | ||
httpx==0.24.1;sys_platform!="win" | ||
httpx==0.27.2;sys_platform!="win" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.