Skip to content

Commit

Permalink
add report about used middlewares (#221)
Browse files Browse the repository at this point in the history
* add report about used middlewares

fixes idrinth-api-bench/issues#334

* fix middlewares being potentially not defined

fixes idrinth-api-bench/issues#334

* fix typos

fixes idrinth-api-bench/issues#334

* fix typos

fixes idrinth-api-bench/issues#334

* fix broken test

fixes idrinth-api-bench/issues#334

* fix broken test

fixes idrinth-api-bench/issues#334

* fix broken test

fixes idrinth-api-bench/issues#334
  • Loading branch information
Idrinth authored Jan 28, 2025
1 parent 3be3b53 commit 257f758
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 22 deletions.
3 changes: 3 additions & 0 deletions integration/executor-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class FakeResult implements Result, ValidationResult, FinishedSet {

public maxDuration: number;

public middlewares: string[];

public response = {
headers: {},
cookies: {},
Expand Down Expand Up @@ -91,6 +93,7 @@ class FakeResult implements Result, ValidationResult, FinishedSet {
this.median80 = duration;
this.stdv100 = 100;
this.stdv80 = 80;
this.middlewares = ['id', 'duration'];
}

public add() {
Expand Down
1 change: 1 addition & 0 deletions src/messaging/finished-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface FinishedSet {
min80: number;
max80: number;
msgs?: {[msg: string]: number};
middlewares?: string[];
}

export default FinishedSet;
6 changes: 6 additions & 0 deletions src/messaging/result-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ export class ResultSet {

public msgs: {[msg: string]: number};

public middlewares: Array<string>;

public constructor(public readonly id: string,) {
this.errors = INITIAL_ZERO;
this.count = INITIAL_ZERO;
this.durations = [];
this.msgs = {};
this.middlewares = [];
}

public add(result: ValidationResult,): void {
if (result.duration !== null) {
this.durations.push(result.duration,);
}
if (this.middlewares.length === 0) {
this.middlewares = result.middlewares ?? [];
}
this.count ++;
if (! result.success) {
this.errors ++;
Expand Down
1 change: 1 addition & 0 deletions src/messaging/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class Result {
response: NeedleResponse,
public validators: Array<string>,
public maxDuration: number|undefined,
public middlewares: Array<string>,
) {
this.duration = (end.shift() - start.shift()) * ToMicro;
this.duration += end.pop() - start.pop();
Expand Down
1 change: 1 addition & 0 deletions src/messaging/validation-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface ValidationResult {
id: string;
success: boolean;
msg?: string;
middlewares: Array<string>,
}

export default ValidationResult;
12 changes: 2 additions & 10 deletions src/reporter/console-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ const cli: Reporter = (
'median 100%',
'min 100%',
'max 100%',
'avg 80%',
'median 80%',
'min 80%',
'max 80%',
'stdv 80%',
'stdv 100%',
'Middlewares'
],
},);
const formatter = new Intl.NumberFormat();
Expand All @@ -41,12 +37,8 @@ const cli: Reporter = (
formatter.format(results[id].median100,),
formatter.format(results[id].min100,),
formatter.format(results[id].max100,),
formatter.format(results[id].avg80,),
formatter.format(results[id].median80,),
formatter.format(results[id].min80,),
formatter.format(results[id].max80,),
formatter.format(results[id].stdv80,),
formatter.format(results[id].stdv100,),
results[id]?.middlewares?.join() ?? '',
],);
}
// eslint-disable-next-line no-console
Expand Down
1 change: 1 addition & 0 deletions src/reporter/csv-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const csv: Reporter = (
csvStream.write({
...results[id],
msgs: JSON.stringify(results[id].msgs,),
middlewares: results[id]?.middlewares?.join() ?? '',
},);
}
csvStream.end();
Expand Down
2 changes: 2 additions & 0 deletions src/reporter/html-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const html: Reporter = (
stream.write('<th>Standard Deviation 80%</th>',);
stream.write('<th>Standard Deviation 100%</th>',);
stream.write('<th>Messages</th>',);
stream.write('<th>Middlewares</th>',);
stream.write('</tr>',);
stream.write('</thead>',);
stream.write('<tbody>',);
Expand All @@ -52,6 +53,7 @@ const html: Reporter = (
stream.write('<td>' + results[id].stdv80 + '</td>',);
stream.write('<td>' + results[id].stdv100 + '</td>',);
stream.write('<td>' + JSON.stringify(results[id].msgs,) + '</td>',);
stream.write('<td>' + (results[id]?.middlewares?.join() ?? '') + '</td>',);
stream.write('</tr>',);
}
stream.write('</tbody>',);
Expand Down
2 changes: 2 additions & 0 deletions src/worker/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default (result: ResultSet,): FinishedSet => {
median80: NaN,
min80: NaN,
max80: NaN,
middlewares: result.middlewares,
};
}
const sorted100 = result.durations.sort((a: number, b: number,) => a-b,);
Expand Down Expand Up @@ -71,5 +72,6 @@ export default (result: ResultSet,): FinishedSet => {
median80: calculateAverage(min80, max80,),
min80,
max80,
middlewares: result.middlewares ?? [],
};
};
8 changes: 8 additions & 0 deletions src/worker/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export default async(task: Task, callable: Callback,): Promise<void> => {
validators: [],
// eslint-disable-next-line no-undefined
maxDuration: undefined,
middlewares: [
...(task.pre?.map((value,) => `pre:${value}`) ?? []),
...(task.post?.map((value,) => `post:${value}`) ?? [])
],
} as Result, error+'', false,),);
return;
}
Expand All @@ -90,6 +94,10 @@ export default async(task: Task, callable: Callback,): Promise<void> => {
result,
task.post || [],
task.main.maxDuration,
[
...(task.pre?.map((value,) => `pre:${value}`) ?? []),
...(task.post?.map((value,) => `post:${value}`) ?? [])
],
);
if (await handlePost(task, httpResult, callable,)) {
callable(buildAnswer(httpResult, '', true,),);
Expand Down
3 changes: 2 additions & 1 deletion test/reporter/console-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import makeConsoleMock from 'consolemock';
import consoleReporter from '../../src/reporter/console-reporter';
import consoleReporter from '../../src/reporter/console-reporter.js';
import {
expect,
} from 'chai';
Expand Down Expand Up @@ -31,6 +31,7 @@ describe('reporter/console-reporter', () => {
max80: 99,
stdv80: 12,
stdv100: 99,
middlewares: ['test', 'rest']
},
};
expect(() => consoleReporter(results, '',),).to.not.throw();
Expand Down
14 changes: 11 additions & 3 deletions test/reporter/csv-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mock = require('mock-fs');
import csvReporter from '../../src/reporter/csv-reporter';
import csvReporter from '../../src/reporter/csv-reporter.js';
import {
expect,
} from 'chai';
Expand Down Expand Up @@ -40,6 +40,10 @@ describe('reporter/csv-reporter', () => {
max80: 99,
stdv100: 9,
stdv80: 8,
middlewares: ['hi'],
msgs: {
'hi': 98,
},
},
};
csvReporter(results, '/csv1',);
Expand All @@ -66,14 +70,18 @@ describe('reporter/csv-reporter', () => {
max80: 99,
stdv100: 9,
stdv80: 8,
middlewares: ['done'],
msgs: {
'hi': 89,
},
},
};
csvReporter(results, '/csv2',);
setTimeout(() => {
expect(readFileSync(file,) + '',).to.equal(
'id,errors,count,avg100,median100,min100,'
+ 'max100,avg80,median80,min80,max80,stdv100,stdv80,msgs'
+ '\n1,14,7,6,33,1,99,76,33,14,99,9,8,',
+ 'max100,avg80,median80,min80,max80,stdv100,stdv80,middlewares,msgs'
+ '\n1,14,7,6,33,1,99,76,33,14,99,9,8,done,"{""hi"":89}"',
);
done();
}, WAIT_TIME,);
Expand Down
10 changes: 7 additions & 3 deletions test/reporter/html-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mock = require('mock-fs');
import htmlReporter from '../../src/reporter/html-reporter';
import htmlReporter from '../../src/reporter/html-reporter.js';
import {
expect,
} from 'chai';
Expand Down Expand Up @@ -43,6 +43,7 @@ describe('reporter/html-reporter', () => {
msgs: {
'some error message': 4,
},
middlewares: ['none',],
},
};
htmlReporter(results, '/html1',);
Expand Down Expand Up @@ -72,6 +73,7 @@ describe('reporter/html-reporter', () => {
msgs: {
'some error message': 4,
},
middlewares: ['none', 'other'],
},
};
htmlReporter(results, '/html2',);
Expand All @@ -84,10 +86,12 @@ describe('reporter/html-reporter', () => {
'<th>Minimum 100%</th><th>Maximum 80%</th><th>Maximum 100%</th>' +
'<th>Median 80%</th><th>Median 100%</th>' +
'<th>Standard Deviation 80%</th><th>Standard Deviation 100%</th>' +
'<th>Messages</th></tr></thead><tbody><tr><th>any</th><td>7</td>' +
'<th>Messages</th><th>Middlewares</th></tr></thead>' +
'<tbody><tr><th>any</th><td>7</td>' +
'<td>14</td><td>76</td><td>6</td><td>14</td><td>1</td><td>99</td>' +
'<td>99</td><td>33</td><td>33</td><td>8</td><td>9</td>' +
'<td>{"some error message":4}</td></tr></tbody></table>' +
'<td>{"some error message":4}</td><td>none,other</td>' +
'</tr></tbody></table>' +
'</body></html>',
);
done();
Expand Down
4 changes: 3 additions & 1 deletion test/reporter/json-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mock = require('mock-fs');
import jsonReporter from '../../src/reporter/json-reporter';
import jsonReporter from '../../src/reporter/json-reporter.js';
import {
expect,
} from 'chai';
Expand Down Expand Up @@ -40,6 +40,7 @@ describe('reporter/json-reporter', () => {
max80: 99,
stdv80: 99,
stdv100: 99,
middlewares: ['hello', 'worlds']
},
};
jsonReporter(results, '/json1',);
Expand All @@ -66,6 +67,7 @@ describe('reporter/json-reporter', () => {
max80: 99,
stdv80: 99,
stdv100: 99,
middlewares: ['hello', 'world']
},
};
jsonReporter(results, '/json2',);
Expand Down
3 changes: 2 additions & 1 deletion test/reporter/multi-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mock = require('mock-fs');
import multiReporter from '../../src/reporter/multi-reporter';
import multiReporter from '../../src/reporter/multi-reporter.js';
import {
expect,
} from 'chai';
Expand Down Expand Up @@ -36,6 +36,7 @@ describe('reporter/multi-reporter', () => {
max80: 99,
stdv80: 99,
stdv100: 99,
middlewares: ['hello'],
},
};
let wasExecuted = false;
Expand Down
9 changes: 8 additions & 1 deletion test/result-set.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint no-magic-numbers:0 */
import ResultSet from '../src/messaging/result-set';
import ResultSet from '../src/messaging/result-set.js';
import {
expect,
} from 'chai';
Expand Down Expand Up @@ -39,27 +39,34 @@ describe('ResultSet', () => {
duration: 666,
success: false,
msg: 'Not enough programmers.',
middlewares: ['hello', 'worlds']
},);

expect(result.errors,).to.equal(1,);
expect(result.count,).to.equal(1,);
expect(result.msgs,).to.deep.equal({
'Not enough programmers.': 1,
},);
expect(result.middlewares,).to.deep.equal(['hello', 'worlds']);
expect(result.durations,).to.deep.equal([ 666, ],);
},);
it('using add should increase values appropriatly', () => {
result.add({
id: 'any',
duration: null,
success: true,
middlewares: [
'hello',
'world',
],
},);

expect(result.errors,).to.equal(1,);
expect(result.count,).to.equal(2,);
expect(result.msgs,).to.deep.equal({
'Not enough programmers.': 1,
},);
expect(result.middlewares,).to.deep.equal(['hello', 'worlds']);
expect(result.durations,).to.deep.equal([ 666, ],);
},);
},);
10 changes: 8 additions & 2 deletions test/worker/calculator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint no-magic-numbers:0 */
import calculator from '../../src/worker/calculator';
import calculator from '../../src/worker/calculator.js';
import {
expect,
} from 'chai';
import 'mocha';
import ValidationResult from '../../src/messaging/validation-result';
import ValidationResult from '../../src/messaging/validation-result.js';

describe('worker/calculator', () => {
it('should be a function', () => {
Expand All @@ -19,12 +19,14 @@ describe('worker/calculator', () => {
id: '##',
errors: 5,
durations: [],
middlewares: [],
count: 9,
},),).to.deep.equal({
id: '##',
errors: 5,
msgs: {},
count: 9,
middlewares: [],
avg100: NaN,
median100: NaN,
min100: NaN,
Expand All @@ -44,6 +46,7 @@ describe('worker/calculator', () => {
},
id: '#1',
errors: 5,
middlewares: [],
msgs: {
some: 5,
},
Expand All @@ -61,6 +64,7 @@ describe('worker/calculator', () => {
some: 5,
},
count: 9,
middlewares: [],
avg100: 3,
median100: 3,
min100: 1,
Expand All @@ -83,6 +87,7 @@ describe('worker/calculator', () => {
msgs: {
some: 5,
},
middlewares: [],
durations: [
1,
2,
Expand All @@ -107,6 +112,7 @@ describe('worker/calculator', () => {
msgs: {
some: 5,
},
middlewares: [],
count: 15,
avg100: 30,
median100: 172,
Expand Down

0 comments on commit 257f758

Please sign in to comment.