8000 Add Profiler code samples (#2027) · simoncklee/python-docs-samples@3aa7511 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3aa7511

Browse files
jqlltswast
authored andcommitted
Add Profiler code samples (GoogleCloudPlatform#2027)
Add Profiler code samples with some functions to make the profile look more interesting
1 parent 92944a6 commit 3aa7511

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

profiler/quickstart/main.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2019 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
"""An example of starting https://cloud.google.com/profiler."""
17+
18+
# [START profiler_python_quickstart]
19+
import googlecloudprofiler
20+
21+
22+
def main():
23+
# Profiler initialization. It starts a daemon thread which continuously
24+
# collects and uploads profiles. Best done as early as possible.
25+
try:
26+
googlecloudprofiler.start(
27+
service='hello-profiler',
28+
service_version='1.0.1',
29+
# verbose is the logging level. 0-error, 1-warning, 2-info,
30+
# 3-debug. It defaults to 0 (error) if not set.
31+
verbose=3,
32+
# project_id must be set if not running on GCP.
33+
# project_id='my-project-id',
34+
)
35+
except (ValueError, NotImplementedError) as exc:
36+
print(exc) # Handles errors here
37+
# [END profiler_python_quickstart]
38+
busyloop()
39+
40+
41+
# A loop function which spends 30% CPU time on loop3() and 70% CPU time
42+
# on loop7().
43+
def busyloop():
44+
while True:
45+
loop3()
46+
loop7()
47+
48+
49+
def loop3():
50+
for _ in range(3):
51+
loop()
52+
53+
54+
def loop7():
55+
for _ in range(7):
56+
loop()
57+
58+
59+
def loop():
60+
for _ in range(10000):
61+
pass
62+
63+
64+
if __name__ == '__main__':
65+
main()

profiler/quickstart/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-profiler

0 commit comments

Comments
 (0)
0