|
| 1 | +import { IFileSystem } from "../common/declarations"; |
| 2 | +import { cache } from "../common/decorators"; |
| 3 | +import { injector } from "../common/yok"; |
| 4 | +import { IProjectConfigService } from "../definitions/project"; |
| 5 | +import * as path from "path"; |
| 6 | +import { originalProcessOn } from "../nativescript-cli"; |
| 7 | + |
| 8 | +export interface ITimelineProfilerService { |
| 9 | + processLogData(data: string, deviceIdentifier: string): void; |
| 10 | +} |
| 11 | + |
| 12 | +const TIMELINE_LOG_RE = /Timeline:\s*(\d*.?\d*ms:\s*)?([^\:]*\:)?(.*)\((\d*.?\d*)ms\.?\s*-\s*(\d*.\d*)ms\.?\)/; |
| 13 | + |
| 14 | +enum ChromeTraceEventPhase { |
| 15 | + BEGIN = "B", |
| 16 | + END = "E", |
| 17 | + INSTANT = "i", |
| 18 | + COMPLETE = "X", |
| 19 | +} |
| 20 | + |
| 21 | +interface ChromeTraceEvent { |
| 22 | + ts: number; |
| 23 | + pid: number; |
| 24 | + tid: number; |
| 25 | + /** event phase */ |
| 26 | + ph?: ChromeTraceEventPhase | string; |
| 27 | + [otherData: string]: any; |
| 28 | +} |
| 29 | + |
| 30 | +interface DeviceTimeline { |
| 31 | + startPoint: number; |
| 32 | + timeline: ChromeTraceEvent[]; |
| 33 | +} |
| 34 | + |
| 35 | +export class TimelineProfilerService implements ITimelineProfilerService { |
| 36 | + private timelines: Map<string, DeviceTimeline> = new Map(); |
| 37 | + private attachedExitHandler: boolean = false; |
| 38 | + constructor( |
| 39 | + private $projectConfigService: IProjectConfigService, |
| 40 | + private $fs: IFileSystem, |
| 41 | + private $logger: ILogger |
| 42 | + ) {} |
| 43 | + |
| 44 | + private attachExitHanlder() { |
| 45 | + if (!this.attachedExitHandler) { |
| 46 | + this.$logger.info('attached "SIGINT" handler to write timeline data.'); |
| 47 | + originalProcessOn("SIGINT", this.writeTimelines.bind(this)); |
| 48 | + this.attachedExitHandler = true; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public processLogData(data: string, deviceIdentifier: string) { |
| 53 | + if (!this.isEnabled()) { |
| 54 | + return; |
| 55 | + } |
| 56 | + this.attachExitHanlder(); |
| 57 | + |
| 58 | + if (!this.timelines.has(deviceIdentifier)) { |
| 59 | + this.timelines.set(deviceIdentifier, { |
| 60 | + startPoint: null, |
| 61 | + timeline: [], |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + const deviceTimeline = this.timelines.get(deviceIdentifier); |
| 66 | + |
| 67 | + data.split("\n").forEach((line) => { |
| 68 | + const trace = this.toTrace(line.trim()); |
| 69 | + if (trace) { |
| 70 | + deviceTimeline.startPoint ??= trace.from; |
| 71 | + deviceTimeline.timeline.push(trace); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + @cache() |
| 77 | + private isEnabled() { |
| 78 | + return this.$projectConfigService.getValue("profiling") === "timeline"; |
| 79 | + } |
| 80 | + |
| 81 | + private toTrace(text: string): ChromeTraceEvent | undefined { |
| 82 | + const result = text.match(TIMELINE_LOG_RE); |
| 83 | + if (!result) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const trace = { |
| 88 | + domain: result[2]?.trim().replace(":", ""), |
| 89 | + name: result[3].trim(), |
| 90 | + from: parseFloat(result[4]), |
| 91 | + to: parseFloat(result[5]), |
| 92 | + }; |
| 93 | + |
| 94 | + return { |
| 95 | + pid: 1, |
| 96 | + tid: 1, |
| 97 | + ts: trace.from * 1000, |
| 98 | + dur: (trace.to - trace.from) * 1000, |
| 99 | + name: trace.name, |
| 100 | + cat: trace.domain ?? "default", |
| 101 | + ph: ChromeTraceEventPhase.COMPLETE, |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + private writeTimelines() { |
| 106 | + this.$logger.info("\n\nWriting timeline data to json..."); |
| 107 | + this.timelines.forEach((deviceTimeline, deviceIdentifier) => { |
| 108 | + const deviceTimelineFileName = `timeline-${deviceIdentifier}.json`; |
| 109 | + this.$fs.writeJson( |
| 110 | + path.resolve(process.cwd(), deviceTimelineFileName), |
| 111 | + deviceTimeline.timeline |
| 112 | + ); |
| 113 | + this.$logger.info( |
| 114 | + `Timeline data for device ${deviceIdentifier} written to ${deviceTimelineFileName}` |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + process.exit(); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +injector.register("timelineProfilerService", TimelineProfilerService); |
0 commit comments