48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
// 解析性能报告,提取时间戳和性能数据
|
||
export function parseVProfReport(rawReport: string): { timestamp: string; data: string } | null {
|
||
if (!rawReport) return null
|
||
|
||
const lines = rawReport.split("\n")
|
||
let timestamp = ""
|
||
let inPerformanceSection = false
|
||
const performanceLines: string[] = []
|
||
|
||
for (const line of lines) {
|
||
// 提取时间戳:格式如 "11/05 01:51:27 [VProf] -- Performance report --"
|
||
const timestampMatch = line.match(
|
||
/(\d{2}\/\d{2}\s+\d{2}:\d{2}:\d{2})\s+\[VProf\]\s+--\s+Performance\s+report\s+--/
|
||
)
|
||
if (timestampMatch) {
|
||
timestamp = timestampMatch[1]
|
||
inPerformanceSection = true
|
||
// 也包含 Performance report 这一行,但移除时间戳
|
||
const lineWithoutTimestamp = line.trim().replace(/^\d{2}\/\d{2}\s+\d{2}:\d{2}:\d{2}\s+/, "")
|
||
performanceLines.push(lineWithoutTimestamp)
|
||
continue
|
||
}
|
||
|
||
// 如果在性能报告部分
|
||
if (inPerformanceSection) {
|
||
const trimmedLine = line.trim()
|
||
|
||
// 只收集包含 [VProf] 的行
|
||
if (trimmedLine.includes("[VProf]")) {
|
||
// 移除行首的时间戳(格式:MM/DD HH:mm:ss )
|
||
// 例如:"11/05 02:13:56 [VProf] ..." -> "[VProf] ..."
|
||
const lineWithoutTimestamp = trimmedLine.replace(/^\d{2}\/\d{2}\s+\d{2}:\d{2}:\d{2}\s+/, "")
|
||
performanceLines.push(lineWithoutTimestamp)
|
||
}
|
||
// 如果遇到空行且已经有数据,可能是报告结束,但不直接结束,因为可能还有更多数据
|
||
// 如果后续没有 [VProf] 行的数据,空行会被过滤掉
|
||
}
|
||
}
|
||
|
||
if (performanceLines.length === 0) return null
|
||
|
||
return {
|
||
timestamp,
|
||
data: performanceLines.join("\n").trim(),
|
||
}
|
||
}
|
||
|