Each directory in this repo contains the necessary files for deploying a simple app that publishes some messages using Google Cloud Pub/Sub in a serverless app.
Directory | Description |
---|---|
cf_apiary |
A Cloud Function that publishes messages using the Cloud Pub/Sub Apiary client. |
cf_cl_mixed |
A Cloud Function that publishes messages using the Cloud Pub/Sub standard client. The first publish future is resolved in a callback function. The subsequent publish futures are resolved sequentially. |
cf_gapic |
A Cloud Function that publishes messages using the Cloud Pub/Sub GAPIC client. |
cr_apiary |
A Cloud Run app that publishes messages using the Cloud Pub/Sub Apiary client. |
cr_cl_mixed |
A Cloud Run app that publishes messages using the Cloud Pub/Sub standard client. The first publish future is resolved in a callback function. The subsequent publish futures are resolved sequentially. |
cr_gapic |
A Cloud Run app that publishes messages using the Cloud Pub/Sub GAPIC client. |
gae_apiary |
An App Engine app that publishes messages using the Cloud Pub/Sub Apiary client. |
gae_cl_mixed |
An App Engine app that publishes messages using the Cloud Pub/Sub standard client. The first publish future is resolved in a callback function. The subsequent publish futures are resolved sequentially. |
gae_gapic |
An App Engine app that publishes messages using the Cloud Pub/Sub GAPIC client. |
-
Install the Cloud SDK.
-
Enable the APIs: Stackdriver Logging, Stackdriver Trace, Cloud Pub/Sub, App Engine, Cloud Run, Cloud Functions, Cloud Build.
-
Setup the Cloud SDK to your GCP project.
gcloud init
-
Create a service account key as a JSON file. For more information, see Creating and managing service accounts.
-
From the Service account list, select New service account.
-
In the Service account name field, enter a name.
-
From the Role list, select Project > Owner.
Note: The Role field authorizes your service account to access resources. You can view and change this field later by using the GCP Console IAM page. If you are developing a production app, specify more granular permissions than Project > Owner. For more information, see Granting roles to service accounts.
-
Click Create. A JSON file that contains your key downloads to your computer.
-
-
Set your
GOOGLE_APPLICATION_CREDENTIALS
environment variable to point to your service account key file.export GOOGLE_APPLICATION_CREDENTIALS=path/to/your/credentials.json
-
Create some Pub/Sub topics for testing the three different types of publish clients.
gcloud pubsub topics create apiary glcoud pubsub topics create standard gcloud pubsub topics create gapic
-
Open
cf.*.py
and updatePROJECT_ID
with your project ID. -
Deploy the function.
gcloud functions deploy [your_function_name] \ --runtime python37 \ --trigger-http
-
Test publish latency.
curl -X POST "https://us-central1-[your-project-id].cloudfunctions.net/[your-function-name]" \ -H "Content-Type:application/json" \ --data '{"n":"100"}'
-
Deploy the app.
gcloud app deploy --version [your-version-name]
-
Test publish latency.
curl -X POST "https://[your-version-name]-dot-[your-project-id].appspot.com/" \ -H "Content-Type:application/json" \ --data '{"n":"100"}'
-
Open
Dockerfile
and updateyour-project-id
with your project ID. -
Build your container image.
gcloud builds submit --tag gcr.io/[your-project-id]/[your-image-name]
-
Deploy to Cloud Run and record the Service URL returned.
gcloud beta run deploy \ --image gcr.io/[your-project-id]/[your-image-name] \ --platform managed
-
Test publish latency.
curl -X POST "https://[your-image-name]-[your-hash-from-the-service-url].a.run.app/" \ -H "Content-Type:application/json" \ --data '{"n": "100"}'
To run the cURL commands repeatedly and record the end-to-end latencies, try:
for i in `seq 1 10`; do $your-curl-command -o /dev/null -w %{time_total}\\n >> output.txt; done;
To run the cURL commands in a parallel fashion, create a postfile
that specifies the value that n
should take, then use Apache Benchmark and do something like:
ab -n 10 -c 2 -s 1800 -T 'application/json' -p postfile [your-endpoint]
-
One way to see latency data is in the GCP Stackdriver Trace page.
-
The other way to latency data is in the GCP Stackdriver Logging page. Here, you will need to navigate to the appropriate logs. You can download the logs locally and parse the data for analysis.