|
| 1 | +# Cloud Spanner CDC Load Test |
| 2 | + |
| 3 | +A Java-based load generator for Cloud Spanner, designed to test Change Data Capture (CDC) and general database performance. It supports various strategies to simulate different types of load. |
| 4 | + |
| 5 | +## Strategies |
| 6 | + |
| 7 | +| Strategy | Description | |
| 8 | +| :--- | :--- | |
| 9 | +| **RANDOM** | Standard Insert/Update/Delete on random IDs. | |
| 10 | +| **SEQUENTIAL** | Strict sequence of Insert -> 10 Updates -> Delete on a unique ID. High churn per ID. | |
| 11 | +| **HOTSPOT** | High contention on a single shared row (Read-Write Transaction). | |
| 12 | +| **ATOMICITY** | Simulates money transfer between two shared rows (Read-Write Transaction). | |
| 13 | +| **SATURATION** | Large batch inserts (~1MB per op) to saturate network/CPU. | |
| 14 | +| **INTEGRITY** | Rapid Insert/Delete cycles on a fixed pool of keys (Resurrection testing). | |
| 15 | +| **READ_HEAVY** | 90% Read (point lookup), 10% Insert. Maintains local cache of 10k recent keys. | |
| 16 | +| **MIXED** | Cycles through all the above strategies across workers. | |
| 17 | + |
| 18 | +## Local Usage |
| 19 | + |
| 20 | +### Prerequisites |
| 21 | +- Java 17+ |
| 22 | +- Maven 3.8+ |
| 23 | +- Google Cloud SDK (`gcloud`) |
| 24 | + |
| 25 | +### Build |
| 26 | +```bash |
| 27 | +mvn clean package -DskipTests |
| 28 | +``` |
| 29 | + |
| 30 | +### Run Locally |
| 31 | +```bash |
| 32 | +java -jar target/spanner-cdc-loadtest-1.0-SNAPSHOT.jar \ |
| 33 | + -p <PROJECT_ID> \ |
| 34 | + -i <INSTANCE_ID> \ |
| 35 | + -d <DATABASE_ID> \ |
| 36 | + -c 10 \ |
| 37 | + -s MIXED \ |
| 38 | + -c 10 \ |
| 39 | + -s MIXED \ |
| 40 | + --duration 60 \ |
| 41 | + --create-schema |
| 42 | +``` |
| 43 | +*Note: Schema creation is disabled by default. Use `--create-schema` to enable it.* |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## Cloud Run Jobs Deployment |
| 48 | + |
| 49 | +Run massive load tests serverlessly using Cloud Run Jobs. |
| 50 | + |
| 51 | +### 1. Variables |
| 52 | +Set your environment variables: |
| 53 | +```bash |
| 54 | +export PROJECT_ID="your-project-id" |
| 55 | +export REGION="us-central1" |
| 56 | +# Use Artifact Registry (pkg.dev), NOT gcr.io |
| 57 | +export REPO_NAME="spanner-loadtest-repo" |
| 58 | +export IMAGE_NAME="${REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/spanner-loadtest:latest" |
| 59 | +export SPANNER_INSTANCE="your-instance-id" |
| 60 | +export SPANNER_DATABASE="your-database-id" |
| 61 | +``` |
| 62 | + |
| 63 | +### 2. Initial Setup (One-time) |
| 64 | +Create the Artifact Registry repository: |
| 65 | +```bash |
| 66 | +gcloud artifacts repositories create ${REPO_NAME} \ |
| 67 | + --repository-format=docker \ |
| 68 | + --location=${REGION} \ |
| 69 | + --description="Docker repository for Spanner Load Test" |
| 70 | +``` |
| 71 | + |
| 72 | +### 3. Build and Push |
| 73 | +Build the container image using Cloud Build: |
| 74 | +```bash |
| 75 | +gcloud builds submit --tag ${IMAGE_NAME} |
| 76 | +``` |
| 77 | + |
| 78 | +### 4. Create the Job |
| 79 | +Create the Cloud Run Job. Note the `--args` syntax requires a single comma-separated string. |
| 80 | + |
| 81 | +```bash |
| 82 | +gcloud run jobs create spanner-loadtest \ |
| 83 | + --image ${IMAGE_NAME} \ |
| 84 | + --region ${REGION} \ |
| 85 | + --cpu=4 \ |
| 86 | + --memory=8Gi \ |
| 87 | + --memory=8Gi \ |
| 88 | + --task-timeout=1h \ |
| 89 | + --args="-p,${PROJECT_ID},-i,${SPANNER_INSTANCE},-d,${SPANNER_DATABASE},-c,5,-s,MIXED,--duration,60" |
| 90 | +``` |
| 91 | +*Note: Ensure `--task-timeout` is greater than your load test `--duration`.* |
| 92 | + |
| 93 | +### 5. Execute the Load Test |
| 94 | +Run the job. You can override arguments (e.g., to increase concurrency or change strategy) using the `--args` flag. |
| 95 | + |
| 96 | +**Example: Run with 200 Total Threads** |
| 97 | +(50 threads per task * 4 tasks) |
| 98 | + |
| 99 | +```bash |
| 100 | +gcloud run jobs execute spanner-loadtest \ |
| 101 | + --region ${REGION} \ |
| 102 | + --tasks 4 \ |
| 103 | + --task-timeout=30m \ |
| 104 | + --args="-p,${PROJECT_ID},-i,${SPANNER_INSTANCE},-d,${SPANNER_DATABASE},-c,50,-s,HOTSPOT,--duration,300" |
| 105 | +``` |
| 106 | + |
| 107 | +### 6. Update Configuration (Resources) |
| 108 | +To change the CPU or Memory limits for an existing job: |
| 109 | +```bash |
| 110 | +gcloud run jobs update spanner-loadtest \ |
| 111 | + --cpu=8 \ |
| 112 | + --memory=16Gi \ |
| 113 | + --region=${REGION} |
| 114 | + --region=${REGION} |
| 115 | +``` |
| 116 | +**Important:** These resources (8 CPU, 16Gi Memory) are allocated **PER TASK**. |
| 117 | +If you execute this job with `--tasks 10`, you will provision **10 separate containers**, each with 8 vCPUs (Total: 80 vCPUs). |
| 118 | +Rates are billed per vCPU-second and GB-second for each task. |
| 119 | + |
| 120 | +### Troubleshooting |
| 121 | +- **Error: `None of [grpclb] specified`**: The shaded JAR is missing `ServicesResourceTransformer`. Ensure it's in `pom.xml` and rebuild. |
| 122 | +- **Error: `denied: gcr.io repo does not exist`**: Use Artifact Registry (`pkg.dev`) as shown above. |
| 123 | + |
| 124 | + |
| 125 | +# End-to-End |
| 126 | + |
| 127 | +## Variables |
| 128 | +``` |
| 129 | +export PROJECT_ID="your-project-id" |
| 130 | +export REGION="us-central1" |
| 131 | +export IMAGE_NAME="gcr.io/${PROJECT_ID}/spanner-loadtest:latest" |
| 132 | +
|
| 133 | +export SPANNER_INSTANCE="your-instance-id" |
| 134 | +export SPANNER_DATABASE="your-database-id" |
| 135 | +``` |
| 136 | + |
| 137 | +## Initial setup |
| 138 | + |
| 139 | +``` |
| 140 | +gcloud artifacts repositories create spanner-loadtest-repo \ |
| 141 | + --repository-format=docker \ |
| 142 | + --location=${REGION} \ |
| 143 | + --description="Docker repository for Spanner Load Test" |
| 144 | +
|
| 145 | +export IMAGE_NAME="${REGION}-docker.pkg.dev/${PROJECT_ID}/spanner-loadtest-repo/spanner-loadtest:latest" |
| 146 | +
|
| 147 | +gcloud builds submit --tag ${IMAGE_NAME} |
| 148 | +
|
| 149 | +gcloud run jobs create spanner-loadtest \ |
| 150 | + --image ${IMAGE_NAME} \ |
| 151 | + --region ${REGION} \ |
| 152 | + --cpu=4 \ |
| 153 | + --memory=8Gi \ |
| 154 | + --task-timeout=1h \ |
| 155 | + --args="-p,${PROJECT_ID},-i,${SPANNER_INSTANCE},-d,${SPANNER_DATABASE},-c,4,-s,MIXED,--duration,600" |
| 156 | +
|
| 157 | +gcloud run jobs execute spanner-loadtest --region ${REGION} --tasks 50 |
| 158 | +``` |
| 159 | + |
| 160 | +## Rebuild after changes |
| 161 | +``` |
| 162 | +gcloud builds submit --tag ${IMAGE_NAME} |
| 163 | +gcloud run jobs execute spanner-loadtest --region ${REGION} --tasks 2 |
| 164 | +``` |
0 commit comments