Skip to content

Instantly share code, notes, and snippets.

@crimsonaltima
Created March 29, 2023 02:29
Show Gist options
  • Save crimsonaltima/402b41ee23be20491d73f97832c0c017 to your computer and use it in GitHub Desktop.
Save crimsonaltima/402b41ee23be20491d73f97832c0c017 to your computer and use it in GitHub Desktop.
Examples of Google Cloud Profiler Python with wait times
#!/usr/bin/env python
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""An example of starting https://cloud.google.com/profiler."""
# [START profiler_python_quickstart]
import googlecloudprofiler
import os
def main():
# Profiler initialization. It starts a daemon thread which continuously
# collects and uploads profiles. Best done as early as possible.
try:
googlecloudprofiler.start(
service='hello-profiler-doc-python-seq',
service_version='1.0.1',
# verbose is the logging level. 0-error, 1-warning, 2-info,
# 3-debug. It defaults to 0 (error) if not set.
verbose=3,
# project_id must be set if not running on GCP.
project_id='data-profiler-experimentor',
)
except (ValueError, NotImplementedError) as exc:
print(exc) # Handle errors here
# [END profiler_python_quickstart]
busyloop()
# A loop function which spends 30% CPU time on loop3() and 70% CPU time
# on loop7().
def busyloop():
while True:
step1_sleep_10_secs()
step2_sleep_15_secs()
step3_sleep_5_secs()
step4_sleep_20_secs()
step5_sleep_25_secs()
def loop3():
for _ in range(3):
loop()
def loop7():
for _ in range(7):
loop()
def loop():
for _ in range(10000):
pass
import time
def step1_sleep_10_secs():
loop()
time.sleep(10)
def step2_sleep_15_secs():
loop()
time.sleep(15)
def step3_sleep_5_secs():
loop()
time.sleep(5)
def step4_sleep_20_secs():
loop()
time.sleep(20)
def step5_sleep_25_secs():
loop()
time.sleep(25)
if __name__ == '__main__':
main()
#!/usr/bin/env python
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""An example of starting https://cloud.google.com/profiler."""
# [START profiler_python_quickstart]
import googlecloudprofiler
import os
def main():
# Profiler initialization. It starts a daemon thread which continuously
# collects and uploads profiles. Best done as early as possible.
try:
googlecloudprofiler.start(
service='hello-profiler-doc-python-seq2',
service_version='1.0.1',
# verbose is the logging level. 0-error, 1-warning, 2-info,
# 3-debug. It defaults to 0 (error) if not set.
verbose=3,
# project_id must be set if not running on GCP.
project_id='data-profiler-experimentor',
)
except (ValueError, NotImplementedError) as exc:
print(exc) # Handle errors here
# [END profiler_python_quickstart]
busyloop()
# A loop function which spends 30% CPU time on loop3() and 70% CPU time
# on loop7().
def busyloop():
while True:
step1_sleep_10_secs()
step2_sleep_15_secs()
step3_sleep_5_secs()
step4_sleep_20_secs()
step5_sleep_25_secs()
def loop3():
for _ in range(3):
loop()
def loop7():
for _ in range(7):
loop()
def loop():
for _ in range(10000):
pass
import time
def step1_sleep_10_secs():
loop()
time.sleep(1)
def step2_sleep_15_secs():
loop()
time.sleep(2)
def step3_sleep_5_secs():
loop()
time.sleep(3)
def step4_sleep_20_secs():
loop()
time.sleep(4)
def step5_sleep_25_secs():
loop()
time.sleep(2)
if __name__ == '__main__':
main()
#!/usr/bin/env python
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""An example of starting https://cloud.google.com/profiler."""
# [START profiler_python_quickstart]
import googlecloudprofiler
import os
def main():
# Profiler initialization. It starts a daemon thread which continuously
# collects and uploads profiles. Best done as early as possible.
try:
googlecloudprofiler.start(
service='hello-profiler-doc-python-seq3',
service_version='1.0.1',
# verbose is the logging level. 0-error, 1-warning, 2-info,
# 3-debug. It defaults to 0 (error) if not set.
verbose=3,
# project_id must be set if not running on GCP.
project_id='data-profiler-experimentor',
)
except (ValueError, NotImplementedError) as exc:
print(exc) # Handle errors here
# [END profiler_python_quickstart]
busyloop()
# A loop function which spends 30% CPU time on loop3() and 70% CPU time
# on loop7().
def busyloop():
while True:
step1_sleep_10_secs()
step2_sleep_15_secs()
step3_sleep_5_secs()
step4_sleep_20_secs()
step5_sleep_25_secs()
step1_sleep_10_secs()
step1_sleep_10_secs()
step1_sleep_10_secs()
step1_sleep_10_secs()
step1_sleep_10_secs()
def loop3():
for _ in range(3):
loop()
def loop7():
for _ in range(7):
loop()
def loop():
for _ in range(10000):
pass
import time
def step1_sleep_10_secs():
loop()
time.sleep(1)
def step2_sleep_15_secs():
loop()
time.sleep(2)
def step3_sleep_5_secs():
loop()
time.sleep(3)
def step4_sleep_20_secs():
loop()
time.sleep(4)
def step5_sleep_25_secs():
loop()
time.sleep(2)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment