8000 Renaming diagnostics functions by jameslan · Pull Request #27 · jameslan/libxml2-wasm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Renaming diagnostics functions #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/diag.mts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ class MemTrackerImpl implements MemTracker {
}

report(): any {
const report: any = {};
const memReport: any = {};
this.disposableInfo.forEach((info) => {
const classReport = report[info.classname] ||= { // eslint-disable-line no-multi-assign
// eslint-disable-next-line no-multi-assign
const classReport = memReport[info.classname] ||= {
garbageCollected: 0,
totalInstances: 0,
instances: [],
Expand All @@ -102,21 +103,21 @@ class MemTrackerImpl implements MemTracker {
callers[info.callstack!] = (callers[info.callstack!] || 0) + 1;
}
});
return report;
return memReport;
}
}

/**
* Memory Diagnostic options.
*/
export interface MemDiagOptions {
export interface DiagOptions {
/**
* Enabling the memory diagnostics.
* Note the tracking information will be lost when it is disabled.
* Note that the tracking information will be lost when it is disabled.
*/
enabled: boolean;
/**
* Generate the statistics of point
* Generate the statistics of the callstack, for {@link disposable!XmlDisposable}.
*/
callerStats?: boolean;
/**
Expand All @@ -135,9 +136,9 @@ export interface MemDiagOptions {
* Note that the allocation will not be monitored before memory diagnostics is enabled.
*
* @param options
* @see {@link memReport}
* @see {@link report}
*/
export function memDiag(options: MemDiagOptions) {
export function configure(options: DiagOptions) {
if (options.enabled) {
memTracker = new MemTrackerImpl(
options.callerDetail === true,
Expand All @@ -153,9 +154,9 @@ export function memDiag(options: MemDiagOptions) {
* @returns The report (JSON) object, whose format may vary according to the settings,
* and is subject to change.
* Returns undefined if memory diagnostic is not enabled.
* @see {@link memDiag}
* @see {@link configure}
*/
export function memReport(): any {
export function report(): any {
return memTracker.report();
}

Expand Down
38 changes: 19 additions & 19 deletions test/crossplatform/diag.spec.mts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { expect } from 'chai';
import { diag, XmlDocument } from '@libxml2-wasm/lib/index.mjs';

describe('memDiag', () => {
describe('diagnostics', () => {
it('should report remaining objects', () => {
diag.memDiag({ enabled: true });
diag.configure({ enabled: true });
const xml1 = XmlDocument.fromString('<doc/>');
const xml2 = XmlDocument.fromString('<doc/>');

xml1.dispose();
const report = diag.memReport();
const report = diag.report();

expect(report.XmlDocument).is.not.null;
expect(report.XmlDocument.totalInstances).to.equal(1);
Expand All @@ -17,13 +17,13 @@ describe('memDiag', () => {
expect(report.XmlDocument.instances[0].caller).to.be.undefined;
expect(report.XmlDocument.callers).to.be.undefined;
xml2.dispose();
diag.memDiag({ enabled: false });
diag.configure({ enabled: false });
});

it('should report caller detail', () => {
diag.memDiag({ enabled: true, callerDetail: true });
diag.configure({ enabled: true, callerDetail: true });
const xml = XmlDocument.fromString('<doc/>');
const report = diag.memReport();
const report = diag.report();

expect(report.XmlDocument).is.not.null;
expect(report.XmlDocument.totalInstances).to.equal(1);
Expand All @@ -32,13 +32,13 @@ describe('memDiag', () => {
expect(report.XmlDocument.instances[0].caller).to.be.a('string');
expect(report.XmlDocument.callers).to.be.undefined;
xml.dispose();
diag.memDiag({ enabled: false });
diag.configure({ enabled: false });
});

it('should report caller stats', () => {
diag.memDiag({ enabled: true, callerStats: true });
diag.configure({ enabled: true, callerStats: true });
const xml = XmlDocument.fromString('<doc/>');
const report = diag.memReport();
const report = diag.report();

expect(report.XmlDocument).is.not.null;
expect(report.XmlDocument.totalInstances).to.equal(1);
Expand All @@ -47,57 +47,57 @@ describe('memDiag', () => {
expect(report.XmlDocument.instances[0].caller).to.be.undefined;
expect(Object.values(report.XmlDocument.callers)).to.deep.equal([1]);
xml.dispose();
diag.memDiag({ enabled: false });
diag.configure({ enabled: false });
});

it('could report caller stat for GC\'ed instance', async () => {
diag.memDiag({ enabled: true, callerStats: true });
diag.configure({ enabled: true, callerStats: true });
XmlDocument.fromString('<doc/>'); // to be GC'ed

// allow finalizer to run
await new Promise((resolve) => { setTimeout(resolve, 0); });
(global as any).gc();
const report = diag.memReport();
const report = diag.report();

expect(report.XmlDocument).is.not.null;
expect(report.XmlDocument.totalInstances).to.equal(1);
expect(report.XmlDocument.garbageCollected).to.equal(1);
expect(report.XmlDocument.instances).to.deep.equal([]);
expect(Object.values(report.XmlDocument.callers)).to.deep.equal([1]);
diag.memDiag({ enabled: false });
diag.configure({ enabled: false });
});

it('should report GC\'ed objects', async () => {
diag.memDiag({ enabled: true });
diag.configure({ enabled: true });
const xml1 = XmlDocument.fromString('<doc/>');
XmlDocument.fromString('<doc/>'); // to be GC'ed

// allow finalizer to run
await new Promise((resolve) => { 8A6F setTimeout(resolve, 0); });
(global as any).gc();
const report = diag.memReport();
const report = diag.report();

expect(report.XmlDocument).is.not.null;
expect(report.XmlDocument.totalInstances).to.equal(2);
expect(report.XmlDocument.garbageCollected).to.equal(1);
expect(report.XmlDocument.instances[0].instance).to.equal(xml1);
xml1.dispose();
diag.memDiag({ enabled: false });
diag.configure({ enabled: false });
});

it('will not track allocation before enabled', () => {
const xml1 = XmlDocument.fromString('<doc/>');
diag.memDiag({ enabled: true });
diag.configure({ enabled: true });
const xml2 = XmlDocument.fromString('<doc/>');

const report = diag.memReport();
const report = diag.report();

expect(report.XmlDocument).is.not.null;
expect(report.XmlDocument.totalInstances).to.equal(1);
expect(report.XmlDocument.garbageCollected).to.equal(0);
expect(report.XmlDocument.instances[0].instance).to.equal(xml2);
xml2.dispose();
xml1.dispose();
diag.memDiag({ enabled: false });
diag.configure({ enabled: false });
});
});
0