Files
wwjcloud-nest-v1/tests/k6/migration-compare.js

75 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2025-11-16 22:13:57 +08:00
import http from 'k6/http'
import { check } from 'k6'
export const options = {
vus: 1,
iterations: 1,
thresholds: {
checks: ['rate==1'],
},
}
function isObject(x) {
return x !== null && typeof x === 'object' && !Array.isArray(x)
}
function deepEqual(a, b) {
if (a === b) return true
if (typeof a !== typeof b) return false
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) return false
for (let i = 0; i < a.length; i++) {
if (!deepEqual(a[i], b[i])) return false
}
return true
}
if (isObject(a) && isObject(b)) {
const ak = Object.keys(a)
const bk = Object.keys(b)
if (ak.length !== bk.length) return false
for (const k of ak) {
if (!Object.prototype.hasOwnProperty.call(b, k)) return false
if (!deepEqual(a[k], b[k])) return false
}
return true
}
return false
}
export default function () {
const base1 = __ENV.BASE1
const base2 = __ENV.BASE2
if (!base1 || !base2) {
throw new Error('必须设置环境变量 BASE1 与 BASE2')
}
const endpoints = [
{ method: 'GET', path: '/api/area/list_by_pid/0' },
{ method: 'GET', path: '/api/area/tree/2' },
{ method: 'GET', path: '/api/copyright' },
{ method: 'GET', path: '/api/init' },
]
for (const ep of endpoints) {
const r1 = http.request(ep.method, `${base1}${ep.path}`)
const r2 = http.request(ep.method, `${base2}${ep.path}`)
const okStatus = r1.status === 200 && r2.status === 200
check({ r1, r2 }, { [`${ep.method} ${ep.path} 状态码一致且为200`]: () => okStatus })
if (!okStatus) {
console.log(`${ep.method} ${ep.path} 状态码不一致`, r1.status, r2.status)
}
let j1, j2
try {
j1 = r1.json()
j2 = r2.json()
} catch (e) {
check({}, { [`${ep.method} ${ep.path} JSON可解析`]: () => false })
continue
}
const equal = deepEqual(j1, j2)
check({}, { [`${ep.method} ${ep.path} 响应完全一致`]: () => equal })
if (!equal) {
console.log(`${ep.method} ${ep.path} 响应不一致`)
}
}
}