Improve response time `//moto-api/reset`
See original GitHub issueHi everyone,
I am using moto 2.2.0 on Python 3.8. I am testing code which uses s3fs, and testing it using moto_server s3
. This is necessary as moto seems to have problems working with async file systems unless it runs as a standalone server.
There seems to be a large (5-6 second) overhead when calling reset
the first time in my fixture, even after not having touched the emulated S3 at all. Subsequent calls return very quickly. Here is an example from a test run:
.2021-07-26 16:19:39,453 WARNING Returning from s3 base...
127.0.0.1 - - [26/Jul/2021 16:19:46] "POST //moto-api/reset HTTP/1.1" 200 -
I can’t paste the exact setup since a lot of the code is internal, but the problematic fixture is the following:
@pytest.fixture(name="s3_base")
def _s3_base(monkeypatch, s3_session_base, mocked_s3_uri):
try:
with monkeypatch.context() as m:
# This ensures we always fail if we accidentally bypass moto in tests which should mock AWS, while the
# monkeypatch context also prevents interference with (exceptional) tests that actually need to touch the
# real S3, e.g., to check whether a bucket exists.
m.setenv("AWS_SECRET_ACCESS_KEY", "fake_secret_access_key")
m.setenv("AWS_ACCESS_KEY_ID", "fake_access_key_id")
yield
finally:
logging.warning("Returning from s3 base...")
requests.post(mocked_s3_uri + "/moto-api/reset")
logging.warning("Server reset OK")
s3_session_base
basically provides the moto server wrapped in a fixture, similar to the code in the s3fs
code base.
The gap between making the POST request in the finally
block and the moto server actually acknowledging it is ~7 seconds. Everything else, including moto server setup, is fast, less than 100ms or so.
My original goal was to allow the moto server to be reused throughout a module, and just reset it for every test, but right now it seems like just re-creating the server for every single test is faster, which seems odd.
Any pointers as to what could be causing the moto server to respond so slowly? Maybe something I could have misconfigured?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5
This is fantastic @bblommers! Works like a charm with the new update. The teardowns now take negligible amounts of time. Thank you!
Before:
After:
Yeah, that makes sense @AndreiBarsan. Either that, or simply add a specific endpoint to reset only a single service (
requests.post("http://localhost:5000/moto-api/resetservice/s3")
). I’ll mark this issue as an enhancement, if anyone wants to pick this up.