jmeter-job/index.js
2025-05-20 16:57:52 +07:00

147 lines
3.4 KiB
JavaScript

const shell = require('shelljs');
const path = require('path');
const scriptsDir = path.join(__dirname, 'jmeter-scripts');
// Schedule configuration: time ranges and jmx file
// const schedule = [
// {
// name: '20tps',
// startHour: 23,
// startMinute: 0,
// endHour: 23,
// endMinute: 10,
// jmx: 'lastlocation_20tps_10min_3000req.jmx',
// },
// {
// name: '50tps',
// startHour: 23,
// startMinute: 15,
// endHour: 23,
// endMinute: 25,
// jmx: 'lastlocation_50tps_10min_3000req.jmx',
// },
// {
// name: '100tps',
// startHour: 23,
// startMinute: 30,
// endHour: 23,
// endMinute: 40,
// jmx: 'lastlocation_100tps_10min_3000req.jmx',
// },
// ];
const schedule = [
{
name: '20tps',
startHour: 23,
startMinute: 0,
endHour: 23,
endMinute: 10,
jmx: 'lastlocation_20tps_10min_3000req.jmx',
},
{
name: '50tps',
startHour: 23,
startMinute: 15,
endHour: 23,
endMinute: 25,
jmx: 'lastlocation_50tps_10min_3000req.jmx',
},
{
name: '100tps',
startHour: 23,
startMinute: 30,
endHour: 23,
endMinute: 40,
jmx: 'lastlocation_100tps_10min_3000req.jmx',
},
];
// Track currently running schedule to avoid rerun within same slot
let currentlyRunning = null;
function isNowInSchedule(now, sched) {
// now is a Date object
const start = new Date(now);
start.setHours(sched.startHour, sched.startMinute, 0, 0);
const end = new Date(now);
end.setHours(sched.endHour, sched.endMinute, 0, 0);
return now >= start && now < end;
}
function formatDateForFolder(date) {
// Format YYYYMMDD_HHmmss for folder naming
const pad = (n) => n.toString().padStart(2, '0');
return (
date.getFullYear().toString() +
pad(date.getMonth() + 1) +
pad(date.getDate()) +
'_' +
pad(date.getHours()) +
pad(date.getMinutes()) +
pad(date.getSeconds())
);
}
function runJMeter(jmxFile) {
const now = new Date();
const timestamp = formatDateForFolder(now);
const resultFolder = path.join(
__dirname,
'results',
`${timestamp}_${path.basename(jmxFile, '.jmx')}`
);
const resultCsv = path.join(resultFolder, 'result.csv');
// Create result folder
shell.mkdir('-p', resultFolder);
// Build command
const cmd = [
'jmeter',
'-n',
'-t',
`"${path.join(scriptsDir, jmxFile)}"`,
'-l',
`"${resultCsv}"`,
'-e',
'-o',
`"${resultFolder}"`,
].join(' ');
console.log(`[${now.toISOString()}] Running: ${cmd}`);
// Run command asynchronously
shell.exec(cmd, { async: true }, (code, stdout, stderr) => {
if (code === 0) {
console.log(`[${new Date().toISOString()}] Finished running ${jmxFile}`);
} else {
console.error(`[${new Date().toISOString()}] JMeter exited with code ${code}`);
console.error(stderr);
}
currentlyRunning = null; // Allow next scheduled run
});
}
function checkSchedule() {
const now = new Date();
// If a job is already running, skip starting a new one
if (currentlyRunning) {
return;
}
// Find schedule where current time fits
const activeSchedule = schedule.find((sched) => isNowInSchedule(now, sched));
if (activeSchedule) {
currentlyRunning = activeSchedule.name;
runJMeter(activeSchedule.jmx);
}
}
// Run check every second
setInterval(checkSchedule, 1000);
console.log(`Scheduler started on ${new Date()}. Waiting for scheduled times to run JMeter tests...`);