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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Run it with `bun run src/main.ts tree FILE`. There is no build step.
```
usage: spanflame tree [--trace ID] [--unit us|ms] [--min-total N] [--max-depth N] FILE
spanflame folded [--trace ID] FILE
spanflame top [--trace ID] [--unit us|ms] [--limit N] FILE
spanflame top [--trace ID] [--unit us|ms] [--limit N] [--min-self N] FILE
spanflame check [--trace ID] FILE
```

Expand Down Expand Up @@ -100,6 +100,15 @@ A name that has some self time but less than 0.05% of the total shows `<0.1%` in
49us 1 <0.1% under
```

`--min-self N` (a whole number from 0 to 1000000000 microseconds, `top` only) leaves out the names whose self time is below N; a name with exactly N stays. The SHARE column keeps the total of all names, so the shares do not change, and `--limit` counts the names that stay. `spanflame top --min-self 300 trace.jsonl` prints:

```
SELF COUNT SHARE NAME
800us 2 44.4% db.fetch
400us 1 22.2% GET /users
350us 2 19.4% db.query
```

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

## Input format
Expand Down
27 changes: 26 additions & 1 deletion src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ export interface RunArgs {
minTotal: number;
maxDepth?: number;
limit: number;
minSelf?: number;
}

const COMMANDS: readonly string[] = ["tree", "folded", "top", "check"];
const OPTIONS = ["--trace", "--unit", "--min-total", "--max-depth", "--limit"] as const;
const OPTIONS = [
"--trace",
"--unit",
"--min-total",
"--max-depth",
"--limit",
"--min-self",
] as const;
const NUMBER = /^(0|[1-9][0-9]*)$/;

function parseNumber(value: string, min: number, max: number): number | null {
Expand Down Expand Up @@ -104,6 +112,17 @@ export function parseArgs(args: string[]): HelpArgs | RunArgs {
}
limit = n;
}
const minSelfValue = values.get("--min-self");
let minSelf = 0;
if (minSelfValue !== undefined) {
const n = parseNumber(minSelfValue, 0, 1000000000);
if (n === null) {
throw new UsageError(
`invalid --min-self "${minSelfValue}" (expected a whole number from 0 to 1000000000)`,
);
}
minSelf = n;
}
if (unitValue !== undefined && (command === "folded" || command === "check")) {
throw new UsageError(`--unit is not valid for ${command}`);
}
Expand All @@ -116,6 +135,9 @@ export function parseArgs(args: string[]): HelpArgs | RunArgs {
if (limitValue !== undefined && command !== "top") {
throw new UsageError("--limit is only valid for top");
}
if (minSelfValue !== undefined && command !== "top") {
throw new UsageError("--min-self is only valid for top");
}
const file = positionals[0];
if (file === undefined) {
throw new UsageError("missing FILE");
Expand All @@ -134,6 +156,9 @@ export function parseArgs(args: string[]): HelpArgs | RunArgs {
if (maxDepth !== undefined) {
result.maxDepth = maxDepth;
}
if (minSelfValue !== undefined) {
result.minSelf = minSelf;
}
const trace = values.get("--trace");
if (trace !== undefined) {
result.trace = trace;
Expand Down
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface Io {
const USAGE = [
"usage: spanflame tree [--trace ID] [--unit us|ms] [--min-total N] [--max-depth N] FILE",
" spanflame folded [--trace ID] FILE",
" spanflame top [--trace ID] [--unit us|ms] [--limit N] FILE",
" spanflame top [--trace ID] [--unit us|ms] [--limit N] [--min-self N] FILE",
" spanflame check [--trace ID] FILE",
]
.map((line) => `${line}\n`)
Expand Down Expand Up @@ -81,7 +81,7 @@ function execute(args: string[], io: Io): number {
} else if (parsed.command === "folded") {
view = renderFolded(traces);
} else {
view = renderTop(traces, { unit: parsed.unit, limit: parsed.limit });
view = renderTop(traces, { unit: parsed.unit, limit: parsed.limit, minSelf: parsed.minSelf });
}
if (view.length > 0) {
io.out(view);
Expand Down
5 changes: 4 additions & 1 deletion src/top.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Node, Trace } from "./types";
export interface TopOptions {
unit: "us" | "ms";
limit: number;
minSelf?: number;
}

interface Row {
Expand Down Expand Up @@ -51,7 +52,9 @@ export function renderTop(traces: Trace[], options: TopOptions): string {
return a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
});
const cells: string[][] = [["SELF", "COUNT", "SHARE", "NAME"]];
for (const row of rows.slice(0, options.limit)) {
const minSelf = options.minSelf ?? 0;
const kept = rows.filter((row) => row.self >= minSelf);
for (const row of kept.slice(0, options.limit)) {
cells.push([
formatDuration(row.self, options.unit),
String(row.count),
Expand Down
2 changes: 1 addition & 1 deletion tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe("cli help and usage", () => {
out: lines(
"usage: spanflame tree [--trace ID] [--unit us|ms] [--min-total N] [--max-depth N] FILE",
" spanflame folded [--trace ID] FILE",
" spanflame top [--trace ID] [--unit us|ms] [--limit N] FILE",
" spanflame top [--trace ID] [--unit us|ms] [--limit N] [--min-self N] FILE",
" spanflame check [--trace ID] FILE",
),
});
Expand Down
2 changes: 1 addition & 1 deletion tests/docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function fenced(lines: string[]): string {
const usage = fenced([
"usage: spanflame tree [--trace ID] [--unit us|ms] [--min-total N] [--max-depth N] FILE",
" spanflame folded [--trace ID] FILE",
" spanflame top [--trace ID] [--unit us|ms] [--limit N] FILE",
" spanflame top [--trace ID] [--unit us|ms] [--limit N] [--min-self N] FILE",
" spanflame check [--trace ID] FILE",
]);

Expand Down
144 changes: 144 additions & 0 deletions tests/top-min-self.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { describe, expect, test } from "bun:test";
import { parseArgs, type RunArgs } from "../src/args";
import { run } from "../src/cli";
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 });
}

const ROWS = [
sp("a", null, "job", 0, 1000),
sp("b", "a", "big", 0, 600),
sp("c", "a", "mid", 600, 800),
sp("d", "a", "small", 800, 850),
sp("e", "a", "small", 850, 900),
];

function top(minSelf: number | undefined, limit = 10): string {
const traces = buildForest(parseSpans(ROWS.join("\n"), "f.jsonl"));
return renderTop(traces, { unit: "us", limit, minSelf });
}

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

function exec(args: string[]): { code: number; out: string; err: string } {
let out = "";
let err = "";
const code = run(args, {
readStdin: () => new TextEncoder().encode(ROWS.join("\n")),
readFile: () => new TextEncoder().encode(ROWS.join("\n")),
out: (value) => {
out += value;
},
err: (value) => {
err += value;
},
});
return { code, out, err };
}

describe("top --min-self", () => {
test("without the option every name is listed", () => {
expect(top(undefined)).toBe(
text([
" SELF COUNT SHARE NAME",
"600us 1 60.0% big",
"200us 1 20.0% mid",
"100us 1 10.0% job",
"100us 2 10.0% small",
]),
);
});

test("a name below the minimum is left out and the shares stay as they are", () => {
expect(top(150)).toBe(
text([" SELF COUNT SHARE NAME", "600us 1 60.0% big", "200us 1 20.0% mid"]),
);
});

test("a name whose self time equals the minimum stays", () => {
expect(top(200)).toBe(top(150));
expect(top(100)).toBe(top(undefined));
expect(top(201)).toBe(text([" SELF COUNT SHARE NAME", "600us 1 60.0% big"]));
});

test("the minimum counts the self time of a name, not of one span", () => {
expect(top(100)).toContain("100us 2 10.0% small");
expect(top(101)).not.toContain("small");
});

test("a minimum above every name leaves the heading only", () => {
expect(top(1000000000)).toBe("SELF COUNT SHARE NAME\n");
});

test("the limit counts the names that stay", () => {
expect(top(150, 1)).toBe(text([" SELF COUNT SHARE NAME", "600us 1 60.0% big"]));
expect(top(100, 3)).toBe(
text([
" SELF COUNT SHARE NAME",
"600us 1 60.0% big",
"200us 1 20.0% mid",
"100us 1 10.0% job",
]),
);
});

test("the command line passes the value to top", () => {
const result = exec(["top", "--min-self", "250", "f.jsonl"]);
expect(result).toEqual({
code: 0,
err: "",
out: text([" SELF COUNT SHARE NAME", "600us 1 60.0% big"]),
});
expect(exec(["top", "--min-self=250", "f.jsonl"])).toEqual(result);
});

test("the minimum is not scaled by --unit", () => {
const result = exec(["top", "--unit", "ms", "--min-self", "250", "f.jsonl"]);
expect(result.out).toBe(text([" SELF COUNT SHARE NAME", "0.600ms 1 60.0% big"]));
});
});

describe("--min-self arguments", () => {
test("the default is none and a good value is read", () => {
expect((parseArgs(["top", "f"]) as RunArgs).minSelf).toBeUndefined();
expect((parseArgs(["top", "--min-self", "0", "f"]) as RunArgs).minSelf).toBe(0);
expect((parseArgs(["top", "--min-self", "1000000000", "f"]) as RunArgs).minSelf).toBe(
1000000000,
);
});

test("a bad value is a usage error", () => {
for (const value of ["-1", "1000000001", "1.5", "x", "007", ""]) {
const result = exec(["top", "--min-self", value, "f.jsonl"]);
expect(result.code).toBe(2);
expect(result.out).toBe("");
expect(result.err).toBe(
`spanflame: invalid --min-self "${value}" (expected a whole number from 0 to 1000000000)\n`,
);
}
});

test("a missing value is a usage error", () => {
expect(exec(["top", "f.jsonl", "--min-self"]).err).toBe(
"spanflame: --min-self needs a value\n",
);
});

test("the option belongs to top only", () => {
for (const command of ["tree", "folded", "check"]) {
const result = exec([command, "--min-self", "5", "f.jsonl"]);
expect(result.code).toBe(2);
expect(result.err).toBe("spanflame: --min-self is only valid for top\n");
}
});
});
Loading