08ea5fbe98
添加用户管理视图、API和状态管理文件
113 lines
3.2 KiB
TypeScript
113 lines
3.2 KiB
TypeScript
import { Page } from '@playwright/test'
|
|
|
|
export class PerformanceMetrics {
|
|
private metrics: Map<string, number[]> = new Map()
|
|
|
|
recordMetric(name: string, value: number) {
|
|
if (!this.metrics.has(name)) {
|
|
this.metrics.set(name, [])
|
|
}
|
|
this.metrics.get(name)!.push(value)
|
|
}
|
|
|
|
getAverage(name: string): number {
|
|
const values = this.metrics.get(name) || []
|
|
if (values.length === 0) return 0
|
|
const sum = values.reduce((a, b) => a + b, 0)
|
|
return sum / values.length
|
|
}
|
|
|
|
getP95(name: string): number {
|
|
const values = this.metrics.get(name) || []
|
|
if (values.length === 0) return 0
|
|
const sorted = [...values].sort((a, b) => a - b)
|
|
const index = Math.floor(sorted.length * 0.95)
|
|
return sorted[index]
|
|
}
|
|
|
|
getP99(name: string): number {
|
|
const values = this.metrics.get(name) || []
|
|
if (values.length === 0) return 0
|
|
const sorted = [...values].sort((a, b) => a - b)
|
|
const index = Math.floor(sorted.length * 0.99)
|
|
return sorted[index]
|
|
}
|
|
|
|
getMax(name: string): number {
|
|
const values = this.metrics.get(name) || []
|
|
if (values.length === 0) return 0
|
|
return Math.max(...values)
|
|
}
|
|
|
|
getMin(name: string): number {
|
|
const values = this.metrics.get(name) || []
|
|
if (values.length === 0) return 0
|
|
return Math.min(...values)
|
|
}
|
|
|
|
printReport() {
|
|
console.log('\n=== 性能测试报告 ===')
|
|
for (const [name, values] of this.metrics.entries()) {
|
|
console.log(`\n${name}:`)
|
|
console.log(` 平均值: ${this.getAverage(name).toFixed(2)}ms`)
|
|
console.log(` P95: ${this.getP95(name).toFixed(2)}ms`)
|
|
console.log(` P99: ${this.getP99(name).toFixed(2)}ms`)
|
|
console.log(` 最大值: ${this.getMax(name).toFixed(2)}ms`)
|
|
console.log(` 最小值: ${this.getMin(name).toFixed(2)}ms`)
|
|
console.log(` 样本数: ${values.length}`)
|
|
}
|
|
console.log('\n====================\n')
|
|
}
|
|
}
|
|
|
|
export class PerformanceTestHelper {
|
|
async clearCacheAndCookies(page: Page) {
|
|
const context = page.context()
|
|
await context.clearCookies()
|
|
await page.evaluate(() => {
|
|
localStorage.clear()
|
|
sessionStorage.clear()
|
|
})
|
|
}
|
|
|
|
async measurePageLoad(page: Page, url: string): Promise<number> {
|
|
const startTime = Date.now()
|
|
await page.goto(url, { waitUntil: 'networkidle' })
|
|
const endTime = Date.now()
|
|
return endTime - startTime
|
|
}
|
|
|
|
async measureApiCall(page: Page, apiCall: () => Promise<void>): Promise<number> {
|
|
const startTime = Date.now()
|
|
await apiCall()
|
|
const endTime = Date.now()
|
|
return endTime - startTime
|
|
}
|
|
|
|
async measureElementInteraction(
|
|
page: Page,
|
|
selector: string,
|
|
action: () => Promise<void>
|
|
): Promise<number> {
|
|
await page.waitForSelector(selector, { state: 'visible' })
|
|
const startTime = Date.now()
|
|
await action()
|
|
const endTime = Date.now()
|
|
return endTime - startTime
|
|
}
|
|
|
|
async measurePageNavigation(
|
|
page: Page,
|
|
fromUrl: string,
|
|
toUrl: string
|
|
): Promise<number> {
|
|
await page.goto(fromUrl, { waitUntil: 'networkidle' })
|
|
const startTime = Date.now()
|
|
await page.goto(toUrl, { waitUntil: 'networkidle' })
|
|
const endTime = Date.now()
|
|
return endTime - startTime
|
|
}
|
|
}
|
|
|
|
export default PerformanceTestHelper
|