Skip to content

Instantly share code, notes, and snippets.

@dphang
Last active October 2, 2020 06:53
Show Gist options
  • Save dphang/2d84a27d8c0e4613b3eaa46b50b14257 to your computer and use it in GitHub Desktop.
Save dphang/2d84a27d8c0e4613b3eaa46b50b14257 to your computer and use it in GitHub Desktop.
// Return from Lambda
return {
status: "200",
statusDescription: "OK",
headers: {
"content-type": [
{
key: "Content-Type",
value: "text/html"
}
]
},
body: require("fs").readFileSync(`./pages${request.uri}`, "utf8")
};
// Return from direct S3 call (not using origin)
const { S3Client } = await import("@aws-sdk/client-s3/S3Client");
const s3 = new S3Client({ region: request.origin?.s3?.region });
console.log("BucketName: " + bucketName);
console.log("S3origin path: " + s3Origin.path);
console.log("URI: " + request.uri);
const s3Params = {
Bucket: bucketName,
Key: `${s3Origin.path.replace("/", "")}${request.uri}` // This is the page e.g static-pages/ssg-page.html
};
const { GetObjectCommand } = await import(
"@aws-sdk/client-s3/commands/GetObjectCommand"
);
const { Body } = await s3.send(new GetObjectCommand(s3Params));
const getStream = await import("get-stream");
const bodyString = await getStream.default(Body as Readable);
return {
status: "200",
statusDescription: "OK",
headers: {
"content-type": [
{
key: "Content-Type",
value: "text/html"
}
]
},
body: bodyString
};
import http from "k6/http";
import { check, sleep } from "k6";
import { Rate } from "k6/metrics";
let failureRate = new Rate("check_failure_rate");
export let options = {
stages: [
// Linearly ramp up from 1 to 50 VUs during first minute
{ target: 50, duration: "1m" },
// Hold at 50 VUs for the next 3 minutes and 30 seconds
{ target: 50, duration: "3m30s" },
// // Linearly ramp down from 50 to 0 50 VUs over the last 30 seconds
{ target: 0, duration: "30s" }
// Total execution time will be ~5 minutes
],
thresholds: {
// Check HTTP request duration is less than the following thresholds
// Low thresholds since initial page is served by CloudFront cache
http_req_duration: [],
// Thresholds based on the custom metric we defined and use to track application failures
check_failure_rate: [
// Global failure rate should be less than 1%
"rate<0.01",
// Abort the test early if it climbs over 5%
{ threshold: "rate<=0.05", abortOnFail: true }
]
}
};
const BASE_URL =
__ENV.K6_BASE_URL || "https://d33grmfi56ge3u.cloudfront.net/ssg-page";
export function setup() {
}
export default function () {
let response = http.get(BASE_URL);
// check() returns false if any of the specified conditions fail
let checkRes = check(response, {
"status is 200": (r) => r.status === 200,
"content is present": (r) => r.body !== {}
});
failureRate.add(!checkRes);
sleep(Math.random() * 3 + 2); // Random sleep between 2s and 5s
}
current (s3 origin + origin request handler + origin response handler)
execution: local
output: -
script: index-test.js
duration: -, iterations: -
vus: 1, max: 50
done [==========================================================] 5m0s / 5m0s
✓ status is 200
✓ content is present
✓ check_failure_rate.........: 0.00% ✓ 0 ✗ 3381
checks.....................: 100.00% ✓ 6764 ✗ 0
data_received..............: 9.1 MB 30 kB/s
data_sent..................: 268 kB 892 B/s
http_req_blocked...........: avg=706.16µs min=0s med=0s max=333.4ms p(90)=1µs p(95)=1µs
http_req_connecting........: avg=324.58µs min=0s med=0s max=193.56ms p(90)=0s p(95)=0s
http_req_duration..........: avg=316.69ms min=110.24ms med=351.43ms max=1.76s p(90)=433.84ms p(95)=564.84ms
http_req_receiving.........: avg=338.28µs min=34µs med=81µs max=219.87ms p(90)=110µs p(95)=124µs
http_req_sending...........: avg=57.91µs min=22µs med=56µs max=286µs p(90)=72µs p(95)=83.94µs
http_req_tls_handshaking...: avg=358.88µs min=0s med=0s max=260.5ms p(90)=0s p(95)=0s
http_req_waiting...........: avg=316.3ms min=110.02ms med=351.23ms max=1.76s p(90)=433.44ms p(95)=559.67ms
http_reqs..................: 3382 11.273326/s
iteration_duration.........: avg=3.78s min=719.9ms med=3.77s max=6.32s p(90)=5s p(95)=5.17s
iterations.................: 3341 11.136659/s
vus........................: 1 min=1 max=50
vus_max....................: 50 min=50 max=50
c1: remove origin response handler
execution: local
output: -
script: index-test.js
duration: -, iterations: -
vus: 1, max: 50
done [==========================================================] 5m0s / 5m0s
✓ status is 200
✓ content is present
✓ check_failure_rate.........: 0.00% ✓ 0 ✗ 3435
checks.....................: 100.00% ✓ 6872 ✗ 0
data_received..............: 9.2 MB 31 kB/s
data_sent..................: 271 kB 904 B/s
http_req_blocked...........: avg=1.44ms min=0s med=0s max=2.03s p(90)=1µs p(95)=1µs
http_req_connecting........: avg=799.37µs min=0s med=0s max=2.02s p(90)=0s p(95)=0s
http_req_duration..........: avg=276.88ms min=95.46ms med=331.17ms max=945.9ms p(90)=386.99ms p(95)=409.45ms
http_req_receiving.........: avg=128.82µs min=37µs med=75µs max=38.73ms p(90)=115µs p(95)=142.25µs
http_req_sending...........: avg=63.91µs min=21µs med=54µs max=12.79ms p(90)=83µs p(95)=109µs
http_req_tls_handshaking...: avg=343.62µs min=0s med=0s max=111.96ms p(90)=0s p(95)=0s
http_req_waiting...........: avg=276.68ms min=91.2ms med=330.94ms max=945.71ms p(90)=386.78ms p(95)=409.32ms
http_reqs..................: 3436 11.453326/s
iteration_duration.........: avg=3.73s min=1.49s med=3.71s max=7.07s p(90)=4.95s p(95)=5.11s
iterations.................: 3389 11.296659/s
vus........................: 1 min=1 max=50
vus_max....................: 50 min=50 max=50
c2: remove origin response handler, s3 origin is just a dummy origin, origin request handler directly hits S3
execution: local
output: -
script: index-test.js
duration: -, iterations: -
vus: 1, max: 50
done [==========================================================] 5m0s / 5m0s
✓ content is present
✗ status is 200
↳ 99% — ✓ 3512 / ✗ 21
✓ check_failure_rate.........: 0.59% ✓ 21 ✗ 3512
checks.....................: 99.68% ✓ 7046 ✗ 22
data_received..............: 9.0 MB 30 kB/s
data_sent..................: 278 kB 927 B/s
http_req_blocked...........: avg=1.06ms min=0s med=0s max=1.04s p(90)=1µs p(95)=1µs
http_req_connecting........: avg=706.7µs min=0s med=0s max=1.03s p(90)=0s p(95)=0s
http_req_duration..........: avg=137.28ms min=27.44ms med=121.03ms max=1.6s p(90)=166.11ms p(95)=199.73ms
http_req_receiving.........: avg=143.19µs min=35µs med=77µs max=89.06ms p(90)=106µs p(95)=119µs
http_req_sending...........: avg=54.39µs min=19µs med=53µs max=204µs p(90)=70µs p(95)=75µs
http_req_tls_handshaking...: avg=338.09µs min=0s med=0s max=267.3ms p(90)=0s p(95)=0s
http_req_waiting...........: avg=137.09ms min=25.72ms med=120.82ms max=1.6s p(90)=165.85ms p(95)=199.25ms
http_reqs..................: 3534 11.779992/s
iteration_duration.........: avg=3.63s min=412.72ms med=3.63s max=6.19s p(90)=4.8s p(95)=4.99s
iterations.................: 3483 11.609992/s
vus........................: 1 min=1 max=50
vus_max....................: 50 min=50 max=50
c3: remove origin response handler, s3 origin is just a dummy origin, origin request handler serves pages from the Lambda package (at edge)
execution: local
output: -
script: index-test.js
duration: -, iterations: -
vus: 1, max: 50
done [==========================================================] 5m0s / 5m0s
✓ status is 200
✓ content is present
✓ check_failure_rate.........: 0.00% ✓ 0 ✗ 3631
checks.....................: 100.00% ✓ 7264 ✗ 0
data_received..............: 9.3 MB 31 kB/s
data_sent..................: 285 kB 949 B/s
http_req_blocked...........: avg=834.65µs min=0s med=0s max=1.08s p(90)=1µs p(95)=1µs
http_req_connecting........: avg=534.69µs min=0s med=0s max=1.03s p(90)=0s p(95)=0s
http_req_duration..........: avg=48.88ms min=24.26ms med=35.98ms max=903.57ms p(90)=70.64ms p(95)=96.75ms
http_req_receiving.........: avg=106.93µs min=34µs med=72µs max=19.48ms p(90)=102µs p(95)=118µs
http_req_sending...........: avg=54.14µs min=20µs med=51µs max=1.63ms p(90)=70µs p(95)=78µs
http_req_tls_handshaking...: avg=292.05µs min=0s med=0s max=256.82ms p(90)=0s p(95)=0s
http_req_waiting...........: avg=48.72ms min=24.17ms med=35.79ms max=903.48ms p(90)=70.53ms p(95)=96.63ms
http_reqs..................: 3632 12.106659/s
iteration_duration.........: avg=3.54s min=347.88ms med=3.52s max=5.72s p(90)=4.76s p(95)=4.91s
iterations.................: 3581 11.936659/s
vus........................: 1 min=1 max=50
vus_max....................: 50 min=50 max=50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment