Skip to content
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ GET /users;db.query;db.fetch 800
50us 1 2.8% GET /health
```

A name that has some self time but less than 0.05% of the total shows `<0.1%` in the SHARE column, so it is not mistaken for a name with no self time (that one shows `0.0%`). For a root `job` of 100000us with two child spans, `edge` of 50us and `under` of 49us:

```
SELF COUNT SHARE NAME
99901us 1 99.9% job
50us 1 0.1% edge
49us 1 <0.1% under
```

`spanflame check trace.jsonl` prints one line per problem and exits 1, or a short ok line and exits 0.

## Input format
Expand Down
3 changes: 3 additions & 0 deletions src/top.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ function formatShare(self: number, total: bigint): string {
return "0.0%";
}
const tenths = (2n * BigInt(self) * 1000n + total) / (2n * total);
if (tenths === 0n && self > 0) {
return "<0.1%";
}
return `${tenths / 10n}.${tenths % 10n}%`;
}

Expand Down
67 changes: 67 additions & 0 deletions tests/top-share.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { describe, expect, test } from "bun:test";
import { buildForest } from "../src/forest";
import { parseSpans } from "../src/records";
import { renderTop } from "../src/top";

function sp(id: string, parent: string | null, name: string, start: number, end: number): string {
const trace = "t";
if (parent === null) {
return JSON.stringify({ trace, id, name, start, end });
}
return JSON.stringify({ trace, id, parent, name, start, end });
}

function top(rows: string[]): string {
const traces = buildForest(parseSpans(rows.join("\n"), "f.jsonl"));
return renderTop(traces, { unit: "us", limit: 10 });
}

function text(rows: string[]): string {
return rows.map((row) => `${row}\n`).join("");
}

describe("top shares that round to zero", () => {
test("a share below one tenth of a percent shows <0.1%", () => {
const out = top([
sp("a", null, "job", 0, 100000),
sp("b", "a", "edge", 100, 150),
sp("c", "a", "under", 200, 249),
]);
expect(out).toBe(
text([
" SELF COUNT SHARE NAME",
"99901us 1 99.9% job",
" 50us 1 0.1% edge",
" 49us 1 <0.1% under",
]),
);
});

test("a single microsecond of a large total shows <0.1%", () => {
const out = top([sp("a", null, "job", 0, 100000), sp("b", "a", "blip", 10, 11)]);
expect(out).toBe(
text([
" SELF COUNT SHARE NAME",
"99999us 1 100.0% job",
" 1us 1 <0.1% blip",
]),
);
});

test("a name without self time still shows 0.0%", () => {
const out = top([sp("a", null, "wrap", 0, 100), sp("b", "a", "leaf", 0, 100)]);
expect(out).toBe(
text([
" SELF COUNT SHARE NAME",
"100us 1 100.0% leaf",
" 0us 1 0.0% wrap",
]),
);
});

test("a zero total shows 0.0% and not <0.1%", () => {
expect(top([sp("a", null, "z", 5, 5)])).toBe(
text(["SELF COUNT SHARE NAME", " 0us 1 0.0% z"]),
);
});
});
Loading