Profiling
Profiling the PVGIS Web API
This page explains how to profile the PVGIS Web API using profilers (Scalene, Pyinstrument, Yappi, FunctionTrace). in order to identify performance bottlenecks and optimize application efficiency. It also covers installing the performance-boosting packages uvloop, httptools.
Web API Profiling¶
It covers installing additional performance-boosting packages (uvloop, httptools) and setting up various profilers (scalene, pyinstrument, yappi).
Performance Optimization¶
Install uvloop
uvloop reportedly brings a 10% performance increase over asyncio.
To install uvloop, run:
FastAPI will automatically use it from your environment.
(Source: M. Trylesinski, Performance tips by the FastAPI Expert, Europython Conference, 2023, Prague)
Install httptools
httptools reportedly brings a 10% performance increase over h11.
To install httptools, run:
FastAPI will automatically use it from your environment.
(Source: M. Trylesinski, Performance tips by the FastAPI Expert, Europython Conference, 2023, Prague)
Profiling Tools¶
Supported profilers for performance analysis include :
scalenepyinstrumentyappifunctiontrace
Environment configuration¶
Set the environment variable PVGISPROTOTYPE_WEB_API_ENVIRONMENT to switch between the Production and Development environments.
This can be done either :
-
in a file named
.envand placed in the main directory of the source code -
alternatively, setting the variable in question directly via :
Profiling settings¶
Configuration is defined in pvgisprototype.web_api.config.settings.py.
Example settings :
from pvgisprototype.web_api.config.options import (
Profiler,
ProfileOutput,
)
from pvgisprototype.web_api.config.options import (
LogLevel,
LogFormat,
)
# Common default settings
LOG_LEVEL_DEFAULT = LogLevel.info
PROFILING_ENABLED_PRODUCTION_DEFAULT = False
LOG_FORMAT_DEFAULT = LogFormat.uvicorn
# Development default settings
LOG_LEVEL_DEVELOPMENT_DEFAULT = LogLevel.info
PROFILING_ENABLED_DEVELOPMENT_DEFAULT = True
PROFILER_DEVELOPMENT_DEFAULT = Profiler.Pyinstrument
PROFILE_OUTPUT_DEVELOPMENT_DEFAULT = ProfileOutput.json
MEASURE_REQUEST_TIME_DEVELOPMENT_DEFAULT = True
# Production default settings
MEASURE_REQUEST_TIME_PRODUCTION_DEFAULT = False
Available options :
| Variable | Options |
|---|---|
| PROFILER_DEVELOPMENT_DEFAULT | Scalene, Pyinstrument, Yappi, FunctionTrace |
| PROFILE_OUTPUT_DEVELOPMENT_DEFAULT* | JSON, PSTAT, CALLGRIND, HTML |
Note
If a profiler doesn't support the specified output format, defaults are :
-
JSONforScalene,PyinstrumentandFunctionTrace -
PSTATforYappi
Pyinstrument¶
Installation¶
To install pyinstrument, run:
Usage¶
To profile with pyinstrument, set PROFILER_DEVELOPMENT_DEFAULT="Pyinstrument" and run the Web API :
uvicorn pvgisprototype.webapi:app --port 8001
After performing a request to the Web API, a profiling file will be generated in the main directory named profile_pyinstrument.<extension> where extension bases upon chosen output format, for example JSON.
Yappi¶
Installation¶
To install yappi, run:
Usage¶
To profile with yappi, set PROFILER_DEVELOPMENT_DEFAULT="Yappi" and start the Web API via :
uvicorn pvgisprototype.webapi:app --port 8001
Once a request to the Web API is completed, a profiling file named profile_yappi.<extension> where extension depends on the chosen output format, will be generated in the main directory
Scalene¶
Installation¶
To install scalene, run:
and verify installation by typing:
Running scalene from the command line¶
In order to use scalene along with a local instance of the PVGIS Web API, run:
Then, open the application using the Swagger UI, run a specific request and after the request completes terminate the local instance using Ctrl + C. The profiling file will be saved to the specified path.
Middleware Usage¶
To profile with scalene as a middleware set the PROFILER option to Scalene and run the server via
Once the Web API server is invoked using scalene we can perform a request and stop it right after. The output file will be named profile_scalene.json.
FunctionTrace¶
Installation¶
FunctionTrace comes in two necessary pieces : a server, and a language-specific client.
Note
It's currently necessary to have cargo installed in order to install FunctionTrace, as functiontrace-server is not packaged for all supported operating systems. You can install cargo via rustup.
FunctionTrace as a middleware¶
Warning
If you wish to use functiontrace as yet another profiling tool, you'd need to install this manually as it is released under a free non commercial license which is incompatible with our open source license. Practically this means that you :
- can install it via
pip install functiontrace - cannot distribute your software under an open source license that does not restrict further commercial use.
To trace the PVGIS Web API webapi.py, load it via functiontrace :
Viewing Traces¶
To view a recorded trace file, go to the Firefox Profiler and choose Load a profile from file, then select the desired trace file. This step will perform some processing, but is entirely local - your trace file never leaves your own machine!
Note: If this is your first time using the Firefox Profiler, you may want to skim the UI guide for it.