Skip to content

Instantly share code, notes, and snippets.

@w0rd-driven
Created June 28, 2023 19:28
Show Gist options
  • Save w0rd-driven/4e982d154a9c423997b73b714ff7e30d to your computer and use it in GitHub Desktop.
Save w0rd-driven/4e982d154a9c423997b73b714ff7e30d to your computer and use it in GitHub Desktop.
K6 S3 test for Digital Ocean Spaces
import exec from 'k6/execution';
import http from 'k6/http';
import { AWSError, AWSConfig, SignatureV4, S3Client } from 'https://jslib.k6.io/aws/0.8.0/aws.js'
const bucketName = 'orange-cdn-west';
export const options = {
ext: {
loadimpact: {
distribution: {
'amazon:us:ashburn': { loadZone: 'amazon:us:ashburn', percent: 50 },
'amazon:us:columbus': { loadZone: 'amazon:us:columbus', percent: 50 },
},
apm: [],
},
},
thresholds: {},
scenarios: {
Ramping: {
executor: 'ramping-vus',
gracefulStop: '30s',
stages: [
{ target: 100, duration: '1m' },
{ target: 500, duration: '3m30s' },
{ target: 100, duration: '1m' },
{ target: 0, duration: '1m' },
],
startVUs: 20,
gracefulRampDown: '30s',
exec: 'ramping',
},
},
}
function listBuckets() {
const method = 'GET'
const scheme = 'https'
const host = `${__ENV.REGION}.digitaloceanspaces.com`
const signer = new SignatureV4({
service: 's3',
region: __ENV.REGION,
credentials: {
accessKeyId: __ENV.ACCESS_KEY_ID,
secretAccessKey: __ENV.SECRET_ACCESS_KEY,
},
uriEscapePath: false,
applyChecksum: true,
})
const signedRequest = signer.sign(
{
method: method,
protocol: scheme,
hostname: host,
path: '/',
headers: {},
},
{}
)
const res = http.request(method, signedRequest.url, signedRequest.body || null, {
headers: signedRequest.headers,
})
return res
}
export function setup() {
const awsConfig = new AWSConfig({
region: `${__ENV.REGION}`,
endpoint: `${__ENV.REGION}.digitaloceanspaces.com`,
accessKeyId: `${__ENV.ACCESS_KEY_ID}`,
secretAccessKey: `${__ENV.SECRET_ACCESS_KEY}`,
});
const s3Client = new S3Client(awsConfig)
s3Client.host = `${__ENV.REGION}.digitaloceanspaces.com`
s3Client.scheme = `https`
try {
// Normal method still throws 503 errors
// const buckets = s3Client.listBuckets()
// console.log(buckets)
// if (buckets.filter((b) => b.name === bucketName).length == 0) {
// exec.test.abort()
// }
const buckets = listBuckets()
console.log(buckets)
// We want to abort the test even if successful
console.error("Aborting the test...")
exec.test.abort();
} catch(exception) {
console.error(exception);
exec.test.abort();
}
}
export function ramping() {
console.log("We're pretending to stress an endpoint.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment