Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/log/common/logExecTime.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { isCancellationError } from '../../../util/vs/base/common/errors';
7
import { StopWatch } from '../../../util/vs/base/common/stopwatch';
8
import { ILogService } from './logService';
9
10
type MeasureCallBack<R> = (time: number, status: 'success' | 'failed' | 'cancelled', result: R | undefined) => void;
11
12
/**
13
* Helper that collects how long a block of code takes to execute.
14
*/
15
16
export async function measureExecTime<R>(fn: () => PromiseLike<R>, cb: MeasureCallBack<R>): Promise<R> {
17
const sw = new StopWatch();
18
try {
19
const result = await fn();
20
cb(sw.elapsed(), 'success', result);
21
return result;
22
} catch (error) {
23
cb(sw.elapsed(), isCancellationError(error) ? 'cancelled' : 'failed', undefined);
24
throw error;
25
}
26
}
27
28
/**
29
* Helper that logs how long a block of code takes to execute.
30
*/
31
export async function logExecTime<R>(logService: ILogService, name: string, fn: () => PromiseLike<R>, measureCb?: MeasureCallBack<R>): Promise<R> {
32
return measureExecTime(() => {
33
logService.trace(`${name} started`);
34
return fn();
35
}, (time, status, result) => {
36
logService.trace(`${name} ${status}. Elapsed ${time}`);
37
measureCb?.(time, status, result);
38
});
39
}
40
41
/**
42
* Decorator that adds logging for how long the method takes to execute.
43
*/
44
export function LogExecTime<T>(
45
getLogService: (self: T) => ILogService,
46
logName: string,
47
measureCb?: (this: T, time: number, status: 'success' | 'failed' | 'cancelled') => void,
48
) {
49
return function (target: T, propertyKey: string, descriptor: PropertyDescriptor) {
50
const originalMethod = descriptor.value;
51
let idPool = 0;
52
descriptor.value = async function (this: T, ...args: any[]) {
53
const id = idPool++;
54
const logService = getLogService(this);
55
return logExecTime(logService, `${logName}#${id}`, () => originalMethod.apply(this, args), measureCb?.bind(this));
56
};
57
58
return descriptor;
59
};
60
}
61
62
63
/**
64
* Decorator that adds a callback about how long an async method takes to execute.
65
*/
66
export function MeasureExecTime<T>(cb: (this: T, time: number, status: 'success' | 'failed' | 'cancelled') => void) {
67
return function (target: T, propertyKey: string, descriptor: PropertyDescriptor) {
68
const originalMethod = descriptor.value;
69
descriptor.value = function (this: T, ...args: any[]) {
70
return measureExecTime(() => originalMethod.apply(this, args), cb.bind(this));
71
};
72
return descriptor;
73
};
74
}
75
76