✨Added root folder for all uploads, document creation, and cleanup after
This commit is contained in:
committed by
Anton Shepilov
parent
47106a8459
commit
0f9e3fb8b4
@@ -3,28 +3,131 @@ import { check, sleep } from "k6";
|
||||
import { FormData } from "https://jslib.k6.io/formdata/0.0.2/index.js";
|
||||
import { Trend } from "k6/metrics";
|
||||
|
||||
//============== init block of the whole test ==============
|
||||
|
||||
// Custom metric to track upload duration
|
||||
let uploadDuration = new Trend("upload_duration");
|
||||
|
||||
export let options = {
|
||||
stages: [
|
||||
{ duration: "30s", target: 20 },
|
||||
{ duration: "1m", target: 20 },
|
||||
{ duration: "10s", target: 0 },
|
||||
{ duration: "30s", target: 2 },
|
||||
{ duration: "3m", target: 2 },
|
||||
{ duration: "3m", target: 1 },
|
||||
],
|
||||
thresholds: {
|
||||
upload_duration: ["p(95)<500"], // 95% of uploads should be faster than 500ms
|
||||
upload_duration: ["p(95)<10000"], // 95% of uploads should be faster than 1500ms
|
||||
},
|
||||
};
|
||||
|
||||
const files = [
|
||||
{ path: "../assets/sample.pdf", type: "application/pdf" },
|
||||
{ path: "../assets/sample.png", type: "image/png" },
|
||||
];
|
||||
|
||||
const binFile = open("../assests/sample.pdf", 'b');
|
||||
const baseURL = `${__ENV.BACKEND}/internal/services/files/v1/companies`;
|
||||
const JWT = __ENV.JWT; // Set JWT as an environment variable
|
||||
const companyID = __ENV.COMPANY_ID; // Set Company ID as an environment variable
|
||||
|
||||
function uploadFile(filePath, fileType, JWT, companyID) {
|
||||
|
||||
//============== init VUs with test data ==============
|
||||
export function setup() {
|
||||
const data = {};
|
||||
|
||||
//get user info
|
||||
data.user = get(`${__ENV.BACKEND}/internal/services/users/v1/users/me`).resource
|
||||
console.log(`Start VU with user: ${JSON.stringify(data.user)}`);
|
||||
|
||||
//create root folder to upload all files
|
||||
const url = `${__ENV.BACKEND}/internal/services/documents/v1/companies/${companyID}/item`
|
||||
const payload = {
|
||||
item:{
|
||||
name: "PerfTests",
|
||||
parent_id: "user_" + data.user.id,
|
||||
is_directory: true,
|
||||
company_id: "00000000-0000-4000-0000-000000000000"
|
||||
},
|
||||
version: {}
|
||||
};
|
||||
data.rootFolder = post(url, payload);
|
||||
console.log(`Created root folder for performance tests: ${JSON.stringify(data.rootFolder)}`);
|
||||
return data;
|
||||
}
|
||||
|
||||
export default function (data) {
|
||||
const responseBody = uploadFile(binFile, "application/pdf");
|
||||
check(responseBody, {
|
||||
"response is successful": body => body.resource !== undefined,
|
||||
"company_id is present": body => body.resource.company_id !== undefined,
|
||||
});
|
||||
|
||||
const file = responseBody.resource;
|
||||
|
||||
const item = {
|
||||
name: file.metadata.name,
|
||||
parent_id: data.rootFolder.id,
|
||||
company_id: file.company_id,
|
||||
};
|
||||
|
||||
const version = {
|
||||
file_metadata: {
|
||||
name: file.metadata.name,
|
||||
size: file.upload_data?.size,
|
||||
thumbnails: [],
|
||||
external_id: file.id,
|
||||
},
|
||||
};
|
||||
|
||||
const response = post(
|
||||
`${__ENV.BACKEND}/internal/services/documents/v1/companies/${companyID}/item`,
|
||||
{
|
||||
item,
|
||||
version,
|
||||
}
|
||||
);
|
||||
|
||||
check(response, {
|
||||
"response is successful": body => body !== undefined,
|
||||
});
|
||||
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
export function teardown(data) {
|
||||
deleteMe(`${__ENV.BACKEND}/internal/services/documents/v1/companies/${companyID}/item/${data.rootFolder.id}`)
|
||||
deleteMe(`${__ENV.BACKEND}/internal/services/documents/v1/companies/${companyID}/item/${data.rootFolder.id}`)
|
||||
}
|
||||
|
||||
|
||||
|
||||
function post(url, payload) {
|
||||
const headers = {
|
||||
Authorization: `Bearer ${JWT}`,
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
console.debug(`POST BODY: ${JSON.stringify(payload)}`);
|
||||
const response = http.post(url, JSON.stringify(payload), { headers })
|
||||
console.debug(`POST RESPONSE: ${JSON.stringify(response)}`);
|
||||
return JSON.parse(response.body)
|
||||
}
|
||||
|
||||
function get(url) {
|
||||
const headers = {
|
||||
Authorization: `Bearer ${JWT}`,
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
const response = http.get(url, { headers })
|
||||
return JSON.parse(response.body)
|
||||
}
|
||||
|
||||
function deleteMe(url) {
|
||||
const headers = {
|
||||
Authorization: `Bearer ${JWT}`,
|
||||
};
|
||||
const response = http.del(url, {}, { headers })
|
||||
console.debug(`DELETE RESPONSE: ${JSON.stringify(response)}`);
|
||||
check(response, {
|
||||
"DELETE response is successful": response => response.status === 200,
|
||||
});
|
||||
return response
|
||||
}
|
||||
|
||||
function uploadFile(filePath, fileType) {
|
||||
const url = `${baseURL}/${companyID}/files?thumbnail_sync=0`;
|
||||
const formData = new FormData();
|
||||
const randomInt = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
|
||||
@@ -39,24 +142,8 @@ function uploadFile(filePath, fileType, JWT, companyID) {
|
||||
let uploadStartTime = new Date().getTime();
|
||||
const response = http.post(url, formData.body(), { headers });
|
||||
let uploadEndTime = new Date().getTime();
|
||||
console.debug(`POST RESPONSE: ${JSON.stringify(response)}`);
|
||||
|
||||
uploadDuration.add(uploadEndTime - uploadStartTime);
|
||||
|
||||
return JSON.parse(response.body);
|
||||
}
|
||||
|
||||
export default function () {
|
||||
const JWT = __ENV.JWT; // Set JWT as an environment variable
|
||||
const companyID = __ENV.COMPANY_ID; // Set Company ID as an environment variable
|
||||
const file = files[Math.floor(Math.random() * files.length)];
|
||||
|
||||
const responseBody = uploadFile(file.path, file.type, JWT, companyID);
|
||||
check(responseBody, {
|
||||
"response is successful": body => body.resource !== undefined,
|
||||
"company_id is present": body => body.resource.company_id !== undefined,
|
||||
});
|
||||
|
||||
|
||||
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
+1071
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "test-commons",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"engines": {
|
||||
"node": ">=18 <19"
|
||||
},
|
||||
"dependencies": {
|
||||
"pino-pretty": "^12.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-node-resolve": "^16.0.0",
|
||||
"@rollup/plugin-commonjs": "^28.0.2",
|
||||
"rollup": "^4.9.5",
|
||||
"rollup-plugin-esbuild-minify": "^1.2.0"
|
||||
},
|
||||
"scripts": {
|
||||
"rollup": "rollup --config",
|
||||
"tests": "rollup --config"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { minify } from 'rollup-plugin-esbuild-minify'
|
||||
import { nodeResolve } from '@rollup/plugin-node-resolve';
|
||||
import commonjs from '@rollup/plugin-commonjs';
|
||||
|
||||
export default {
|
||||
input: 'files/upload-files.js',
|
||||
output: {
|
||||
dir: 'dist',
|
||||
format: 'es',
|
||||
sourcemap: true,
|
||||
},
|
||||
external: [new RegExp(/^(k6|https?\:\/\/)(\/.*)?/)],
|
||||
plugins: [nodeResolve()]
|
||||
};
|
||||
Reference in New Issue
Block a user