The code for the Flask web application is based on the Flask official tutorial, with modifications to make some network operations.
Here is the summary of the steps that follow.
- Make sure you have installed the latest version of AWS CLI. Use an IAM entity for the AWS CLI that has permissions to access CodeGuru Profiler, S3 and SQS to create all the required components for the demo application to run.
- Create a profiling group in CodeGuru Profiler, named
PythonDemoFlaskApplication. - Create a S3 bucket, e.g.
s3://any_BUCKET_NAME. Note, the bucket name must be unique across all of Amazon S3. See here for more details. - Create virtual environment with venv, e.g.
python3 -m venv ./venv. - Activate the virtual environment, e.g.
source venv/bin/activate. - Install the needed dependencies that are used for the demo application through
pipusing therequirements.txtfile.
Here are the commands to run on your machine.
aws configure # Set up your AWS credentials and region as usual.aws codeguruprofiler create-profiling-group --profiling-group-name PythonDemoFlaskApplication
# It is required to set the DEMO_APP_BUCKET_NAME environment applications for later running the demo application.
# Make sure you update `YOUR-BUCKET-NAME-REPLACE-ME`with a bucket name that is unique across all of Amazon S3.
export DEMO_APP_BUCKET_NAME=BUCKET_NAME
aws s3 mb s3://${DEMO_APP_BUCKET_NAME}python3 -m venv ./venv
source venv/bin/activate
pip install -r requirements.txtThe main entry point is in sample-demo-flask-app/flaskr/__init__.py that initializes applications, like the main one from sample-demo-flask-app/flaskr/blog.py.
-
The configuration for gunicorn is in
gunicorn_conf.py. -
There in the
gunicorn_conf.pyyou can find the configuration for CodeGuru Profiler to start and to log.
After the following command, there will now be a flaskr.sqlite file in the instance/ folder in your project.
flask init-dbStart the service using the gunicorn configuration.
# Start the service using a gunicorn server and 5 workers.
gunicorn "flaskr:create_app()" -c gunicorn_conf.py --log-level DEBUG --workers=5Note that the agent currently does NOT work with -k option for gevent workers.
Generate traffic by running the following script that will make thousands of requests to the local server started at http://127.0.0.1:8000.
./generate-traffic.shGo to the AWS CodeGuru Profiler console to check the results. Choose the region you picked and your profiling group.
The attached screenshot was captured by hiding the gunicorn.workers.sync:SyncWorker:wait frame because it covered more than half of the time.