What is Profiling and GCP Cloud Profiler?
Performance profiling allows to discover which unit of application, is misbehaving in terms of execution time and system resource usage. For example, if a function in an application takes 80% of time, it’s worth investigating and fixing it to improve overall app performance.
Analysing performance of production application needs lot of effort. Most of the times, load testing in test environment does not give accurate estimates for production workloads.
Continuous profiling of production systems is an effective way to discover where resources like CPU and memory are consumed as a service operates in prod environment. Profiling adds additional load on the production system, so it’s resource consumption and time must be as minimul as possible.
GCP Cloud Profiler is a statistical, low-overhead profiler that continuously gathers CPU usage and memory-allocation information from applications. It attributes that information to the source code that generated it, to identify the parts of the application that are consuming the most resources. At present, Cloud Profiler supports CPU time and Wall time for Python language.
In this demo we are going to use a Python program to profile.
Profiling using GCP Cloud Profiler
Enabling the Profiler API
Use the following gcloud command to enable Profile API in your GCP project.
gcloud services enable cloudprofiler.googleapis.com
In this demo I’m running python program on gce instance so I’m going to install profiler package using following command.
pip3 install google-cloud-profiler
When you are trying to profile Docker applications, you need to install via Dockerfile. For cloud fucnctions or app engine application add this package to requirements.txt file.
Using Cloud Profiler
Add following code to a file and start running it. replace project_id value with your project id.
#!/usr/bin/env python import time import googlecloudprofiler def main(): try: googlecloudprofiler.start( service='demo-profiler', service_version='v1', verbose=3, project_id='devops-counsel-demo' ) except (ValueError, NotImplementedError) as exc: print(exc) load_generator() def load_generator(): while True: load30() load50() load70() load90() def load30(): for _ in range(3): load() def load50(): for _ in range(5): load() def load70(): for _ in range(7): load() def load90(): for _ in range(9): load() def load(): time.sleep(10) if __name__ == '__main__': main()
Once you started the application, it will create WALL and CPU profiles like below
Analyzing data on Profiler Console
After Profiler has collected data, you can view and analyze the data from cloud console.
The profiler console has couple of drop menus to switch between services and versions. filters to focus on specific applications units.
In this simple Cloud Profiler quick start demo, I have covered how to profile a Python program. for more information on Cloud Profiler read Google Cloud Official documentation.