Skip to content

Commit 258d6c5

Browse files
committed
chore: polyfill performance module and track debugging
1 parent 0c9bb55 commit 258d6c5

7 files changed

+43
-10
lines changed

lib/common/decorators.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export function performanceLog(injector?: IInjector): any {
101101
const result = originalMethod.apply(this, args);
102102
const resolvedPromise = Promise.resolve(result);
103103
let end;
104-
104+
105105
if (resolvedPromise !== result) {
106106
end = performanceService.now();
107107
performanceService.processExecutionData(trackName, start, end, args);
@@ -115,13 +115,12 @@ export function performanceLog(injector?: IInjector): any {
115115
end = performanceService.now();
116116
performanceService.processExecutionData(trackName, start, end, args);
117117
});
118-
119118
}
120-
119+
121120
return result;
122121
}
123-
}
124-
descriptor.value = tempObject[originalMethod.name]
122+
};
123+
descriptor.value = tempObject[originalMethod.name];
125124

126125
// used to get parameter names in hooks decorator
127126
descriptor.value.toString = () => {

lib/declarations.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ interface INodePackageManager {
7676
}
7777

7878
interface IPerformanceService {
79+
// Will process the data based on the command opitons (--performance flag and user-reporting setting)
7980
processExecutionData(methodInfo: string, startTime: number, endTime: number, args: any[]): void;
81+
82+
// Will return a reference time in milliseconds
8083
now(): number;
8184
}
8285

lib/services/android-device-debug-service.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { sleep } from "../common/helpers";
22
import { DebugServiceBase } from "./debug-service-base";
33
import { LiveSyncPaths } from "../common/constants";
4+
import { performanceLog } from "../common/decorators";
45

56
export class AndroidDeviceDebugService extends DebugServiceBase implements IDeviceDebugService {
67
private _packageName: string;
@@ -24,6 +25,7 @@ export class AndroidDeviceDebugService extends DebugServiceBase implements IDevi
2425
this.deviceIdentifier = device.deviceInfo.identifier;
2526
}
2627

28+
@performanceLog()
2729
public async debug(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
2830
this._packageName = debugData.applicationIdentifier;
2931
const result = this.device.isEmulator
@@ -97,6 +99,7 @@ export class AndroidDeviceDebugService extends DebugServiceBase implements IDevi
9799
return this.device.adb.executeCommand(["forward", `tcp:${local}`, `localabstract:${remote}`]);
98100
}
99101

102+
@performanceLog()
100103
private async debugOnDevice(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
101104
let packageFile = "";
102105

@@ -119,6 +122,7 @@ export class AndroidDeviceDebugService extends DebugServiceBase implements IDevi
119122
return deviceActionResult[0].result;
120123
}
121124

125+
@performanceLog()
122126
private async debugCore(device: Mobile.IAndroidDevice, packageFile: string, appData: Mobile.IApplicationData, debugOptions: IDebugOptions): Promise<string> {
123127
if (debugOptions.stop) {
124128
await this.removePortForwarding();

lib/services/debug-service.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { EventEmitter } from "events";
44
import { CONNECTION_ERROR_EVENT_NAME, DebugCommandErrors } from "../constants";
55
import { CONNECTED_STATUS } from "../common/constants";
66
import { DebugTools, TrackActionNames } from "../constants";
7+
import { performanceLog } from "../common/decorators";
78

89
export class DebugService extends EventEmitter implements IDebugService {
910
private _platformDebugServices: IDictionary<IDeviceDebugService>;
@@ -17,6 +18,7 @@ export class DebugService extends EventEmitter implements IDebugService {
1718
this._platformDebugServices = {};
1819
}
1920

21+
@performanceLog()
2022
public async debug(debugData: IDebugData, options: IDebugOptions): Promise<IDebugInformation> {
2123
const device = this.$devicesService.getDeviceByIdentifier(debugData.deviceIdentifier);
2224

lib/services/ios-device-debug-service.ts

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getPidFromiOSSimulatorLogs } from "../common/helpers";
88
const inspectorAppName = "NativeScript Inspector.app";
99
const inspectorNpmPackageName = "tns-ios-inspector";
1010
const inspectorUiDir = "WebInspectorUI/";
11+
import { performanceLog } from "../common/decorators";
1112

1213
export class IOSDeviceDebugService extends DebugServiceBase implements IDeviceDebugService {
1314
private _lldbProcess: ChildProcess;
@@ -38,6 +39,7 @@ export class IOSDeviceDebugService extends DebugServiceBase implements IDeviceDe
3839
return "ios";
3940
}
4041

42+
@performanceLog()
4143
public async debug(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
4244
this.validateOptions(debugOptions);
4345

@@ -75,6 +77,7 @@ export class IOSDeviceDebugService extends DebugServiceBase implements IDeviceDe
7577
}
7678
}
7779

80+
@performanceLog()
7881
private async startDeviceLogProcess(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
7982
if (debugOptions.justlaunch) {
8083
// No logs should be printed on console when `--justlaunch` option is passed.
@@ -95,6 +98,7 @@ export class IOSDeviceDebugService extends DebugServiceBase implements IDeviceDe
9598
await this.device.openDeviceLogStream({ predicate: IOS_LOG_PREDICATE });
9699
}
97100

101+
@performanceLog()
98102
private async startAppOnSimulator(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
99103
const args = debugOptions.debugBrk ? "--nativescript-debug-brk" : "--nativescript-debug-start";
100104
const launchResult = await this.$iOSEmulatorServices.runApplicationOnEmulator(debugData.pathToAppPackage, {
@@ -112,6 +116,7 @@ export class IOSDeviceDebugService extends DebugServiceBase implements IDeviceDe
112116
this.startAppDebuggerOnSimulator(pid);
113117
}
114118

119+
@performanceLog()
115120
private async startAppOnDevice(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
116121
const runOptions: IRunPlatformOptions = {
117122
device: this.deviceIdentifier,
@@ -148,6 +153,7 @@ export class IOSDeviceDebugService extends DebugServiceBase implements IDeviceDe
148153
}
149154
}
150155

156+
@performanceLog()
151157
private async wireDebuggerClient(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
152158
if ((debugOptions.inspector || !debugOptions.client) && this.$hostInfo.isDarwin) {
153159
return await this.setupTcpAppDebugProxy(debugData, debugOptions);
@@ -181,6 +187,7 @@ export class IOSDeviceDebugService extends DebugServiceBase implements IDeviceDe
181187
return null;
182188
}
183189

190+
@performanceLog()
184191
private async openAppInspector(fileDescriptor: string, debugData: IDebugData, debugOptions: IDebugOptions): Promise<ChildProcess> {
185192
if (debugOptions.client) {
186193
const inspectorPath = await this.$packageInstallationManager.getInspectorFromCache(inspectorNpmPackageName, debugData.projectDir);

lib/services/livesync/livesync-service.ts

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DeviceTypes, DeviceDiscoveryEventNames, HmrConstants } from "../../comm
88
import { cache } from "../../common/decorators";
99
import * as constants from "../../constants";
1010
import { PreviewAppLiveSyncEvents } from "./playground/preview-app-constants";
11+
import { performanceLog } from "../../common/decorators";
1112

1213
const deviceDescriptorPrimaryKey = "identifier";
1314

@@ -156,6 +157,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
156157
}
157158
}
158159

160+
@performanceLog()
159161
private async refreshApplication(projectData: IProjectData, liveSyncResultInfo: ILiveSyncResultInfo, debugOpts?: IDebugOptions, outputPath?: string): Promise<void | IDebugInformation> {
160162
const deviceDescriptor = this.getDeviceDescriptor(liveSyncResultInfo.deviceAppData.device.deviceInfo.identifier, projectData.projectDir);
161163

@@ -195,6 +197,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
195197
});
196198
}
197199

200+
@performanceLog()
198201
private async refreshApplicationWithDebug(projectData: IProjectData, liveSyncResultInfo: ILiveSyncResultInfo, debugOptions: IDebugOptions, outputPath?: string): Promise<IDebugInformation> {
199202
const deviceAppData = liveSyncResultInfo.deviceAppData;
200203
debugOptions = debugOptions || {};
@@ -292,6 +295,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
292295
return _.find(deviceDescriptors, d => d.identifier === deviceIdentifier);
293296
}
294297

298+
@performanceLog()
295299
private async enableDebuggingCoreWithoutWaitingCurrentAction(deviceOption: IEnableDebuggingDeviceOptions, debuggingAdditionalOptions: IDebuggingAdditionalOptions): Promise<IDebugInformation> {
296300
const currentDeviceDescriptor = this.getDeviceDescriptor(deviceOption.deviceIdentifier, debuggingAdditionalOptions.projectDir);
297301
if (!currentDeviceDescriptor) {

lib/services/performance-service.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
import { TrackActionNames, AnalyticsEventLabelDelimiter } from "../constants";
1+
import { TrackActionNames } from "../constants";
22
const EOL = require("os").EOL;
33
import { getFormattedDate } from "../common/helpers";
4-
const { performance } = require('perf_hooks');
4+
import * as semver from "semver";
55

66
export class PerformanceService implements IPerformanceService {
77
public static LOG_MESSAGE_TEMPLATE = `Execution of method "%s" took %s ms.`;
88
public static FAIL_LOG_MESSAGE_TEMPLATE = `Failed to log pefromance data for method %s.`;
9+
private static MIN_NODE_PERFORMANCE_MODULE_VERSION = "8.5.0";
10+
private performance: {now(): number} = null;
911

1012
constructor(
1113
private $options: IOptions,
1214
private $fs: IFileSystem,
1315
private $logger: ILogger,
1416
private $analyticsService: IAnalyticsService
15-
) { }
17+
) {
18+
if (this.isPerformanceModuleSupported()) {
19+
this.performance = require("perf_hooks").performance;
20+
}
21+
}
1622

1723
public processExecutionData(methodInfo: string, startTime: number, endTime: number, args: any[]): void {
1824
const executionTime = Math.floor(endTime - startTime);
@@ -27,7 +33,15 @@ export class PerformanceService implements IPerformanceService {
2733
}
2834

2935
public now(): number {
30-
return performance.now();
36+
if (this.isPerformanceModuleSupported()) {
37+
return this.performance.now();
38+
} else {
39+
return new Date().getTime();
40+
}
41+
}
42+
43+
private isPerformanceModuleSupported(): boolean {
44+
return semver.gte(process.version, PerformanceService.MIN_NODE_PERFORMANCE_MODULE_VERSION);
3145
}
3246

3347
private trackAnalyticsData(methodInfo: string, executionTime: number): void {
@@ -68,7 +82,7 @@ export class PerformanceService implements IPerformanceService {
6882
executionTime,
6983
timestamp: getFormattedDate(),
7084
methodArgs: JSON.parse(methodArgs)
71-
}
85+
};
7286

7387
try {
7488
this.$fs.appendFile(filePath, `${JSON.stringify(info)}${EOL}`);

0 commit comments

Comments
 (0)