Repository navigation
Expand file tree
/
Copy pathnode-nim-tester.js
More file actions
executable file
·157 lines (147 loc) · 6.27 KB
/
Copy pathnode-nim-tester.js
File metadata and controls
executable file
·157 lines (147 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#! /usr/bin/env node
const { Command } = require('commander')
const WebAT = require('hawk-web')
const program = new Command()
const os = require('os')
const path = require('path')
const fs = require('fs')
const v8 = require('v8')
const { createLogger, format, transports } = require('winston')
const { combine, timestamp, label, printf } = format
const downloadSDK = require('./script/download-sdk.js').downloadSDK
const loggerFormat = printf(({ level, message, label, timestamp }) => {
return `${timestamp} [${label}] ${level}: ${message}`
})
const logger = createLogger({
level: 'info',
format: combine(
label({ label: 'right meow!' }),
timestamp({
format: () => {
const now = new Date()
const dateStr = now.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' })
const ms = now.getMilliseconds().toString().padStart(3, '0')
return `${dateStr}.${ms}`
}
}),
loggerFormat
),
defaultMeta: { service: 'user-service' },
transports: [
new transports.File({
filename: (() => {
const logDir = path.join(os.homedir(), 'Downloads', 'node-nim')
// 如果文件夹不存在则创建
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, { recursive: true })
}
return path.join(logDir, `node-memory-${new Date().toISOString().split('T')[0]}-${process.pid}.log`)
})()
}),
],
})
function getSystemMemoryInfo () {
const totalMemory = os.totalmem()
const freeMemory = os.freemem()
const usedMemory = totalMemory - freeMemory
return {
total: Math.round(totalMemory / 1024 / 1024), // MB
free: Math.round(freeMemory / 1024 / 1024), // MB
used: Math.round(usedMemory / 1024 / 1024), // MB
usagePercent: Math.round((usedMemory / totalMemory) * 100) // 百分比
}
}
function getProcessMemoryInfo () {
const usage = process.memoryUsage()
return {
rss: Math.round(usage.rss / 1024 / 1024), // 常驻内存大小
heapTotal: Math.round(usage.heapTotal / 1024 / 1024), // 堆总大小ß
heapUsed: Math.round(usage.heapUsed / 1024 / 1024), // 堆已使用
external: Math.round(usage.external / 1024 / 1024), // V8 外部内存
arrayBuffers: Math.round((usage.arrayBuffers || 0) / 1024 / 1024) // ArrayBuffer 大小
}
}
function getV8HeapDetails () {
const heapStats = v8.getHeapStatistics()
const heapSpaceStats = v8.getHeapSpaceStatistics()
return {
heap: {
totalHeapSize: Math.round(heapStats.total_heap_size / 1024 / 1024), // 总堆大小 MB
totalHeapSizeExecutable: Math.round(heapStats.total_heap_size_executable / 1024 / 1024), // 可执行堆大小 MB
totalPhysicalSize: Math.round(heapStats.total_physical_size / 1024 / 1024), // 物理大小 MB
totalAvailableSize: Math.round(heapStats.total_available_size / 1024 / 1024), // 可用大小 MB
usedHeapSize: Math.round(heapStats.used_heap_size / 1024 / 1024), // 已使用堆大小 MB
heapSizeLimit: Math.round(heapStats.heap_size_limit / 1024 / 1024), // 堆大小限制 MB (max-old-space-size)
mallocedMemory: Math.round(heapStats.malloced_memory / 1024 / 1024), // malloc 分配的内存 MB
peakMallocedMemory: Math.round(heapStats.peak_malloced_memory / 1024 / 1024), // 峰值 malloc 内存 MB
numberOfNativeContexts: heapStats.number_of_native_contexts, // 原生上下文数量
numberOfDetachedContexts: heapStats.number_of_detached_contexts // 分离上下文数量
},
spaces: heapSpaceStats.map(space => ({
name: space.space_name,
size: Math.round(space.space_size / 1024 / 1024), // 空间大小 MB
used: Math.round(space.space_used_size / 1024 / 1024), // 已使用大小 MB
available: Math.round(space.space_available_size / 1024 / 1024), // 可用大小 MB
physical: Math.round(space.physical_space_size / 1024 / 1024) // 物理大小 MB
}))
}
}
program
.command('run')
.description('run node-nim tester')
.requiredOption('--deviceId <deviceId>', 'tester device id')
.option('--taskId <taskId>', 'hawk test suite task id')
.option('--url <url>', 'hawk websocket url')
.option('--version <version>', 'test sdk version')
.option('--nimSdkUrl <nimSdkUrl>', 'nim native sdk download url')
.allowUnknownOption(true)
.action(async (options) => {
logger.info(`run node-nim tester, options: ${JSON.stringify(options)}`)
const heapStats = v8.getHeapStatistics()
logger.info(`V8 heap limit (max-old-space-size): ${Math.round(heapStats.heap_size_limit / 1024 / 1024)} MB`)
setInterval(() => {
const processMemoryUsage = getProcessMemoryInfo()
logger.info(`System memory usage: ${JSON.stringify(getSystemMemoryInfo(), null, 2)}`)
logger.info(`Process memory usage: ${JSON.stringify(getProcessMemoryInfo(), null, 2)}`)
logger.info(`V8 heap details: ${JSON.stringify(getV8HeapDetails())}`)
if (processMemoryUsage.heapTotal > 1000) {
const logDir = path.join(os.homedir(), 'Downloads', 'node-nim')
// 如果文件夹不存在则创建
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, { recursive: true })
}
const heapSnapshotFile = path.join(logDir, `node-heap-snapshot-${new Date().toISOString().split('T')[0]}-${process.pid}.log`)
if (!fs.existsSync(heapSnapshotFile)) {
logger.info(`Writing heap snapshot to ${heapSnapshotFile}`)
require('v8').writeHeapSnapshot(heapSnapshotFile)
}
}
}, 5 * 1000)
if (options.nimSdkUrl) {
await downloadSDK(options.nimSdkUrl)
}
const nimInstance = require('./dist/node-nim')
nimInstance.v2 = new nimInstance.V2NIMClient()
nimInstance.nim = new nimInstance.NIM()
nimInstance.qchat = new nimInstance.QChat()
nimInstance.chatroom = new nimInstance.ChatRoom()
new WebAT({
applicationName: 'nim',
platform: 'Windows&MacOS',
version: options.version,
url: options.url,
deviceId: options.deviceId,
taskId: options.taskId,
targets: {
NIM: nimInstance,
fs,
os
},
oncompleted: () => {
// 执行完成后退出进程
process.exit(0)
}
})
})
// parse
program.parse()