Skip to content

Commit f4fa111

Browse files
henrymercerCopilot
andcommitted
Share elapsed-time rounding for bundle downloads
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 89606bb commit f4fa111

5 files changed

Lines changed: 63 additions & 31 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 30 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/setup-codeql.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ export async function downloadCodeQLBundle(
11701170
...result,
11711171
statusReport: {
11721172
...result.statusReport,
1173-
totalDurationMs: Math.round(performance.now() - startTime),
1173+
totalDurationMs: util.durationMsSince(startTime),
11741174
perLanguageBundleFallback: true,
11751175
},
11761176
};

‎src/tools-download.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import * as tar from "./tar";
1717
import {
1818
asHTTPError,
1919
cleanUpPath,
20+
durationMsSince,
2021
getErrorMessage,
2122
getRequiredEnvParam,
2223
HTTPError,
@@ -91,7 +92,7 @@ export async function downloadAndExtract(
9192
logger,
9293
);
9394

94-
const totalDurationMs = Math.round(performance.now() - startTime);
95+
const totalDurationMs = durationMsSince(startTime);
9596
logger.info(
9697
`Finished downloading and extracting CodeQL bundle to ${dest} (${formatDuration(
9798
totalDurationMs,
@@ -124,7 +125,7 @@ export async function downloadAndExtract(
124125
authorization,
125126
headers,
126127
);
127-
const downloadDurationMs = Math.round(performance.now() - toolsDownloadStart);
128+
const downloadDurationMs = durationMsSince(toolsDownloadStart);
128129

129130
logger.info(
130131
`Finished downloading CodeQL bundle to ${archivedBundlePath} (${formatDuration(
@@ -144,7 +145,7 @@ export async function downloadAndExtract(
144145
tarVersion,
145146
logger,
146147
);
147-
extractionDurationMs = Math.round(performance.now() - extractionStart);
148+
extractionDurationMs = durationMsSince(extractionStart);
148149
logger.info(
149150
`Finished extracting CodeQL bundle to ${dest} (${formatDuration(
150151
extractionDurationMs,
@@ -157,7 +158,7 @@ export async function downloadAndExtract(
157158
return {
158159
downloadDurationMs,
159160
extractionDurationMs,
160-
totalDurationMs: Math.round(performance.now() - startTime),
161+
totalDurationMs: durationMsSince(startTime),
161162
};
162163
}
163164

‎src/util.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fs from "fs";
22
import * as os from "os";
33
import path from "path";
4+
import { performance } from "perf_hooks";
45

56
import * as core from "@actions/core";
67
import test from "ava";
@@ -508,6 +509,26 @@ test("joinAtMost - truncates list if array is > than limit", (t) => {
508509
t.false(result.includes("test6"));
509510
});
510511

512+
test.serial(
513+
"durationMsSince rounds elapsed milliseconds rather than the timestamps",
514+
(t) => {
515+
const startTime = 1000.25;
516+
const now = sinon.stub(performance, "now");
517+
for (const [endTime, expected] of [
518+
[1000.25, 0],
519+
[1000.74, 0],
520+
[1000.75, 1],
521+
[1001.74, 1],
522+
[1001.75, 2],
523+
[2000.74, 1000],
524+
[2000.75, 1001],
525+
]) {
526+
now.returns(endTime);
527+
t.is(util.durationMsSince(startTime), expected);
528+
}
529+
},
530+
);
531+
511532
test("Success creates a success result", (t) => {
512533
const result = new util.Success("test value");
513534
t.true(result.isSuccess());

‎src/util.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from "fs";
22
import * as fsPromises from "fs/promises";
33
import * as os from "os";
44
import * as path from "path";
5+
import { performance } from "perf_hooks";
56

67
import * as core from "@actions/core";
78
import * as io from "@actions/io";
@@ -681,6 +682,11 @@ export async function bundleDb(
681682
return databaseBundlePath;
682683
}
683684

685+
/** Returns the elapsed milliseconds, rounded, since a `performance.now()` timestamp. */
686+
export function durationMsSince(startTime: number): number {
687+
return Math.round(performance.now() - startTime);
688+
}
689+
684690
/**
685691
* @param milliseconds time to delay
686692
* @param opts options

0 commit comments

Comments
 (0)