Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e019f3

Browse files
jqlltswast
authored andcommitted
Add Profiler code samples for App Engine (GoogleCloudPlatform#2028)
* Add Profiler code samples for App Engine * Fix comments
1 parent 3aa7511 commit 3e019f3

File tree

9 files changed

+167
-1
lines changed

9 files changed

+167
-1
lines changed

profiler/appengine/flexible/app.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
runtime: python
2+
env: flex
3+
entrypoint: gunicorn -b :$PORT main:app
4+
5+
# App Engine flexible Python 3 runtime uses Python 3.7.x, which supports both
6+
# CPU and Wall profiling.
7+
runtime_config:
8+
python_version: 3
9+
10+
# This sample incurs costs to run on the App Engine flexible environment.
11+
# The settings below are to reduce costs during testing and are not appropriate
12+
# for production use. For more information, see:
13+
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
14+
manual_scaling:
15+
instances: 1
16+
resources:
17+
cpu: 1
18+
memory_gb: 0.5
19+
disk_size_gb: 10

profiler/appengine/flexible/main.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2019 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""An example of using https://cloud.google.com/profiler on GAE flex."""
15+
16+
from flask import Flask
17+
# [START profiler_python_appengine_flex]
18+
import googlecloudprofiler
19+
20+
# Profiler initialization. It starts a daemon thread which continuously
21+
# collects and uploads profiles. Best done as early as possible.
22+
try:
23+
# service and service_version can be automatically inferred when
24+
# running on App Engine. project_id must be set if not running
25+
# on GCP.
26+
googlecloudprofiler.start(verbose=3)
27+
except (ValueError, NotImplementedError) as exc:
28+
print(exc) # Handle errors here
29+
30+
# [END profiler_python_appengine_flex]
31+
32+
33+
app = Flask(__name__)
34+
35+
36+
@app.route('/')
37+
def hello():
38+
"""Return a friendly HTTP greeting."""
39+
return 'Hello World!'
40+
41+
42+
if __name__ == '__main__':
43+
# This is used when running locally. Gunicorn is used to run the
44+
# application on Google App Engine. See entrypoint in app.yaml.
45+
app.run(host='127.0.0.1', port=8080, debug=True)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2019 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import main
16+
17+
18+
def test_index():
19+
main.app.testing = True
20+
client = main.app.test_client()
21+
22+
r = client.get('/')
23+
assert r.status_code == 200
24+
assert 'Hello World' in r.data.decode('utf-8')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Flask==1.0.2
2+
gunicorn==19.9.0
3+
google-cloud-profiler
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
runtime: python37
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""An example of using https://cloud.google.com/profiler on GAE standard."""
15+
16+
from flask import Flask
17+
# [START profiler_python_appengine_standard_python37]
18+
import googlecloudprofiler
19+
20+
# Profiler initialization. It starts a daemon thread which continuously
21+
# collects and uploads profiles. Best done as early as possible.
22+
try:
23+
# service and service_version can be automatically inferred when
24+
# running on App Engine. project_id must be set if not running
25+
# on GCP.
26+
googlecloudprofiler.start(verbose=3)
27+
except (ValueError, NotImplementedError) as exc:
28+
print(exc) # Handle errors here
29+
30+
# [END profiler_python_appengine_standard_python37]
31+
32+
33+
# If `entrypoint` is not defined in app.yaml, App Engine will look for an app
34+
# called `app` in `main.py`.
35+
app = Flask(__name__)
36+
37+
38+
@app.route('/')
39+
def hello():
40+
"""Return a friendly HTTP greeting."""
41+
return 'Hello World!'
42+
43+
44+
if __name__ == '__main__':
45+
# This is used when running locally only. When deploying to Google App
46+
# Engine, a webserver process such as Gunicorn will serve the app. This
47+
# can be configured by adding an `entrypoint` to app.yaml.
48+
app.run(host='127.0.0.1', port=8080, debug=True)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2019 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import main
16+
17+
18+
def test_index():
19+
main.app.testing = True
20+
client = main.app.test_client()
21+
22+
r = client.get('/')
23+
assert r.status_code == 200
24+
assert 'Hello World' in r.data.decode('utf-8')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Flask==1.0.2
2+
google-cloud-profiler

profiler/quickstart/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def main():
3333
# project_id='my-project-id',
3434
)
3535
except (ValueError, NotImplementedError) as exc:
36-
print(exc) # Handles errors here
36+
print(exc) # Handle errors here
3737
# [END profiler_python_quickstart]
3838
busyloop()
3939

0 commit comments

Comments
 (0)