From 2e25fbef62808b49b810fb9bfb8a8a272eef2995 Mon Sep 17 00:00:00 2001 From: wphan Date: Wed, 5 Jul 2023 16:11:55 -0700 Subject: [PATCH] add k6 test whitelist switch, allow elb health check --- k6-tests/l2AndBatchL2Load.ts | 28 ++++++++++++++++++++++++++++ package.json | 2 ++ src/index.ts | 11 ++++++++++- yarn.lock | 10 ++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 k6-tests/l2AndBatchL2Load.ts diff --git a/k6-tests/l2AndBatchL2Load.ts b/k6-tests/l2AndBatchL2Load.ts new file mode 100644 index 0000000..e22a940 --- /dev/null +++ b/k6-tests/l2AndBatchL2Load.ts @@ -0,0 +1,28 @@ +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + vus: 555, + duration: '30s', + ext: { + loadimpact: { + projectID: 3648966, + name: 'L2 and BatchL2 tests' + } + } +}; + +const l2Url = 'https://mainnet-beta.api.drift.trade/dlob/l2?marketIndex=0&marketType=perp&depth=20&grouping=100&includeVamm=true&includePhoenix=false&includeSerum=false'; +const batchL2Url = 'https://mainnet-beta.api.drift.trade/dlob/batchL2?marketType=perp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cspot%2Cspot%2Cspot&marketIndex=0%2C1%2C2%2C3%2C4%2C5%2C6%2C7%2C8%2C9%2C10%2C11%2C12%2C0%2C1%2C2&depth=100%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5&includeVamm=true%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Cfalse%2Cfalse%2Cfalse&includePhoenix=false%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Ctrue%2Ctrue%2Ctrue&includeSerum=false%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Ctrue%2Ctrue%2Ctrue'; +// const l2Url = 'http://localhost:6969/l2?marketIndex=0&marketType=perp&depth=20&grouping=100&includeVamm=true&includePhoenix=false&includeSerum=false'; +// const batchL2Url = 'http://localhost:6969/batchL2?marketType=perp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cperp%2Cspot%2Cspot%2Cspot&marketIndex=0%2C1%2C2%2C3%2C4%2C5%2C6%2C7%2C8%2C9%2C10%2C11%2C12%2C0%2C1%2C2&depth=100%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5%2C5&includeVamm=true%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Ctrue%2Cfalse%2Cfalse%2Cfalse&includePhoenix=false%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Ctrue%2Ctrue%2Ctrue&includeSerum=false%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Cfalse%2Ctrue%2Ctrue%2Ctrue'; + +export default function() { + for (let i = 0; i < 10; i += 1) { + http.get(l2Url); + if (i % 2 === 0) { + http.get(batchL2Url); + } + sleep(1); + } +} \ No newline at end of file diff --git a/package.json b/package.json index c702356..e57cad7 100644 --- a/package.json +++ b/package.json @@ -26,12 +26,14 @@ "winston": "^3.8.1" }, "devDependencies": { + "@types/k6": "^0.45.0", "@typescript-eslint/eslint-plugin": "^4.28.0", "@typescript-eslint/parser": "^4.28.0", "eslint": "^7.29.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-prettier": "^3.4.0", "husky": "^7.0.4", + "k6": "^0.0.0", "prettier": "^2.4.1", "ts-node": "^10.9.1" }, diff --git a/src/index.ts b/src/index.ts index bfcc8b3..fb6f838 100644 --- a/src/index.ts +++ b/src/index.ts @@ -67,6 +67,8 @@ const rateLimitCallsPerSecond = process.env.RATE_LIMIT_CALLS_PER_SECOND ? parseInt(process.env.RATE_LIMIT_CALLS_PER_SECOND) : 10; +const loadTestAllowed = process.env.LOAD_TEST_ALLOWED.toLowerCase() === 'true' ? true : false; + const logFormat = ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent" :req[x-forwarded-for]'; const logHttp = morgan(logFormat, { @@ -90,6 +92,13 @@ app.use( max: rateLimitCallsPerSecond, standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers legacyHeaders: false, // Disable the `X-RateLimit-*` headers + skip: (req, _res) => { + if (!loadTestAllowed) { + return false; + } + + return req.headers['user-agent'].includes('k6'); + } }) ); @@ -376,7 +385,7 @@ const main = async () => { const handleHealthCheck = async (req, res, next) => { try { - if (req.url === '/health') { + if (req.url === '/health' || req.url === '/') { if (opts.testLiveness) { if (Date.now() > startupTime + 60 * 1000) { healthStatus = HEALTH_STATUS.LivenessTesting; diff --git a/yarn.lock b/yarn.lock index a1ea8ca..e605a8d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1186,6 +1186,11 @@ resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3" integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ== +"@types/k6@^0.45.0": + version "0.45.0" + resolved "https://registry.yarnpkg.com/@types/k6/-/k6-0.45.0.tgz#0d678cdbbb2d54fd057296835a11b36b3be6ab96" + integrity sha512-ySQ1LQ67kqYz/4W6oJ/9afPWHV/2GYKVUQFkOzM+fBtY2POCY8D7GX4F6rT0R8u2Be/DK8s1Ypr2s+LbToj/oA== + "@types/keygrip@*": version "1.0.2" resolved "https://registry.yarnpkg.com/@types/keygrip/-/keygrip-1.0.2.tgz#513abfd256d7ad0bf1ee1873606317b33b1b2a72" @@ -2824,6 +2829,11 @@ jsonparse@^1.2.0: resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280" integrity sha512-POQXvpdL69+CluYsillJ7SUhKvytYjW9vG/GKpnf+xP8UWgYEM/RaMzHHofbALDiKbbP1W8UEYmgGl39WkPZsg== +k6@^0.0.0: + version "0.0.0" + resolved "https://registry.yarnpkg.com/k6/-/k6-0.0.0.tgz#8c923200be0a68c578e8f5a32be96b1d8065cc3b" + integrity sha512-GAQSWayS2+LjbH5bkRi+pMPYyP1JSp7o+4j58ANZ762N/RH/SdlAT3CHHztnn8s/xgg8kYNM24Gd2IPo9b5W+g== + kuler@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/kuler/-/kuler-2.0.0.tgz#e2c570a3800388fb44407e851531c1d670b061b3"