Skip to content

Commit 0311171

Browse files
committed
fix: parse ps cpu time formats
1 parent 5393240 commit 0311171

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

extensions/qa-lab/src/process-tree-cpu.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ import {
1010
describe("process tree CPU helpers", () => {
1111
it("parses ps CPU time strings", () => {
1212
expect(parsePsCpuTimeMs("00:01")).toBe(1_000);
13+
expect(parsePsCpuTimeMs("00:00.12")).toBe(120);
1314
expect(parsePsCpuTimeMs("01:02")).toBe(62_000);
14-
expect(parsePsCpuTimeMs("01:02:03")).toBe(3_723_000);
15+
expect(parsePsCpuTimeMs("01:02:03.45")).toBe(3_723_450);
16+
expect(parsePsCpuTimeMs("1-02:03:04.5")).toBe(93_784_500);
1517
});
1618

1719
it("rejects malformed ps CPU time strings", () => {
1820
expect(parsePsCpuTimeMs("")).toBeNull();
1921
expect(parsePsCpuTimeMs("nope")).toBeNull();
22+
expect(parsePsCpuTimeMs("1::02")).toBeNull();
23+
expect(parsePsCpuTimeMs("1-02:03")).toBeNull();
24+
expect(parsePsCpuTimeMs("01:60")).toBeNull();
25+
expect(parsePsCpuTimeMs("01:02:60")).toBeNull();
2026
expect(parsePsCpuTimeMs("1:2:3:4")).toBeNull();
2127
});
2228

extensions/qa-lab/src/process-tree-cpu.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,35 @@ function parseNonNegativeNumber(value: unknown): number | null {
4141
}
4242

4343
export function parsePsCpuTimeMs(raw: string): number | null {
44-
const parts = raw.trim().split(":").map(Number);
45-
if (parts.some((part) => !Number.isFinite(part) || part < 0)) {
44+
const match = raw.trim().match(/^(?:(\d+)-)?(\d+):(\d{2}(?:\.\d+)?)(?::(\d{2}(?:\.\d+)?))?$/u);
45+
if (!match) {
4646
return null;
4747
}
48-
if (parts.length === 2) {
49-
return Math.round((parts[0] * 60 + parts[1]) * 1000);
48+
const [, daysRaw, firstRaw, secondRaw, thirdRaw] = match;
49+
if (daysRaw !== undefined && thirdRaw === undefined) {
50+
return null;
51+
}
52+
const days = daysRaw === undefined ? 0 : Number(daysRaw);
53+
const first = Number(firstRaw);
54+
const second = Number(secondRaw);
55+
const third = thirdRaw === undefined ? 0 : Number(thirdRaw);
56+
const values = [days, first, second, third];
57+
if (values.some((part) => !Number.isFinite(part) || part < 0)) {
58+
return null;
59+
}
60+
if (thirdRaw !== undefined && !Number.isInteger(second)) {
61+
return null;
62+
}
63+
if (second >= 60 || (thirdRaw !== undefined && third >= 60)) {
64+
return null;
65+
}
66+
if (daysRaw !== undefined && thirdRaw !== undefined) {
67+
return Math.round((days * 24 * 60 * 60 + first * 60 * 60 + second * 60 + third) * 1000);
5068
}
51-
if (parts.length === 3) {
52-
return Math.round((parts[0] * 60 * 60 + parts[1] * 60 + parts[2]) * 1000);
69+
if (thirdRaw !== undefined) {
70+
return Math.round((first * 60 * 60 + second * 60 + third) * 1000);
5371
}
54-
return null;
72+
return Math.round((first * 60 + second) * 1000);
5573
}
5674

5775
export function parsePsRssBytes(raw: string): number | null {

0 commit comments

Comments
 (0)