新增易方达接口以及添加claude技能
This commit is contained in:
+222
@@ -0,0 +1,222 @@
|
||||
package com.fangjia.script.opentest
|
||||
|
||||
import com.fangjia.core.Constants
|
||||
import com.fangjia.core.util.HttpRequest
|
||||
import com.fangjia.script.util.ExcelUtils
|
||||
import net.sf.json.JSONObject
|
||||
|
||||
/**
|
||||
* E基金地址匹配接口 - 接口测试脚本
|
||||
* 功能:使用ExcelUtils.multiThreadingModify多线程调用接口,记录结果、耗时和异常信息
|
||||
* @author wenyan
|
||||
*/
|
||||
|
||||
println "========== E基金接口测试 =========="
|
||||
println "开始时间:${new Date()}"
|
||||
|
||||
try {
|
||||
// 1. 定义数据目录和文件路径
|
||||
String scriptDir = new File(getClass().protectionDomain.codeSource.location.path).parent
|
||||
String dataDir = new File(scriptDir, "data").absolutePath
|
||||
String inputFile = "${dataDir}${File.separator}efund_test_data.xlsx"
|
||||
String outputFile = "${dataDir}${File.separator}efund_test_result.xlsx"
|
||||
|
||||
println "测试数据文件:${inputFile}"
|
||||
println "结果输出文件:${outputFile}"
|
||||
|
||||
// 检查测试数据文件是否存在
|
||||
if (!new File(inputFile).exists()) {
|
||||
println "\n错误:测试数据文件不存在!"
|
||||
println "请先运行 EfundTestDataExtractor.groovy 提取测试数据"
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 读取测试数据并准备结果Excel
|
||||
println "\n正在准备测试数据..."
|
||||
List<List<Object>> allData = ExcelUtils.read(inputFile, 0, 0) // 读取所有数据(包括表头)
|
||||
|
||||
if (allData.size() <= 1) {
|
||||
println "警告:测试数据为空!"
|
||||
return
|
||||
}
|
||||
|
||||
println "共 ${allData.size() - 1} 条测试数据"
|
||||
|
||||
// 3. 修改表头,添加结果列
|
||||
allData[0] = ["序号", "城市", "地址", "返回城市", "返回区域", "返回街道","返回道路","返回门牌号",
|
||||
"小区名称", "物业类型", "匹配级别", "匹配置信度",
|
||||
"参考价格", "典型总价", "更新时间", "耗时(ms)", "异常信息"]
|
||||
|
||||
// 4. 为每一行数据添加空的结果列
|
||||
for (int i = 1; i < allData.size(); i++) {
|
||||
List row = allData[i]
|
||||
// 添加12个结果列(从"返回城市"到"异常信息")
|
||||
(3..14).each { row << "" }
|
||||
}
|
||||
|
||||
// 5. 写入初始结果文件
|
||||
println "正在创建结果文件..."
|
||||
ExcelUtils.write(outputFile, allData, "测试结果")
|
||||
println "结果文件创建成功!"
|
||||
|
||||
// 6. 使用 ExcelUtils.multiThreadingModify 多线程调用接口
|
||||
println "\n开始多线程测试接口..."
|
||||
println "接口地址:http://localhost:9180/efund/match"
|
||||
println "线程数:CPU核心数 × 4 = ${Runtime.getRuntime().availableProcessors() * 4}"
|
||||
|
||||
ExcelUtils.multiThreadingModify(outputFile, { List row ->
|
||||
HttpRequest request = new HttpRequest()
|
||||
long startTime = System.currentTimeMillis()
|
||||
|
||||
try {
|
||||
// 提取参数(row[0]=序号, row[1]=城市, row[2]=地址)
|
||||
String city = row[1]?.toString() ?: ""
|
||||
String address = row[2]?.toString() ?: ""
|
||||
|
||||
// 调用接口
|
||||
Map<String, String> params = ["address": address, "token": Constants.SUPER_USER_TOKEN]
|
||||
if (city) {
|
||||
params.put("city", city)
|
||||
}
|
||||
|
||||
String response = request.get("http://localhost:9180/efund/match", params, "UTF-8")
|
||||
long duration = System.currentTimeMillis() - startTime
|
||||
|
||||
// 解析响应
|
||||
JSONObject json = JSONObject.fromObject(response)
|
||||
Integer code = json.optInt("code")
|
||||
JSONObject result = json.optJSONObject("result")
|
||||
|
||||
if (code == 200 && result != null) {
|
||||
// 填充结果列(直接修改row)
|
||||
row[3] = result.optString("city", "")
|
||||
row[4] = result.optString("district", "")
|
||||
row[5] = result.optString("street", "")
|
||||
row[6] = result.optString("road", "")
|
||||
row[7] = result.optString("road_number", "")
|
||||
row[8] = result.optString("community_name", "")
|
||||
row[9] = result.optString("property_type", "")
|
||||
row[10] = result.optString("match_level", "")
|
||||
row[11] = result.opt("match_confidence") ?: ""
|
||||
row[12] = result.opt("reference_price") ?: ""
|
||||
row[13] = result.opt("typical_total_price") ?: ""
|
||||
row[14] = result.optString("update_time", "")
|
||||
row[15] = duration
|
||||
row[16] = "" // 无异常
|
||||
} else {
|
||||
// 接口返回异常
|
||||
row[15] = duration
|
||||
String msg = json.optString("msg", "")
|
||||
row[16] = "接口返回错误(code=${code}): ${msg}"
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
long duration = System.currentTimeMillis() - startTime
|
||||
row[15] = duration
|
||||
row[16] = "异常: ${e.class.simpleName} - ${e.message}"
|
||||
}
|
||||
}, 0, "测试结果")
|
||||
|
||||
// 7. 统计结果
|
||||
println "\n正在统计测试结果..."
|
||||
List<List<Object>> finalData = ExcelUtils.read(outputFile, 0, 0)
|
||||
|
||||
int totalCount = finalData.size() - 1 // 减去表头
|
||||
int successCount = 0
|
||||
int failCount = 0
|
||||
long totalTime = 0
|
||||
|
||||
Map<String, Integer> timeDist = [
|
||||
"<100ms": 0,
|
||||
"100-200ms": 0,
|
||||
"200-500ms": 0,
|
||||
"500-1000ms": 0,
|
||||
">1000ms": 0
|
||||
]
|
||||
|
||||
for (int i = 1; i < finalData.size(); i++) {
|
||||
List row = finalData[i]
|
||||
|
||||
// 统计成功/失败
|
||||
String errorMsg = row[14]?.toString() ?: ""
|
||||
if (errorMsg == "") {
|
||||
successCount++
|
||||
|
||||
// 统计耗时
|
||||
if (row[13] instanceof Number) {
|
||||
int duration = ((Number)row[13]).intValue()
|
||||
totalTime += duration
|
||||
|
||||
// 耗时分布
|
||||
if (duration < 100) timeDist["<100ms"]++
|
||||
else if (duration < 200) timeDist["100-200ms"]++
|
||||
else if (duration < 500) timeDist["200-500ms"]++
|
||||
else if (duration < 1000) timeDist["500-1000ms"]++
|
||||
else timeDist[">1000ms"]++
|
||||
}
|
||||
} else {
|
||||
failCount++
|
||||
}
|
||||
}
|
||||
|
||||
// 8. 保存统计数据到新Sheet
|
||||
println "\n正在保存统计数据到Excel..."
|
||||
List<List<Object>> statsData = []
|
||||
|
||||
// 基本统计
|
||||
statsData << ["统计项", "数值"]
|
||||
statsData << ["测试时间", new Date().format("yyyy-MM-dd HH:mm:ss")]
|
||||
statsData << ["", ""]
|
||||
statsData << ["总测试数", totalCount]
|
||||
statsData << ["成功数", successCount]
|
||||
statsData << ["失败数", failCount]
|
||||
statsData << ["成功率", totalCount > 0 ? String.format('%.2f%%', successCount * 100.0 / totalCount) : '0.00%']
|
||||
|
||||
if (successCount > 0) {
|
||||
int avgTime = (totalTime / successCount).intValue()
|
||||
statsData << ["", ""]
|
||||
statsData << ["平均耗时(ms)", avgTime]
|
||||
|
||||
// 耗时分布
|
||||
statsData << ["", ""]
|
||||
statsData << ["耗时分布", "数量"]
|
||||
timeDist.each { range, count ->
|
||||
statsData << [range, count]
|
||||
}
|
||||
}
|
||||
|
||||
// 添加统计Sheet
|
||||
ExcelUtils.write(outputFile, statsData, "统计数据")
|
||||
|
||||
// 9. 控制台输出统计结果
|
||||
println "\n========== 测试完成 =========="
|
||||
println "总测试数:${totalCount}"
|
||||
println "成功数:${successCount}"
|
||||
println "失败数:${failCount}"
|
||||
println "成功率:${totalCount > 0 ? String.format('%.2f%%', successCount * 100.0 / totalCount) : '0.00%'}"
|
||||
|
||||
if (successCount > 0) {
|
||||
int avgTime = (totalTime / successCount).intValue()
|
||||
println "平均耗时:${avgTime} ms"
|
||||
|
||||
println "\n耗时分布:"
|
||||
timeDist.each { range, count ->
|
||||
if (count > 0) {
|
||||
println " ${range}: ${count} 条"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println "\n结果文件:${outputFile}"
|
||||
println " - Sheet1: 测试结果(详细数据)"
|
||||
println " - Sheet2: 统计数据(汇总信息)"
|
||||
|
||||
} catch (Exception e) {
|
||||
println "\n错误:脚本执行失败!"
|
||||
println "异常类型:${e.class.name}"
|
||||
println "异常信息:${e.message}"
|
||||
e.printStackTrace()
|
||||
} finally {
|
||||
println "\n结束时间:${new Date()}"
|
||||
println "========================================"
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
package com.fangjia.script.opentest
|
||||
|
||||
import com.fangjia.core.Constants
|
||||
import com.fangjia.core.util.DateUtil
|
||||
import com.fangjia.core.util.Mongo3Utils
|
||||
import com.fangjia.script.util.ExcelUtils
|
||||
import com.mongodb.client.MongoCollection
|
||||
import com.mongodb.client.MongoCursor
|
||||
import org.bson.Document
|
||||
|
||||
/**
|
||||
* E基金地址匹配接口 - 测试数据提取脚本
|
||||
* 功能:从MongoDB 206服务器提取历史测试数据并导出到Excel
|
||||
* @author wenyan
|
||||
*/
|
||||
|
||||
println "========== E基金接口测试数据提取 =========="
|
||||
println "开始时间:${new Date()}"
|
||||
|
||||
try {
|
||||
// 1. 连接MongoDB 206 (使用 Mongo3Utils 和 Constants.SEAWATER_MONGO_URI)
|
||||
println "\n正在连接MongoDB 206服务器..."
|
||||
MongoCollection<Document> coll = Mongo3Utils.getDbCollection(
|
||||
Constants.SEAWATER_MONGO_URI,
|
||||
"business",
|
||||
"open_uri_log"
|
||||
)
|
||||
println "MongoDB连接成功!"
|
||||
|
||||
// 2. 查询条件(使用 Document)
|
||||
println "\n构建查询条件..."
|
||||
Document query = new Document()
|
||||
query.put("user_id", "1007569")
|
||||
query.put("request_uri", "/address/risk")
|
||||
query.put("c_date", new Document().append('$gt', DateUtil.parseDate("2025-07-01", "yyyy-MM-dd")))
|
||||
|
||||
// 先查询总数
|
||||
long totalCount = coll.countDocuments(query)
|
||||
println "符合条件的数据总数:${totalCount}"
|
||||
|
||||
if (totalCount == 0) {
|
||||
println "警告:未找到符合条件的数据!"
|
||||
return
|
||||
}
|
||||
|
||||
println "将提取前 1000 条数据"
|
||||
|
||||
// 3. 查询1000条数据(只查询需要的字段)
|
||||
println "\n开始查询数据..."
|
||||
Document projection = new Document("parameter_map", 1).append("_id", 0)
|
||||
MongoCursor<Document> cursor = coll.find(query)
|
||||
.projection(projection)
|
||||
.limit(1000)
|
||||
.iterator()
|
||||
|
||||
// 4. 提取parameter_map中的city和address
|
||||
List<List<Object>> excelData = []
|
||||
excelData << ["序号", "城市", "地址"]
|
||||
|
||||
int index = 0
|
||||
int validCount = 0
|
||||
int errorCount = 0
|
||||
|
||||
while (cursor.hasNext()) {
|
||||
try {
|
||||
Document doc = cursor.next()
|
||||
index++
|
||||
|
||||
// 提取parameter_map
|
||||
Document paramMap = doc.get("parameter_map", Document.class)
|
||||
|
||||
if (paramMap == null) {
|
||||
println "警告:第 ${index} 条数据没有 parameter_map 字段"
|
||||
errorCount++
|
||||
continue
|
||||
}
|
||||
|
||||
String city = paramMap.getString("city") ?: ""
|
||||
String address = paramMap.getString("address") ?: ""
|
||||
|
||||
excelData << [index, city, address]
|
||||
validCount++
|
||||
|
||||
if (index % 100 == 0) {
|
||||
println "已处理 ${index} 条数据..."
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
errorCount++
|
||||
println "处理第 ${index} 条数据时出错:${e.message}"
|
||||
}
|
||||
}
|
||||
|
||||
cursor.close()
|
||||
|
||||
println "\n数据提取完成!"
|
||||
println "总处理数:${index}"
|
||||
println "有效数据:${validCount}"
|
||||
println "错误数据:${errorCount}"
|
||||
|
||||
// 5. 保存到Excel
|
||||
println "\n正在保存Excel文件..."
|
||||
String scriptDir = new File(getClass().protectionDomain.codeSource.location.path).parent
|
||||
String dataDir = new File(scriptDir, "data").absolutePath
|
||||
new File(dataDir).mkdirs() // 确保目录存在
|
||||
|
||||
String outputFile = "${dataDir}${File.separator}efund_test_data.xlsx"
|
||||
ExcelUtils.write(outputFile, excelData, "测试数据")
|
||||
|
||||
println "Excel文件保存成功!"
|
||||
println "输出文件:${outputFile}"
|
||||
println "数据行数:${excelData.size()} 行(含表头)"
|
||||
|
||||
} catch (Exception e) {
|
||||
println "\n错误:脚本执行失败!"
|
||||
println "异常类型:${e.class.name}"
|
||||
println "异常信息:${e.message}"
|
||||
e.printStackTrace()
|
||||
} finally {
|
||||
println "\n结束时间:${new Date()}"
|
||||
println "=========================================="
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package com.fangjia.script.temp
|
||||
import java.nio.file.*
|
||||
import java.nio.charset.*
|
||||
import java.nio.file.attribute.*
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
// ===== 修改为你的目标目录 =====
|
||||
def rootDir = Paths.get("C:\\Users\\wenyan\\IdeaProjects\\main")
|
||||
// =================================
|
||||
|
||||
def gbkCharset = Charset.forName("GBK")
|
||||
def utf8Charset = StandardCharsets.UTF_8
|
||||
|
||||
Files.walk(rootDir)
|
||||
.filter { Files.isRegularFile(it) }
|
||||
.filter {
|
||||
def name = it.toString().toLowerCase()
|
||||
name.endsWith(".java") || name.endsWith(".groovy")
|
||||
}
|
||||
.forEach { path ->
|
||||
|
||||
byte[] bytes = Files.readAllBytes(path)
|
||||
|
||||
if (isGBK(bytes)) {
|
||||
println "Converting: $path"
|
||||
|
||||
// 用 GBK 解码
|
||||
String content = new String(bytes, gbkCharset)
|
||||
|
||||
// 写回 UTF-8(无 BOM)
|
||||
Files.write(path, content.getBytes(utf8Charset),
|
||||
StandardOpenOption.TRUNCATE_EXISTING)
|
||||
|
||||
} else {
|
||||
println "Skip (not GBK): $path"
|
||||
}
|
||||
}
|
||||
|
||||
println "Done."
|
||||
|
||||
|
||||
// ===== 判断是否为 GBK 编码 =====
|
||||
boolean isGBK(byte[] bytes) {
|
||||
try {
|
||||
def decoder = Charset.forName("UTF-8")
|
||||
.newDecoder()
|
||||
.onMalformedInput(CodingErrorAction.REPORT)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPORT)
|
||||
|
||||
decoder.decode(ByteBuffer.wrap(bytes))
|
||||
return false // 能被 UTF-8 正确解析 → 不是 GBK
|
||||
} catch (Exception e) {
|
||||
return true // UTF-8 解析失败 → 很可能是 GBK
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user