-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tracked list to utils (#2338)
To allow tracking sizes of internal lists using libp2p metrics, add a tracked list type similar to tracked map.
- Loading branch information
1 parent
388d02b
commit 581574d
Showing
4 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Metrics } from '@libp2p/interface' | ||
|
||
export interface CreateTrackedListInit { | ||
/** | ||
* The metric name to use | ||
*/ | ||
name: string | ||
|
||
/** | ||
* A metrics implementation | ||
*/ | ||
metrics?: Metrics | ||
} | ||
|
||
export function trackedList <V> (config: CreateTrackedListInit): V[] { | ||
const { name, metrics } = config | ||
const list: V[] = [] | ||
|
||
metrics?.registerMetric(name, { | ||
calculate: () => { | ||
return list.length | ||
} | ||
}) | ||
|
||
return list | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { expect } from 'aegir/chai' | ||
import { stubInterface } from 'sinon-ts' | ||
import { trackedList } from '../src/tracked-list.js' | ||
import type { Metric, Metrics } from '@libp2p/interface' | ||
import type { SinonStubbedInstance } from 'sinon' | ||
|
||
describe('tracked-list', () => { | ||
let metrics: SinonStubbedInstance<Metrics> | ||
|
||
beforeEach(() => { | ||
metrics = stubInterface<Metrics>() | ||
}) | ||
|
||
it('should return a list with metrics', () => { | ||
const name = 'system_component_metric' | ||
const metric = stubInterface<Metric>() | ||
// @ts-expect-error the wrong overload is selected | ||
metrics.registerMetric.withArgs(name).returns(metric) | ||
|
||
const list = trackedList({ | ||
name, | ||
metrics | ||
}) | ||
|
||
expect(list).to.be.an.instanceOf(Array) | ||
expect(metrics.registerMetric.calledWith(name)).to.be.true() | ||
}) | ||
|
||
it('should return a map without metrics', () => { | ||
const name = 'system_component_metric' | ||
const metric = stubInterface<Metric>() | ||
// @ts-expect-error the wrong overload is selected | ||
metrics.registerMetric.withArgs(name).returns(metric) | ||
|
||
const list = trackedList({ | ||
name | ||
}) | ||
|
||
expect(list).to.be.an.instanceOf(Array) | ||
expect(metrics.registerMetric.called).to.be.false() | ||
}) | ||
|
||
it('should track metrics', () => { | ||
const name = 'system_component_metric' | ||
|
||
const list = trackedList({ | ||
name, | ||
metrics | ||
}) | ||
|
||
const calculate = metrics.registerMetric.getCall(0).args[1].calculate | ||
|
||
expect(list).to.be.an.instanceOf(Array) | ||
expect(calculate()).to.equal(0) | ||
|
||
list.push('value1') | ||
|
||
expect(calculate()).to.equal(1) | ||
|
||
list.push('value2') | ||
|
||
expect(calculate()).to.equal(2) | ||
|
||
list.push('value3') | ||
|
||
expect(calculate()).to.equal(3) | ||
|
||
list.pop() | ||
|
||
expect(calculate()).to.equal(2) | ||
|
||
list.splice(0, list.length) | ||
|
||
expect(calculate()).to.equal(0) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters