Skip to content

Commit

Permalink
chore: do not keep log file open
Browse files Browse the repository at this point in the history
  • Loading branch information
rorlic committed Mar 19, 2024
1 parent d99f7b1 commit 3006cb4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
53 changes: 34 additions & 19 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ export class Controller {

private _writeMetadata(run: TestRun) {
const metadata = path.join(this._baseFolder, run.id, metadataName);
const fd = fs.openSync(metadata, 'w');
try {
fs.writeFileSync(fd, JSON.stringify(run), { encoding: 'utf8', flush: true });
} finally {
fs.closeSync(fd);
}
this._write(metadata, JSON.stringify(run));
}

private _purgeTestRun(run: TestRun) {
Expand All @@ -93,6 +88,33 @@ export class Controller {
return '';
}

private _read(fullPathName: string) {
const fd = fs.openSync(fullPathName, 'r');
try {
return fs.readFileSync(fd, { encoding: 'utf8' });
} finally {
fs.closeSync(fd);
}
}

private _write(fullPathName: string, data: string) {
const fd = fs.openSync(fullPathName, 'w');
try {
fs.writeFileSync(fd, data, { encoding: 'utf8', flush: true });
} finally {
fs.closeSync(fd);
}
}

private _append(fullPathName: string, data: string) {
const fd = fs.openSync(fullPathName, 'a');
try {
fs.writeFileSync(fd, data, { encoding: 'utf8', flush: true });
} finally {
fs.closeSync(fd);
}
}

constructor(private _baseFolder: string, private _baseUrl: string, private _refreshTimeInSeconds: number) { }

public get runningCount(): number {
Expand Down Expand Up @@ -136,20 +158,12 @@ export class Controller {
return this._testRuns.map(x => this._purgeTestRun(x));
}

private _read(fullPathName: string) {
const fd = fs.openSync(fullPathName, 'r');
try {
fs.readFileSync(fd, { encoding: 'utf8' });
} finally {
fs.closeSync(fd);
}
}

public async getTestRunStatus(id: string, limit: number = 1000) {
const run = this._getTestRun(id);
if (!run) throw new Error(`Test ${id} does not exist.`);

const output = limit ? await read(run.stdout, limit) : this._read(run.stdout);
const logs = path.join(this._baseFolder, id, stdoutName);
const output = limit ? await read(logs, limit) : this._read(logs);
const data = {
...run,
refresh: run.status === TestRunStatus.running ? this._refreshTimeInSeconds : false,
Expand Down Expand Up @@ -202,15 +216,13 @@ export class Controller {
await fsp.writeFile(path.join(folder, testName), body);

const parsed = this._testParser.parse(body) as JMeterTest;
const stdout = path.join(folder, stdoutName);
const timestamp = new Date().toISOString();

const run = {
id: id,
name: parsed.jmeterTestPlan.hashTree.TestPlan._testname,
category: category,
timestamp: timestamp,
stdout: stdout,
status: TestRunStatus.running
} as TestRun;
this._writeMetadata(this._upsertTestRun(run));
Expand All @@ -225,7 +237,10 @@ export class Controller {
}
});

jmeter.stdout.pipe(fs.createWriteStream(stdout, { encoding: 'utf8', flags: 'a', flush: true, autoClose: true, emitClose: false }));
// jmeter.stdout.pipe(fs.createWriteStream(stdout, { encoding: 'utf8', flags: 'a', flush: true, autoClose: true, emitClose: false }));
const logs = path.join(folder, stdoutName);
jmeter.stdout.on('data', (data: any) => { this._append(logs, data.toString())});
jmeter.stderr.on('data', (data: any) => { this._append(logs, data.toString())});

const statusUrl = `${this._baseUrl}/${id}`;
const resultsUrl = `${statusUrl}/results/`;
Expand Down
1 change: 0 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export interface TestRun {
category?: string;
name: string;
timestamp: string;
stdout: string;
status: TestRunStatus;
code: number | null;
}
Expand Down

0 comments on commit 3006cb4

Please sign in to comment.