Analyze the performance of an AlloyDB instance with pgbench

Overview

In this tutorial, you integrate an AlloyDB instance and a Compute Engine VM instance on a Virtual Private Cloud (VPC) network. You then verify the integration by installing pgbench on the Compute Engine VM instance, and use this benchmarking tool to conduct performance tests against the AlloyDB instance.

Objectives

In this tutorial, you learn how to:

  • Create a VPC network for your AlloyDB instance and your Compute Engine VM instance so that communication can pass between the instance and the VM securely.
  • Create an AlloyDB instance and a Compute Engine VM instance, and assign and configure your VPC network for both instances.
  • Install pgbench on the Compute Engine VM instance, run it against the AlloyDB instance, and use it to conduct performance tests against the AlloyDB instance.

Costs

This tutorial uses billable components of Google Cloud, including VPC network, AlloyDB, and Compute Engine. Use the pricing calculator to generate a cost estimate based on your projected usage.

Before you begin

  1. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to the project selector page

  2. Make sure that billing is enabled for your Google Cloud project. Learn how to confirm that billing is enabled for your project.

  3. Enable the VPC network, AlloyDB, and Compute Engine APIs.

    Enable the APIs

  4. Make sure that you have the AlloyDB Admin role assigned to your user account.

    Go to the IAM page

Create a VPC network

In this section, you create a VPC network for your AlloyDB instance and your Compute Engine VM instance so that communication can pass between the instance and the VM securely.

  1. Go to the VPC networks page in the Google Cloud Console.

    Go to the VPC networks page

  2. Click CREATE VPC NETWORK. The Create a VPC network page appears.

  3. Enter my-vpc-network for the Name of your VPC network.

  4. In the Subnet creation mode section, select the Custom option.

  5. Click ADD SUBNET.

  6. Enter my-subnet for the Name of your subnet.

  7. Select a Region and enter an IP address range for your subnet.

  8. Click DONE.

  9. Click CREATE.

Create an AlloyDB instance

In this section, you create an AlloyDB instance, and assign and configure your VPC network for the instance.

  1. Go to the AlloyDB Instances page in the Google Cloud Console.

    Go to the AlloyDB Instances page

  2. Click CREATE INSTANCE.

  3. In the Instance ID field, enter a unique identifier for the AlloyDB instance that you're creating. For this tutorial, enter my-instance in the field.

  4. In the Password field, either enter the password for your AlloyDB instance or click GENERATE to have AlloyDB create a password for you.

  5. From the Network menu, select my-vpc-network. You created this VPC network in Create a VPC network.

  6. Because your VPC network hasn't yet initialized private service access, click SET UP CONNECTION.

  7. In the Create a private service connection pane that appears:

    1. Select the Use an automatically allocated IP range option to have Google Cloud select an automatically allocated IP range in your network.

    2. Click CONTINUE.

    3. Click CREATE CONNECTION.

  8. Click CREATE INSTANCE.

  9. Make a note of the private IP address of your AlloyDB instance that appears on the AlloyDB Instances page. You'll need this address to verify the integration between the AlloyDB instance and the Compute Engine VM instance.

Create a Compute Engine VM instance

In this section, you create a Compute Engine VM instance, and assign and configure your VPC network for the instance.

  1. Go to the VM instances page in the Google Cloud Console.

    Go to the VM instances page

  2. Click Create instance.

  3. In the Name field of the Create an instance page, enter a unique identifier for the Compute Engine VM instance that you're creating. For this tutorial, enter my-compute-engine-vm-instance in the field.

  4. In the Access scopes section of the page, select the Allow full access to all Cloud APIs option.

  5. Expand the Management, security, disks, networking, sole tenancy node, and then complete the following steps:

    1. Click the Networking subtab.

    2. Click Add network interface.

    3. From the Network menu, select my-vpc-network. You created this VPC network in Create a VPC network.

    4. Click Done. Two VPC networks appear in the Network interfaces section of the page: default and my-vpc-network

  6. Hover your mouse over the default VPC network.

  7. Click the Delete icon that appears. This icon resembles a trashcan.

  8. Click Create.

Verify the integration

In this section, you install pgbench on the Compute Engine VM instance, run it against the AlloyDB instance, and use it to conduct performance tests against the AlloyDB instance.

  1. Go to the AlloyDB Instances page in the Google Cloud Console.

    Go to the AlloyDB Instances page

  2. Connect to your Compute Engine VM instance. To do so, click the Connect menu to the right of your VM instance, and then select Open in browser window.

  3. Install the Contributed Extensions and Additions to PostgreSQL package on your Compute Engine VM instance.

    sudo apt-get -y install postgresql-contrib
  4. Initialize a database for pgbench on your AlloyDB for PostgreSQL instance. For example, use the default postgres database:

    pgbench -i --host=INSTANCE_PRIVATE_IP --port=PORT_NUMBER --username=USERNAME

    Replace the following:

    • INSTANCE_PRIVATE_IP: The private IP address of your AlloyDB instance. You made a note of this IP address when you created the instance.
    • PORT_NUMBER: The port number reserved for your AlloyDB instance. For this tutorial, the port number is 5432.
    • USERNAME: The administrator username for your AlloyDB instance. For this tutorial, the username is postgres.
  5. At the Password prompt, enter the password for your AlloyDB instance. When you created the instance, you either supplied a password for it or had AlloyDB generate a password for you.

  6. Run pgbench with no arguments to verify basic functionality.

    pgbench --host=INSTANCE_PRIVATE_IP --port=PORT_NUMBER --username=USERNAME
  7. At the Password prompt, enter the password for your AlloyDB instance.

    You should see output similar to the following:

    starting vacuum...end.
    transaction type: <builtin: TPC-B (sort of)>
    scaling factor: 1
    query mode: simple
    number of clients: 1
    number of threads: 1
    number of transactions per client: 10
    number of transactions actually processed: 10/10
    latency average = 3.794 ms
    tps = 263.560171 (including connections establishing)
    tps = 345.375423 (excluding connections establishing)
    
  8. Use some options to make a useful test. Some important options are -c (the number of clients) and --transactions (the number of transactions per client).

    For this tutorial, conduct a performance test against 10 clients that are accessing your AlloyDB for PostgreSQL database and 100 transactions for each client.

    pgbench --host=INSTANCE_PRIVATE_IP --port=PORT_NUMBER --username=USERNAME -c 10 --transactions=100
  9. At the Password prompt, enter the password for your AlloyDB instance.

    You should see output similar to the following:

    starting vacuum...end.
    transaction type: 
    scaling factor: 1
    query mode: simple
    number of clients: 10
    number of threads: 1
    number of transactions per client: 100
    number of transactions actually processed: 1000/1000
    latency average = 36.421 ms
    tps = 274.563444 (including connections establishing)
    tps = 275.733225 (excluding connections establishing)
    

Clean up

After you've finished this tutorial, you can clean up the resources that you created so they won't take up quota and you won't be billed for them in the future.

To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, follow these steps.

  1. Use the Google Cloud console to delete your project, AlloyDB instance, Compute Engine VM instance, and VPC network.

The following sections describe how to delete or turn off these resources.

Delete your project

The easiest way to eliminate billing is to delete the project that you created for this tutorial.

  1. Go to the Manage resources page in the Google Cloud Console.

    Go to the Manage resources page

  2. In the project list, select the project that you want to delete, and then click Delete.

  3. In the dialog box, type the project ID, and then click Shut down to delete the project.

Delete the AlloyDB instance

  1. Go to the AlloyDB Instances page in the Google Cloud Console.

    Go to the AlloyDB Instances page

  2. Click the name of your AlloyDB instance. For this tutorial, click my-instance.

  3. Click DELETE.

  4. In the dialog box, enter the name of your AlloyDB instance in the text field, and then click DELETE.

Delete the Compute Engine VM instance

  1. Go to the VM instances page in the Google Cloud Console.

    Go to the VM instances page

  2. Click the name of your Compute Engine VM instance. For this tutorial, click my-compute-engine-vm-instance.

  3. Click the Delete icon. This icon resembles a trashcan.

  4. In the dialog box, click DELETE.

Delete the VPC network

  1. Go to the VPC networks page in the Google Cloud Console.

    Go to the VPC networks page

  2. Click the name of your VPC network. For this tutorial, click my-vpc-network.

  3. Click DELETE VPC NETWORK.

  4. In the dialog box, click DELETE.