Skip to content

Commit

Permalink
Add JSON output parameter to more analyses (#129)
Browse files Browse the repository at this point in the history
Summary:
Added `OutputOption` to a few more analyses, as the `printNodeListInTerminal` method supports JSON output.

Pull Request resolved: #129

Reviewed By: twobassdrum

Differential Revision: D61780561

Pulled By: JacksonGL

fbshipit-source-id: 28c3f55459f2ad1a04cfcbb7861e74fa5c3e5d22
  • Loading branch information
aelij authored and facebook-github-bot committed Aug 26, 2024
1 parent 66c1bf7 commit 574e4e1
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 23 deletions.
18 changes: 14 additions & 4 deletions packages/heap-analysis/src/PluginUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,8 @@ function printReferencesInTerminal(
options: AnyOptions & PrintNodeOption = {},
): void {
if (config.outputFormat === OutputFormat.Json) {
const jsonEdges = edgeList.map(edge => getEdgeRecord(edge));

info.writeOutput(JSON.stringify(jsonEdges));
info.writeOutput('\n');
printEdgesJson(edgeList);
return;
}

const dot = chalk.grey('· ');
Expand Down Expand Up @@ -337,6 +335,11 @@ function printReferrersInTerminal(
edgeList: IHeapEdge[],
options: AnyOptions & PrintNodeOption = {},
): void {
if (config.outputFormat === OutputFormat.Json) {
printEdgesJson(edgeList);
return;
}

const dot = chalk.grey('· ');
const indent = options.indent || '';
let n = 0;
Expand All @@ -353,6 +356,13 @@ function printReferrersInTerminal(
}
}

function printEdgesJson(edgeList: IHeapEdge[]) {
const jsonEdges = edgeList.map(edge => getEdgeRecord(edge));

info.writeOutput(JSON.stringify(jsonEdges));
info.writeOutput('\n');
}

function getObjectOutgoingEdgeCount(node: IHeapNode): number {
if (node.name === 'Set' || node.name === 'Map') {
const edge = utils.getEdgeByNameAndType(node, 'table');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class HeapAnalysisOutputOption extends BaseOption {

async parse(config: MemLabConfig, args: ParsedArgs): Promise<void> {
const name = this.getOptionName();
const format = `${args[name]}` ?? 'text';
const format = args[name] == null ? 'text' : `${args[name]}`;
config.outputFormat = HeapAnalysisOutputOption.parseOutputFormat(format);
if (config.outputFormat === OutputFormat.Json) {
config.isContinuousTest = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {utils, BaseOption} from '@memlab/core';
import BaseAnalysis from '../BaseAnalysis';
import pluginUtils from '../PluginUtils';
import SnapshotFileOption from '../options/HeapAnalysisSnapshotFileOption';
import OutputOption from '../options/HeapAnalysisOutputOption';

export default class DetachedDOMElementAnalysis extends BaseAnalysis {
getCommandName(): string {
Expand All @@ -28,7 +29,7 @@ export default class DetachedDOMElementAnalysis extends BaseAnalysis {

/** @internal */
getOptions(): BaseOption[] {
return [new SnapshotFileOption()];
return [new SnapshotFileOption(), new OutputOption()];
}

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {

import {BaseOption} from '@memlab/core';
import SnapshotFileOption from '../../options/HeapAnalysisSnapshotFileOption';
import OutputOption from '../../options/HeapAnalysisOutputOption';
import BaseAnalysis from '../../BaseAnalysis';
import pluginUtils from '../../PluginUtils';
import windowBuiltInVars from './BuiltInGlobalVariables';
Expand All @@ -32,7 +33,7 @@ class GlobalVariableAnalysis extends BaseAnalysis {

/** @internal */
getOptions(): BaseOption[] {
return [new SnapshotFileOption()];
return [new SnapshotFileOption(), new OutputOption()];
}

/** @internal */
Expand Down
3 changes: 2 additions & 1 deletion packages/heap-analysis/src/plugins/ObjectFanoutAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {BaseOption, utils} from '@memlab/core';
import BaseAnalysis from '../BaseAnalysis';
import pluginUtils from '../PluginUtils';
import SnapshotFileOption from '../options/HeapAnalysisSnapshotFileOption';
import OutputOption from '../options/HeapAnalysisOutputOption';

class ObjectFanoutAnalysis extends BaseAnalysis {
getCommandName(): string {
Expand All @@ -29,7 +30,7 @@ class ObjectFanoutAnalysis extends BaseAnalysis {

/** @internal */
getOptions(): BaseOption[] {
return [new SnapshotFileOption()];
return [new SnapshotFileOption(), new OutputOption()];
}

/** @internal */
Expand Down
3 changes: 2 additions & 1 deletion packages/heap-analysis/src/plugins/ObjectSizeAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {BaseOption, utils} from '@memlab/core';
import BaseAnalysis from '../BaseAnalysis';
import pluginUtils from '../PluginUtils';
import SnapshotFileOption from '../options/HeapAnalysisSnapshotFileOption';
import OutputOption from '../options/HeapAnalysisOutputOption';

class ObjectSizeRankAnalysis extends BaseAnalysis {
getCommandName(): string {
Expand All @@ -27,7 +28,7 @@ class ObjectSizeRankAnalysis extends BaseAnalysis {

/** @internal */
getOptions(): BaseOption[] {
return [new SnapshotFileOption()];
return [new SnapshotFileOption(), new OutputOption()];
}

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {AnalyzeSnapshotResult, HeapAnalysisOptions} from '../PluginUtils';

import {utils, BaseOption} from '@memlab/core';
import SnapshotFileOption from '../options/HeapAnalysisSnapshotFileOption';
import OutputOption from '../options/HeapAnalysisOutputOption';
import BaseAnalysis from '../BaseAnalysis';
import pluginUtils from '../PluginUtils';

Expand All @@ -27,7 +28,7 @@ export default class UnmountedFiberNodeAnalysis extends BaseAnalysis {

/** @internal */
getOptions(): BaseOption[] {
return [new SnapshotFileOption()];
return [new SnapshotFileOption(), new OutputOption()];
}

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ const name = analysis.getCommandName();
```

* **Source**:
* heap-analysis/src/plugins/DetachedDOMElementAnalysis.ts:20
* heap-analysis/src/plugins/DetachedDOMElementAnalysis.ts:21

___

### <a id="getdetachedelements"></a>**getDetachedElements**()

* **Returns**: `IHeapNode`[]
* **Source**:
* heap-analysis/src/plugins/DetachedDOMElementAnalysis.ts:47
* heap-analysis/src/plugins/DetachedDOMElementAnalysis.ts:48
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ const name = analysis.getCommandName();
```

* **Source**:
* heap-analysis/src/plugins/GlobalVariableAnalysis/GlobalVariableAnalysis.ts:24
* heap-analysis/src/plugins/GlobalVariableAnalysis/GlobalVariableAnalysis.ts:25
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ const name = analysis.getCommandName();
```

* **Source**:
* heap-analysis/src/plugins/ObjectFanoutAnalysis.ts:21
* heap-analysis/src/plugins/ObjectFanoutAnalysis.ts:22
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ const name = analysis.getCommandName();
```

* **Source**:
* heap-analysis/src/plugins/ObjectSizeAnalysis.ts:19
* heap-analysis/src/plugins/ObjectSizeAnalysis.ts:20
16 changes: 8 additions & 8 deletions website/docs/api/modules/heap_analysis_src.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class TestObject {}
```

* **Source**:
* heap-analysis/src/PluginUtils.ts:747
* heap-analysis/src/PluginUtils.ts:757

___

Expand All @@ -132,7 +132,7 @@ import {getFullHeapFromFile} from '@memlab/heap-analysis';
```

* **Source**:
* heap-analysis/src/PluginUtils.ts:544
* heap-analysis/src/PluginUtils.ts:554

___

Expand All @@ -144,7 +144,7 @@ ___
* `file`: `string`
* **Returns**: `Promise`<`IHeapSnapshot`\>
* **Source**:
* heap-analysis/src/PluginUtils.ts:575
* heap-analysis/src/PluginUtils.ts:585

___

Expand Down Expand Up @@ -191,7 +191,7 @@ The new heap analysis can also be used with [analyze](api_src.md#analyze), in th
[BrowserInteractionResultReader](../classes/api_src.BrowserInteractionResultReader.md).

* **Source**:
* heap-analysis/src/PluginUtils.ts:456
* heap-analysis/src/PluginUtils.ts:466

___

Expand Down Expand Up @@ -238,7 +238,7 @@ The new heap analysis can also be used with [analyze](api_src.md#analyze), in th
ascending order from [BrowserInteractionResultReader](../classes/api_src.BrowserInteractionResultReader.md).

* **Source**:
* heap-analysis/src/PluginUtils.ts:407
* heap-analysis/src/PluginUtils.ts:417

___

Expand Down Expand Up @@ -286,7 +286,7 @@ The new heap analysis can also be used with [analyze](api_src.md#analyze), in th
ascending order from [BrowserInteractionResultReader](../classes/api_src.BrowserInteractionResultReader.md).

* **Source**:
* heap-analysis/src/PluginUtils.ts:510
* heap-analysis/src/PluginUtils.ts:520

___

Expand Down Expand Up @@ -358,7 +358,7 @@ Each heap snapshot could be non-trivial in size, loading them all at once
may not be possible.

* **Source**:
* heap-analysis/src/PluginUtils.ts:652
* heap-analysis/src/PluginUtils.ts:662

___

Expand All @@ -383,4 +383,4 @@ import type {takeNodeFullHeap} from '@memlab/heap-analysis';
```

* **Source**:
* heap-analysis/src/PluginUtils.ts:567
* heap-analysis/src/PluginUtils.ts:577
5 changes: 5 additions & 0 deletions website/docs/cli/CLI-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ memlab analyze detached-DOM

**Options**:
* **`--snapshot`**: set file path of the heap snapshot under analysis
* **`--output`**: specify output format of the analysis (defaults to text)
* **`--help`**, **`-h`**: print helper text
* **`--verbose`**, **`-v`**: show more details
* **`--sc`**: set to continuous test mode
Expand All @@ -248,6 +249,7 @@ memlab analyze global-variable

**Options**:
* **`--snapshot`**: set file path of the heap snapshot under analysis
* **`--output`**: specify output format of the analysis (defaults to text)
* **`--help`**, **`-h`**: print helper text
* **`--verbose`**, **`-v`**: show more details
* **`--sc`**: set to continuous test mode
Expand Down Expand Up @@ -282,6 +284,7 @@ memlab analyze object-fanout

**Options**:
* **`--snapshot`**: set file path of the heap snapshot under analysis
* **`--output`**: specify output format of the analysis (defaults to text)
* **`--help`**, **`-h`**: print helper text
* **`--verbose`**, **`-v`**: show more details
* **`--sc`**: set to continuous test mode
Expand Down Expand Up @@ -330,6 +333,7 @@ memlab analyze object-size

**Options**:
* **`--snapshot`**: set file path of the heap snapshot under analysis
* **`--output`**: specify output format of the analysis (defaults to text)
* **`--help`**, **`-h`**: print helper text
* **`--verbose`**, **`-v`**: show more details
* **`--sc`**: set to continuous test mode
Expand Down Expand Up @@ -410,6 +414,7 @@ memlab analyze unmounted-fiber-node

**Options**:
* **`--snapshot`**: set file path of the heap snapshot under analysis
* **`--output`**: specify output format of the analysis (defaults to text)
* **`--help`**, **`-h`**: print helper text
* **`--verbose`**, **`-v`**: show more details
* **`--sc`**: set to continuous test mode
Expand Down

0 comments on commit 574e4e1

Please sign in to comment.