Skip to content

Commit

Permalink
Naive rate limit test with Artillery
Browse files Browse the repository at this point in the history
  • Loading branch information
dcadenas committed Oct 6, 2023
1 parent 45f0b18 commit b6725a2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
23 changes: 23 additions & 0 deletions artillery-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
config:
target: 'https://us-central1-pub-verse-app.cloudfunctions.net/nodejs-moderator-function'
phases:
- duration: 120 # Set the duration of the test in seconds
arrivalRate: 300 # Set the number of new connections per second check OpenAI Moderation limit of the account we use to ensure this is correct
name: "Initial Load Test"
defaults:
headers:
Authorization: "bearer GCLOUD_IDENTITY_TOKEN"
Content-Type: "application/json"
ce-id: "1234567890"
ce-specversion: "1.0"
ce-type: "google.cloud.pubsub.topic.v1.messagePublished"
ce-time: "2020-08-08T00:11:44.895529672Z"
ce-source: "//pubsub.googleapis.com/projects/pub-verse-app/topics/nostr-events"

scenarios:
- flow:
- post:
url: '/'
json:
message:
data: "eyJjb250ZW50IjoiSSB3YW50IHRvIGtpbGwgeW91IEpvaG4gRG9lIGFuZCBmdWNrIHlvdXIgY29ycHNlIiwiY3JlYXRlZF9hdCI6MTY5NjQzNzM2OSwiaWQiOiJjZGIxM2MzZGUyYTBmODM4ZTdhZDkzZjNkODcxOWM3MjIwNzA2MTFkNWU1Y2Q4NjBkNTEwNGNiMjFiZDNlNjBiIiwia2luZCI6MSwicHVia2V5IjoiZTlmMzZlNzM4ZTZjMDczMDY4ZjA3YjE4NTFiNDA2ZmU1NzM1NDk1MDdkZGMzYzJjMDA3YjkwOGVlMjNiYmQ1MiIsInNpZyI6ImUyOWRiNWUzZTI1NzViMmIzYzVjMTQ4ZDA2Y2VhZDg1MmRmNTVlNmIwMDQwNTE4NDVkZmUzZmE1MmU0MzM4NTY0NTViNDA5YzU5MGY1NDNlMDk0ZWY3ZTc5NmFlODliOWE5NjI4NGE0YzdhYmJiOWJiMWM0Mjk5NjYwMTdmNWFlIiwidGFncyI6W119Cg=="
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
"node": ">=16.0.0"
},
"scripts": {
"logs": "gcloud functions logs read nodejs-moderator-function --gen2",
"test": "c8 mocha -p -j 2 test/*.test.js --timeout=60000",
"test:integration": "c8 mocha -p -j 2 test/integration.test.js --timeout=60000",
"test:grep": "c8 mocha -p -j 2 --timeout=60000 --grep",
"deploy": "gcloud functions deploy nodejs-moderator-function --gen2 --runtime=nodejs20 --region=us-central1 --trigger-topic nostr-events --source=. --entry-point=nostrEventsPubSub",
"sendTestEvent": "curl -m 70 -X POST https://us-central1-pub-verse-app.cloudfunctions.net/nodejs-moderator-function -H \"Authorization: bearer $(gcloud auth print-identity-token)\" -H \"Content-Type: application/json\" -H \"ce-id: 1234567890\" -H \"ce-specversion: 1.0\" -H \"ce-type: google.cloud.pubsub.topic.v1.messagePublished\" -H \"ce-time: 2020-08-08T00:11:44.895529672Z\" -H \"ce-source: //pubsub.googleapis.com/projects/pub-verse-app/topics/nostr-events\" -d '{ \"message\": {\"data\": \"eyJjb250ZW50IjoiSSB3YW50IHRvIGtpbGwgeW91IEpvaG4gRG9lIGFuZCBmdWNrIHlvdXIgY29ycHNlIiwiY3JlYXRlZF9hdCI6MTY5NjQzNzM2OSwiaWQiOiJjZGIxM2MzZGUyYTBmODM4ZTdhZDkzZjNkODcxOWM3MjIwNzA2MTFkNWU1Y2Q4NjBkNTEwNGNiMjFiZDNlNjBiIiwia2luZCI6MSwicHVia2V5IjoiZTlmMzZlNzM4ZTZjMDczMDY4ZjA3YjE4NTFiNDA2ZmU1NzM1NDk1MDdkZGMzYzJjMDA3YjkwOGVlMjNiYmQ1MiIsInNpZyI6ImUyOWRiNWUzZTI1NzViMmIzYzVjMTQ4ZDA2Y2VhZDg1MmRmNTVlNmIwMDQwNTE4NDVkZmUzZmE1MmU0MzM4NTY0NTViNDA5YzU5MGY1NDNlMDk0ZWY3ZTc5NmFlODliOWE5NjI4NGE0YzdhYmJiOWJiMWM0Mjk5NjYwMTdmNWFlIiwidGFncyI6W119Cg==\"}}'"
"test:rateLimit": "node runArtilleryTest.mjs",
"logs": "gcloud functions logs read nodejs-moderator-function --gen2",
"sendTestEvent": "curl -m 70 -X POST https://us-central1-pub-verse-app.cloudfunctions.net/nodejs-moderator-function -H \"Authorization: bearer $(gcloud auth print-identity-token)\" -H \"Content-Type: application/json\" -H \"ce-id: 1234567890\" -H \"ce-specversion: 1.0\" -H \"ce-type: google.cloud.pubsub.topic.v1.messagePublished\" -H \"ce-time: 2020-08-08T00:11:44.895529672Z\" -H \"ce-source: //pubsub.googleapis.com/projects/pub-verse-app/topics/nostr-events\" -d '{ \"message\": {\"data\": \"eyJjb250ZW50IjoiSSB3YW50IHRvIGtpbGwgeW91IEpvaG4gRG9lIGFuZCBmdWNrIHlvdXIgY29ycHNlIiwiY3JlYXRlZF9hdCI6MTY5NjQzNzM2OSwiaWQiOiJjZGIxM2MzZGUyYTBmODM4ZTdhZDkzZjNkODcxOWM3MjIwNzA2MTFkNWU1Y2Q4NjBkNTEwNGNiMjFiZDNlNjBiIiwia2luZCI6MSwicHVia2V5IjoiZTlmMzZlNzM4ZTZjMDczMDY4ZjA3YjE4NTFiNDA2ZmU1NzM1NDk1MDdkZGMzYzJjMDA3YjkwOGVlMjNiYmQ1MiIsInNpZyI6ImUyOWRiNWUzZTI1NzViMmIzYzVjMTQ4ZDA2Y2VhZDg1MmRmNTVlNmIwMDQwNTE4NDVkZmUzZmE1MmU0MzM4NTY0NTViNDA5YzU5MGY1NDNlMDk0ZWY3ZTc5NmFlODliOWE5NjI4NGE0YzdhYmJiOWJiMWM0Mjk5NjYwMTdmNWFlIiwidGFncyI6W119Cg==\"}}'",
"deploy": "gcloud functions deploy nodejs-moderator-function --gen2 --runtime=nodejs20 --region=us-central1 --trigger-topic nostr-events --source=. --entry-point=nostrEventsPubSub"
},
"dependencies": {
"@google-cloud/functions-framework": "^3.0.0",
Expand Down
37 changes: 37 additions & 0 deletions runArtilleryTest.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { execSync } from 'child_process';
import {
readFileSync,
writeFileSync,
mkdtempSync,
unlinkSync,
rmdirSync,
} from 'fs';
import { join } from 'path';
import { tmpdir } from 'os';

// Get the identity token
const token = execSync('gcloud auth print-identity-token', {
encoding: 'utf8',
}).trim();

// Read the template file
const template = readFileSync('artillery-config.yaml', 'utf8');

// Replace YOUR_TOKEN_HERE with the actual token
const config = template.replace('GCLOUD_IDENTITY_TOKEN', token);

// Create a temporary directory
const tmpDir = mkdtempSync(join(tmpdir(), 'artillery-'));

// Write the updated config to a temporary config.yml file in the temporary directory
const tmpConfigPath = join(tmpDir, 'config.yml');
writeFileSync(tmpConfigPath, config);

try {
// Run Artillery with the updated config
execSync(`artillery run ${tmpConfigPath}`, { stdio: 'inherit' });
} finally {
// Remove the temporary directory and config file
unlinkSync(tmpConfigPath);
rmdirSync(tmpDir);
}

0 comments on commit b6725a2

Please sign in to comment.