惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
H
Help Net Security
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
A
About on SuperTechFans
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
博客园 - 【当耐特】
Microsoft Security Blog
Microsoft Security Blog
Martin Fowler
Martin Fowler
I
InfoQ
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
U
Unit 42
The Cloudflare Blog
Y
Y Combinator Blog

博客园 - ReturnHome

shell脚本 sql窗口函数 sql常用 大数据面试问题 Python常用库和方法 金融行业测试总结 电商高并发高库存系统测试总结 AI测试 大模型性能测试 100 条大模型人工测试用例 记忆管理 多智能体协作 Elasticsearch(ES) 知识检索 AI提示词链 详解https协议和http协议的区别和底层工作原理 请求区别 app测试注意点 TCP的三次握手和四次挥手 常用的中间件
jmeter计算模型token
ReturnHome · 2026-03-16 · via 博客园 - ReturnHome

image

import java.io.FileWriter
import java.io.BufferedWriter
import java.text.SimpleDateFormat

// 1. 获取本次请求的关键信息
def sampleLabel = prev.getSampleLabel() // 采样器名称
def responseCode = prev.getResponseCode() // 响应码
def responseData = prev.getResponseDataAsString() // 响应体
def timestamp = System.currentTimeMillis() // 时间戳(毫秒)
def success = prev.isSuccessful() ? "成功" : "失败" // 是否成功
def elapsedTimeMs = prev.getTime() // 获取原始耗时(毫秒)
def elapsedTimeSec = elapsedTimeMs / 1000.0 // 转换为秒(保留小数)
def requestTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) // 易读的请求时间
def responseCharCount = responseData ? responseData.length() : 0 // 响应体字符数(空响应记0)

// 2. 计算吞吐量:ResponseCharCount / ElapsedTime(s) / 1.5
def throughput = 0.0
if (elapsedTimeSec > 0) { // 避免除以0报错
throughput = (responseCharCount / elapsedTimeSec) / 1.5
}

// 3. 处理响应数据中的特殊字符(避免破坏 CSV 格式)
def cleanResponse = responseData ? responseData
.replaceAll('"', '""') // 双引号转义
.replaceAll('\r?\n', '\\n') // 换行符转义
.replaceAll(',', '\\,') : "" // 逗号转义(空响应则为空字符串)

// 4. 定义 CSV 文件路径(改成你自己的路径)
def csvFile = new File("D:/miaobiapi/testshuju_response.csv")

// 5. 写入文件(追加模式,加锁避免并发写入冲突)
def writer = new BufferedWriter(new FileWriter(csvFile, true))
try {
// 如果文件不存在,先写入表头(新增吞吐量列)
if (csvFile.length() == 0) {
writer.write("RequestTime,Timestamp,SamplerLabel,ResponseCode,Success,ElapsedTime(s),ResponseCharCount,Throughput,ResponseData\n")
}
// 写入一行数据(吞吐量保留3位小数,保证格式统一)
writer.write(String.format("\"%s\",\"%d\",\"%s\",\"%s\",\"%s\",\"%.3f\",\"%d\",\"%.3f\",\"%s\"\n",
requestTime, timestamp, sampleLabel, responseCode, success, elapsedTimeSec, responseCharCount, throughput, cleanResponse))
} catch (Exception e) {
log.error("写入CSV失败:" + e.getMessage()) // 写入失败时打印日志
} finally {
writer.close() // 确保文件流关闭
}