MCPcopy Create free account
hub / github.com/firebase/firebase-tools / profiler

Function profiler

src/profiler.ts:21–122  ·  view source on GitHub ↗
(options: any)

Source from the content-addressed store, hash-verified

19 */
20// eslint-disable-next-line @typescript-eslint/no-explicit-any
21export async function profiler(options: any): Promise<unknown> {
22 const origin = realtimeOriginOrEmulatorOrCustomUrl(options.instanceDetails.databaseUrl);
23 const url = new URL(utils.getDatabaseUrl(origin, options.instance, "/.settings/profile.json?"));
24 const rl = readline.createInterface({ input: process.stdin });
25
26 const fileOut = !!options.output;
27 const tmpFile = tmp.tmpNameSync();
28 const tmpStream = fs.createWriteStream(tmpFile);
29 const outStream = fileOut ? fs.createWriteStream(options.output) : process.stdout;
30 const spinner = ora({
31 text: "0 operations recorded. Press [enter] to stop",
32 color: "yellow",
33 });
34 const outputFormat = options.raw ? "RAW" : options.parent.json ? "JSON" : "TXT";
35
36 // Controller is used to stop the request stream when the user stops the
37 // command or the duration passes.
38 const controller = new AbortController();
39
40 const generateReport = (): Promise<void> => {
41 rl.close();
42 spinner.stop();
43 controller.abort();
44 const dataFile = options.input || tmpFile;
45 const reportOptions: ProfileReportOptions = {
46 format: outputFormat,
47 isFile: fileOut,
48 isInput: !!options.input,
49 collapse: options.collapse,
50 };
51 const report = new ProfileReport(dataFile, outStream, reportOptions);
52 return report.generate();
53 };
54
55 if (options.input) {
56 // If there is input, don't contact the server.
57 return generateReport();
58 }
59
60 const c = new Client({ urlPrefix: url.origin, auth: true });
61 const res = await c.request<unknown, NodeJS.ReadableStream>({
62 method: "GET",
63 path: url.pathname,
64 responseType: "stream",
65 resolveOnHTTPError: true,
66 headers: {
67 Accept: "text/event-stream",
68 },
69 signal: controller.signal,
70 });
71
72 if (res.response.status >= 400) {
73 throw responseToError(res.response, await res.response.text());
74 }
75
76 if (!options.duration) {
77 spinner.start();
78 }

Callers 1

Calls 6

responseToErrorFunction · 0.90
generateReportFunction · 0.85
onMethod · 0.80
startMethod · 0.65
toStringMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…