Skip to content

Commit

Permalink
perf: remove redundant information from cpu stats reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKolarik committed May 10, 2024
1 parent 020f876 commit f6455cc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
9 changes: 4 additions & 5 deletions src/lib/ws/synced-probe-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class SyncedProbeList extends EventEmitter {
}

private serializeProbeStats (stats: ProbeStats): string {
const loadStats = stats.cpu.load.flatMap(load => [ load.idle, load.usage ]);
const loadStats = stats.cpu.load.map(load => load.usage);
return [ stats.jobs.count ].concat(loadStats).join();
}

Expand All @@ -196,10 +196,9 @@ export class SyncedProbeList extends EventEmitter {
count: parts[0] || 0,
},
cpu: {
count: Math.floor(parts.length / 2),
load: parts.slice(1).reduce((acc, v, i, a) => {
return acc.concat([{ idle: v, usage: a[i + 1] || 0 }]);
}, [] as ProbeStats['cpu']['load']),
load: parts.slice(1).map((usage) => {
return { usage };
}),
},
};
}
Expand Down
18 changes: 9 additions & 9 deletions test/tests/unit/ws/synced-probe-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ describe('SyncedProbeList', () => {

it('emits stats in the message on change', async () => {
const sockets = [
{ data: { probe: { client: 'A', location: {}, stats: { cpu: { count: 1, load: [{ idle: 0, usage: 0 }] }, jobs: { count: 0 } } } } },
{ data: { probe: { client: 'B', location: {}, stats: { cpu: { count: 1, load: [{ idle: 0, usage: 0 }] }, jobs: { count: 0 } } } } },
{ data: { probe: { client: 'C', location: {}, stats: { cpu: { count: 1, load: [{ idle: 0, usage: 0 }] }, jobs: { count: 0 } } } } },
{ data: { probe: { client: 'A', location: {}, stats: { cpu: { load: [{ usage: 0 }] }, jobs: { count: 0 } } } } },
{ data: { probe: { client: 'B', location: {}, stats: { cpu: { load: [{ usage: 0 }] }, jobs: { count: 0 } } } } },
{ data: { probe: { client: 'C', location: {}, stats: { cpu: { load: [{ usage: 0 }] }, jobs: { count: 0 } } } } },
];

localFetchSocketsStub.resolves(sockets);
Expand All @@ -119,8 +119,8 @@ describe('SyncedProbeList', () => {

// @ts-expect-error the arg must be an object
expect(JSON.parse(redisXAdd.secondCall.args[2].s)).to.deep.equal({
B: '1,0,0',
C: '1,0,0',
B: '1,0',
C: '1,0',
});

expect(redisXAdd.secondCall.args[2]).to.not.have.property('r');
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('SyncedProbeList', () => {

// @ts-expect-error the arg must be an object
expect(JSON.parse(redisXAdd.args[3][2].s)).to.deep.equal({
C: '2,0,0',
C: '2,0',
});

expect(redisXAdd.args[3]![2]).to.deep.include({ '+': 'D', '-': 'B' });
Expand Down Expand Up @@ -189,9 +189,9 @@ describe('SyncedProbeList', () => {

it('reads remote stats updates', async () => {
const probes = {
A: { client: 'A', location: {}, stats: { cpu: { count: 1, load: [{ idle: 0, usage: 0 }] }, jobs: { count: 0 } } },
B: { client: 'B', location: {}, stats: { cpu: { count: 1, load: [{ idle: 0, usage: 0 }] }, jobs: { count: 0 } } },
C: { client: 'C', location: {}, stats: { cpu: { count: 1, load: [{ idle: 0, usage: 0 }] }, jobs: { count: 0 } } },
A: { client: 'A', location: {}, stats: { cpu: { load: [{ usage: 0 }] }, jobs: { count: 0 } } },
B: { client: 'B', location: {}, stats: { cpu: { load: [{ usage: 0 }] }, jobs: { count: 0 } } },
C: { client: 'C', location: {}, stats: { cpu: { load: [{ usage: 0 }] }, jobs: { count: 0 } } },
} as unknown as Record<string, Probe>;

redisXRange.resolves([
Expand Down

0 comments on commit f6455cc

Please sign in to comment.