-
Notifications
You must be signed in to change notification settings - Fork 607
chore: k6 benchmarks #3675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
chore: k6 benchmarks #3675
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # API Benchmark Tools | ||
|
|
||
| This directory contains k6 load testing scripts for comparing v1 and v2 Unkey APIs. | ||
|
|
||
| ## Scripts | ||
|
|
||
| ### ratelimit.js - Ratelimit API Benchmark | ||
| Tests the ratelimiting endpoints with random identifiers. | ||
|
|
||
| ```bash | ||
| UNKEY_ROOT_KEY=your_key REGION=us-east k6 cloud ratelimit.js | ||
| ``` | ||
|
|
||
| **Features:** | ||
| - 50/50 split between v1 (`api.unkey.dev`) and v2 (`api.unkey.com`) APIs | ||
| - Random user identifiers from pool of 5 users | ||
| - Tests `/v1/ratelimits.limit` vs `/v2/ratelimit.limit` | ||
|
|
||
| ### keyverify.js - Key Verification Benchmark | ||
| Tests the key verification endpoints. | ||
|
|
||
| ```bash | ||
| UNKEY_ROOT_KEY=your_key KEY=your_test_key k6 cloud keyverify.js | ||
| ``` | ||
|
|
||
| **Features:** | ||
| - 50/50 split between v1 and v2 APIs | ||
| - Tests `/v1/keys.verifyKey` vs `/v2/keys.verifyKey` | ||
|
|
||
| ## Setup | ||
| 1. Install k6: `brew install k6` | ||
| 2. Set up k6 Cloud account and configure project ID in scripts | ||
| 3. Configure environment variables | ||
|
|
||
| ## Environment Variables | ||
| - `UNKEY_ROOT_KEY` - Required. Your Unkey root API key | ||
| - `KEY` - Required for keyverify.js. Test key to verify | ||
|
|
||
| ## Configuration | ||
| - **Duration:** 10 minutes at 10 req/s | ||
| - **Load zones:** Currently US East only (others commented out) | ||
| - **Thresholds:** P95 < 500ms, 99% success rate | ||
| - **Project ID:** 3788521 (update as needed) | ||
|
|
||
| ## Metrics | ||
| - `request_latency` - Custom trend metric with URL and region tags | ||
| - Built-in k6 HTTP metrics (duration, success rate, etc.) | ||
| - All metrics automatically available in Grafana Cloud | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import http from 'k6/http'; | ||
| import { check } from 'k6'; | ||
| import { Rate, Trend } from 'k6/metrics'; | ||
chronark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| // Custom metrics | ||
| const requestLatencyTrend = new Trend('request_latency', true); | ||
|
|
||
| const loadZones = [ | ||
| 'amazon:us:ashburn', // US East | ||
| // 'amazon:us:portland', // US West | ||
| // 'amazon:ie:dublin', // Europe West | ||
| // 'amazon:de:frankfurt', // Europe Central | ||
| // 'amazon:sg:singapore', // Asia Pacific | ||
| // 'amazon:jp:tokyo', // Asia Pacific East | ||
| // 'amazon:au:sydney', // Australia | ||
| // 'amazon:br:sao paulo', // South America | ||
| // 'amazon:in:mumbai', // India | ||
| // 'amazon:ca:montreal' // Canada | ||
| ]; | ||
|
|
||
| const equalPercent = Math.floor(100 / loadZones.length); | ||
| const distribution = {}; | ||
| loadZones.forEach((zone, index) => { | ||
| distribution[zone] = { | ||
| loadZone: zone, | ||
| percent: index === loadZones.length - 1 ? 100 - (equalPercent * (loadZones.length - 1)) : equalPercent | ||
| }; | ||
| }); | ||
|
|
||
| export const options = { | ||
| cloud: { | ||
| project: "3788521", | ||
| distribution: distribution | ||
| }, | ||
| stages: [ | ||
| { duration: '10m', target: 10 }, // 10 req/s for 1 minute | ||
chronark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ], | ||
| thresholds: { | ||
| http_req_duration: ['p(95)<500'], // 95% of requests must complete below 500ms | ||
| checks: ['rate>0.99'], // 99% of checks must pass | ||
| }, | ||
| }; | ||
|
|
||
| const UNKEY_ROOT_KEY = __ENV.UNKEY_ROOT_KEY; | ||
| const KEY = __ENV.KEY; | ||
|
|
||
| if (!UNKEY_ROOT_KEY) { | ||
| throw new Error('UNKEY_ROOT_KEY environment variable is required'); | ||
| } | ||
|
|
||
| if (!KEY) { | ||
| throw new Error('KEY environment variable is required'); | ||
| } | ||
|
|
||
| const headers = { | ||
| 'Content-Type': 'application/json', | ||
| 'Authorization': `Bearer ${UNKEY_ROOT_KEY}`, | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| export default function () { | ||
|
|
||
|
|
||
|
|
||
| const response = Math.random() < 0.5 | ||
| ? http.post('https://api.unkey.dev/v1/keys.verifyKey', JSON.stringify({ | ||
| key: KEY, | ||
| }), { | ||
| headers: headers, | ||
| tags: { version: 'v1' }, | ||
| }) | ||
| : http.post('https://api.unkey.com/v2/keys.verifyKey', JSON.stringify({ | ||
| key: KEY | ||
| }), { | ||
| headers: headers, | ||
| tags: { version: 'v2' }, | ||
| }); | ||
|
|
||
| check(response, { | ||
| 'status is 200': (r) => r.status === 200, | ||
| }); | ||
chronark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| requestLatencyTrend.add(response.timings.duration, { url: response.request.url }); | ||
| } | ||
chronark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import http from 'k6/http'; | ||
| import { check } from 'k6'; | ||
| import { Rate, Trend } from 'k6/metrics'; | ||
chronark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| // Custom metrics | ||
| const requestLatencyTrend = new Trend('request_latency', true); | ||
|
|
||
| const loadZones = [ | ||
| 'amazon:us:ashburn', // US East | ||
| // 'amazon:us:portland', // US West | ||
| // 'amazon:ie:dublin', // Europe West | ||
| // 'amazon:de:frankfurt', // Europe Central | ||
| // 'amazon:sg:singapore', // Asia Pacific | ||
| // 'amazon:jp:tokyo', // Asia Pacific East | ||
| // 'amazon:au:sydney', // Australia | ||
| // 'amazon:br:sao paulo', // South America | ||
| // 'amazon:in:mumbai', // India | ||
| // 'amazon:ca:montreal' // Canada | ||
| ]; | ||
|
|
||
| const equalPercent = Math.floor(100 / loadZones.length); | ||
| const distribution = {}; | ||
| loadZones.forEach((zone, index) => { | ||
| distribution[zone] = { | ||
| loadZone: zone, | ||
| percent: index === loadZones.length - 1 ? 100 - (equalPercent * (loadZones.length - 1)) : equalPercent | ||
| }; | ||
| }); | ||
|
|
||
| export const options = { | ||
| cloud: { | ||
| project: "3788521", | ||
| distribution: distribution | ||
| }, | ||
| stages: [ | ||
| { duration: '10m', target: 10 }, // 10 req/s for 1 minute | ||
chronark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ], | ||
| thresholds: { | ||
| http_req_duration: ['p(95)<500'], // 95% of requests must complete below 500ms | ||
| checks: ['rate>0.99'], // 99% of checks must pass | ||
| }, | ||
| }; | ||
|
|
||
| const UNKEY_ROOT_KEY = __ENV.UNKEY_ROOT_KEY; | ||
| const REGION = __ENV.REGION || 'local'; | ||
|
|
||
| if (!UNKEY_ROOT_KEY) { | ||
| throw new Error('UNKEY_ROOT_KEY environment variable is required'); | ||
| } | ||
|
|
||
| const headers = { | ||
| 'Content-Type': 'application/json', | ||
| 'Authorization': `Bearer ${UNKEY_ROOT_KEY}`, | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| const identifiers = ['user1', 'user2', 'user3', 'user4', 'user5'] | ||
| export default function () { | ||
| // Randomly choose between v1 and v2 (50/50 split) | ||
|
|
||
| const identifier = identifiers[Math.floor(Math.random() * identifiers.length)]; | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| const response = Math.random() < 0.5 | ||
| ? http.post('https://api.unkey.dev/v1/ratelimits.limit', JSON.stringify({ | ||
| namespace: 'benchmark', | ||
| identifier, | ||
| limit: 100, | ||
| duration: 60000 | ||
| }), { | ||
| headers: headers, | ||
| tags: { version: 'v1', region: REGION }, | ||
| }) | ||
| : http.post('https://api.unkey.com/v2/ratelimit.limit', JSON.stringify({ | ||
| namespace: 'benchmark', | ||
| identifier, | ||
| limit: 100, | ||
| duration: 60000 | ||
| }), { | ||
| headers: headers, | ||
| tags: { version: 'v2', region: REGION }, | ||
| }); | ||
|
|
||
| check(response, { | ||
| 'status is 200': (r) => r.status === 200, | ||
| }); | ||
chronark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| requestLatencyTrend.add(response.timings.duration, { url: response.request.url, region: REGION }); | ||
| } | ||
chronark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.