Skip to content
This repository was archived by the owner on Mar 24, 2026. It is now read-only.

[aiohttp] Eliminate some overhead in jinja environment - #6289

Merged
NateBrady23 merged 8 commits into
TechEmpower:masterfrom
Dreamsorcerer:patch-2
Jan 18, 2021
Merged

NateBrady23 merged 8 commits into
TechEmpower:masterfrom
Dreamsorcerer:patch-2

Conversation

@Dreamsorcerer

Copy link
Copy Markdown
Contributor

I noticed that uvicorn is practically cheating by pre-loading a jinja template, without using the rest of the jinja environment like a regular application would.

Rather than copying that cheat, I had a look at what unneeded overheads existed in the jinja environment. The cache_size causes LRUCache to be replaced with a simple dict. auto_reload ensures we only check the disk on the first load.

I'm not sure exactly how the benchmarks are run, but it would probably be a good idea if the benchmarks did a dummy run first, which allows connections to be created and caches to be populated. Then do the real benchmark. Or maybe a best of 3 runs or similar (as long as the app is not reloaded between runs). This would ensure the comparisons are fairer without needing every framework to implement logic to ensure things are prepopulated before the benchmark runs.

@Dreamsorcerer Dreamsorcerer changed the title Eliminate some overhead in jinja environment [aiohttp] Eliminate some overhead in jinja environment Jan 12, 2021
@Dreamsorcerer

Copy link
Copy Markdown
Contributor Author

Hmm, test failure appears that by random chance number of DB queries was 1 less than allowed in the updates.

This looks like random chance, as it picks a series of random ids. Any duplicate ids will result in 1 less DB update (as everything is prepared and deduplicated by sqlalchemy before commit).

@Dreamsorcerer

Copy link
Copy Markdown
Contributor Author

I see there are others using the same cheat as well. Would it be better to do the same thing, even if it doesn't really match how an application would actually be written?

@NateBrady23

Copy link
Copy Markdown
Member

@Dreamsorcerer Yes, that would be ideal. We do expect that if the same ID be chosen twice at random, two updates would happen.

@Dreamsorcerer

Dreamsorcerer commented Jan 14, 2021 •

Copy link
Copy Markdown
Contributor Author

I don't think any of the Python examples do 2 updates for the same ID, they are all based on the same logic using SQLAlchemy. I assume this is why the tests match an approximate number in the first place (it appears to allow 112 updates to be missing, and this time we had 113).

They all use code something like:

with sess.begin():
  for id_ in ids:
     world = sess.get(World, id_)
     world.randomnumber = rand_new

Because the commit doesn't happen until the loop is finished, I'm sure sqlalchemy is smart enough to deduplicate the unneeded updates before it sends the command to the server. It probably also caches the select in there too.

@NateBrady23

Copy link
Copy Markdown
Member

Some implementations make sure that each random ID is unique, another way to solve. Up to you.

@Dreamsorcerer

Copy link
Copy Markdown
Contributor Author

I mean, if there's an example, we can do this for all of them, but the logic appears the same (allowing duplicates) in all the ones I looked at (uvicorn, flask, aiohttp, fastapi).

@NateBrady23

Copy link
Copy Markdown
Member

Sorry, I'm not talking about Python specifically. But I imagine that each of the ones you looked at with the same logic will also have this problem, and may encounter it on official round runs.

@Dreamsorcerer

Copy link
Copy Markdown
Contributor Author

Would it be reasonable to generate the random ids in a global variable that is referenced in the view function?

This means that multiple calls to the same endpoint would update the same objects, but would make it easier to ensure no random-related timings affect the benchmark making it fairer. That should be easy for me to implement quickly.

@Dreamsorcerer

Dreamsorcerer commented Jan 14, 2021 •

Copy link
Copy Markdown
Contributor Author

Or, actually, maybe a simple solution would be:

all_ids = list(range(10000))
random.shuffle(all_ids)
ids = all_ids[:num_ids]

I'm guessing random.shuffle should be pretty consistent for a fixed length.

@Dreamsorcerer

Dreamsorcerer commented Jan 14, 2021 •

Copy link
Copy Markdown
Contributor Author

I had a look through a couple of other languages, and didn't see any others to base the logic on, they all seem to do the same thing. Except this one, which appears to not be random at all, just iterates from 0: https://github.com/TechEmpower/FrameworkBenchmarks/blob/master/frameworks/JavaScript/nodejs/handlers/mongoose.js#L82

(edit: Nevermind, it mixes the array, so that looks like the way to go)

@Dreamsorcerer

Copy link
Copy Markdown
Contributor Author

Switched to using the same cheat as others.

I'll propose a fix for randint separately.

@NateBrady23
NateBrady23 merged commit 3830f1f into TechEmpower:master Jan 18, 2021
andreasrueb pushed a commit to digitallyinduced/IHP-FrameworkBenchmarks that referenced this pull request Apr 13, 2021
)

* Eliminate some overhead in jinja environment

* Update main.py

* Update views.py

* Update requirements.txt

* Update views.py

* Update requirements.txt

* Update views.py

* Update main.py
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants