question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve response time `//moto-api/reset`

See original GitHub issue

Hi 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
AndreiBarsancommented, Sep 16, 2021

This is fantastic @bblommers! Works like a charm with the new update. The teardowns now take negligible amounts of time. Thank you!

Before:

====================================================================== test session starts ======================================================================
platform linux -- Python 3.8.7, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/andrei/... configfile: setup.cfg
plugins: ...
gw0 [143] / gw1 [143] / gw2 [143] / gw3 [143]
.........................................................................................................................s.....................           [100%]
============================================================= slowest 10 durations ==============================================================
5.23s teardown <moto test>
5.20s teardown <moto_test>
5.16s teardown <moto test>
5.08s teardown <moto test>
0.92s setup    <moto test>
0.83s setup    <moto test>
0.81s setup    <moto test>
...
========================================================= 142 passed, 1 skipped in 18.17s ========================================================

After:

python -m pytest ... --maxfail=4 --durations=10 -n 4
====================================================================== test session starts ======================================================================platform linux -- Python 3.8.7, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/andrei/..., configfile: setup.cfg
plugins: ...
gw0 [143] / gw1 [143] / gw2 [143] / gw3 [143]
.....................................................................................................................s.........................           [100%]
===================================================================== slowest 10 durations ======================================================================
0.78s setup    <moto test>
0.67s setup    <moto test> 
0.66s setup    <moto test>
0.65s setup    <moto test>
0.51s call     <unrelated test>
...
================================================================ 142 passed, 1 skipped in 12.71s ===============================================================
1reaction
bblommerscommented, Jul 27, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

8 Ways to Effectively Reduce Server Response Time
Reducing your web server response time can boost your website performance. Learn how to reduce server response time with this guide.
Read more >
7 Ways to Reduce Server Response Time - phoenixNAP
In this article, learn how to reduce server response time with 7 actionable tips. Is your server slow? TTFB suffering? Improve your server ......
Read more >
Moto Documentation - Read the Docs
this time, moto does not have support for handling the configuration history changes ... This option allows to prettify responses from moto.
Read more >
5 Ways to Reduce Server Response Times | Rigor
Rigor is the only Digital Experience Monitoring (DEM) solution designed to find, fix and prevent web performance defects to improve the user experiences...
Read more >
RESPRNGP - Response time distribution (%) - IBM
RESPRNGP: Response time increase - Response time distribution (%). This report shows the number of transactions for each range of response times as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found