Skip to content

Commit

Permalink
fix: analysis total time sum unique stacks
Browse files Browse the repository at this point in the history
Collect the stacks and only sum the unique entries e.g
If we have an analysis like this

methodA: 5s
methodB: 3s
methodC: 2s

Sibling Methods
The three methods are at the same level directly called from EXECUTION_STARTED.
All 3 should be summed as they are in different stacks.

Total: 10s

Child Methods

methodB is a child of methodA and methodC is a sibling of methodA.
we first add methodA to the total but the total for methodB has already been accounted by including methodA so we skip it and any other children of methodA.
methodC is in a unique stack and needs to be added to the total.

Total: 7s
  • Loading branch information
lukecotter committed Dec 2, 2024
1 parent 512b3a0 commit f979a81
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
8 changes: 6 additions & 2 deletions log-viewer/modules/components/AnalysisView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { vscodeMessenger } from '../services/VSCodeExtensionMessenger.js';
import { globalStyles } from '../styles/global.styles.js';
import './skeleton/GridSkeleton.js';

import { callStackSum } from '../components/analysis-view/column-calcs/CallStackSum.js';
import { Find, formatter } from '../components/calltree-view/module/Find.js';
import { RowNavigation } from '../datagrid/module/RowNavigation.js';

Expand Down Expand Up @@ -191,7 +192,7 @@ export class AnalysisView extends LitElement {
const methodMap: Map<string, Metric> = new Map();

addNodeToMap(methodMap, rootMethod);
const metricList = [...methodMap.values()];
const metricList = Array.from(methodMap.values());

const headerMenu = [
{
Expand Down Expand Up @@ -328,7 +329,7 @@ export class AnalysisView extends LitElement {
},
accessorDownload: NumberAccessor,
bottomCalcFormatter: progressFormatter,
bottomCalc: 'max',
bottomCalc: callStackSum,
bottomCalcFormatterParams: { precision: 3, totalValue: rootMethod.duration.total },
},
{
Expand Down Expand Up @@ -377,11 +378,13 @@ export class Metric {
totalTime = 0;
selfTime = 0;
namespace;
nodes: TimedNode[] = [];

constructor(node: TimedNode) {
this.name = node.text;
this.type = node.type;
this.namespace = node.namespace;
this.nodes.push(node);
}
}

Expand All @@ -398,6 +401,7 @@ function addNodeToMap(map: Map<string, Metric>, node: TimedNode, key?: string) {
++metric.count;
metric.totalTime += node.duration.total;
metric.selfTime += node.duration.self;
metric.nodes.push(node);
}

children.forEach(function (child) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { LogLine, TimedNode } from '../../../parsers/ApexLogParser';
import { type Metric } from '../../AnalysisView.js';

export function callStackSum(values: number[], data: Metric[], _calcParams: unknown) {
// All filtered debug logs nodes
const includedNodes = new Set<TimedNode>();
data.forEach((row) => {
row.nodes.forEach((node) => {
includedNodes.add(node);
});
});

let total = 0;
data.forEach((row, i) => {
// All the parents (to root) of the log nodes for this row.
const parents = _getParentNodes(row.nodes);
// If any of these parent are else where in the (filtered) stacks do not include in the sum.
// This value will be included when the parent is summed e.g m1 -> m2 -> m3 (no need to include m2 + m3)
if (!_containsAny(parents, includedNodes)) {
total += values[i] ?? 0;
}
});

return total;
}

const _getParentNodes = (nodes: TimedNode[]) => {
const parents = new Set<LogLine>();
nodes.forEach((node) => {
let parent = node.parent;
while (parent && !parents.has(parent)) {
parents.add(parent);
parent = parent.parent;
}
});
return parents;
};

const _containsAny = (target: Set<LogLine>, toCheck: Set<LogLine>) => {
for (const t of target) {
if (toCheck.has(t)) {
return true;
}
}
return false;
};

0 comments on commit f979a81

Please sign in to comment.