新增小区第三方均价接口(ApiCode:3004)及测试脚本

- AverageStatistics 新增 xingfuliEsfPrice、maitianEsfPrice 字段
- AverageStatisticsService 新增 findRecentMonths 方法(查询最近N个月数据)
- PropertyController 新增 thirdPartyPriceAction(/property/thirdPartyPrice)
  - 返回幸福里、安居客、房天下、链家、城市房产、吉屋、淘房365、麦田均价
  - 仅取二手房价格,最多回溯3个月补全空值
- 新增 opentest 测试脚本:数据提取 + 多线程接口测试
- 更新 claude 技能文档:修正本地端口、seaweed集合字段说明、Groovy static 限制

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-02 16:27:26 +08:00
parent 5789350de2
commit 106d0cdbad
10 changed files with 391 additions and 5 deletions
@@ -0,0 +1,157 @@
package com.fangjia.script.opentest
import com.fangjia.core.util.HttpRequest
import com.fangjia.script.util.ExcelUtils
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.apache.commons.lang.SystemUtils
/**
* 小区第三方均价接口 - 接口测试脚本
* 接口: /property/thirdPartyPrice
*
* 依赖:先运行 ThirdPartyPriceTestDataExtractor.groovy 生成测试数据
* 输出: opentest/data/third_party_price_test_result.xlsx
* - Sheet1: 测试结果(详细数据)
* - Sheet2: 统计数据(汇总信息)
*/
final String TOKEN = "F54F52381C49BB9EB4A33EB1B65604AE4B71A28AEE53518A94A2F360408B9056D57553931D15CE6DDE765562DAD286BA38E05A4CDAFC51C3D527A4959BF8E75A3B95DB7108FCEA340DDE61925616DB55118E1851E67D83EAD800460D100D6B667A4ED8EE67C8F7FB"
final String HOST = SystemUtils.IS_OS_WINDOWS ? "http://localhost:9180" : "http://open.fangjia.com"
String scriptDir = new File(getClass().protectionDomain.codeSource.location.path).parent
String dataDir = new File(scriptDir, "data").absolutePath
String inputFile = "${dataDir}${File.separator}third_party_price_test_data.xlsx"
String outputFile = "${dataDir}${File.separator}third_party_price_test_result.xlsx"
// 1. 读取测试数据(含表头)
List<List<Object>> allData = ExcelUtils.read(inputFile, 0, 0)
// 2. 修改表头,添加结果列
allData[0] = ["序号", "id", "city", "region", "name",
"date", "xinfuliAvgPrice", "anjukeAvgPrice", "fangAvgPrice",
"lianjiaAvgPrice", "cityhouseAvgPrice", "jiwuAvgPrice",
"taofangAvgPrice", "maitianAvgPrice",
"耗时(ms)", "异常信息"]
// 3. 为每行添加空的结果列
for (int i = 1; i < allData.size(); i++) {
List row = allData[i]
(5..15).each { row << "" }
}
// 4. 写入初始结果文件
ExcelUtils.write(outputFile, allData, "测试结果")
// 5. 多线程调用接口
ExcelUtils.multiThreadingModify(outputFile, { List row ->
String id = row[1]?.toString() ?: ""
if (!id) return
HttpRequest request = new HttpRequest()
long startTime = System.currentTimeMillis()
try {
String url = "${HOST}/property/thirdPartyPrice?id=${id}&token=${TOKEN}"
String response = request.get(url, "utf-8")
long duration = System.currentTimeMillis() - startTime
JSONObject json = JSONObject.fromObject(response)
int code = json.optInt("code")
JSONArray resultArr = json.optJSONArray("result")
if (code == 200 && resultArr != null && resultArr.size() > 0) {
JSONObject result = resultArr.getJSONObject(0)
row[5] = result.opt("date")
row[6] = result.opt("xinfuliAvgPrice")
row[7] = result.opt("anjukeAvgPrice")
row[8] = result.opt("fangAvgPrice")
row[9] = result.opt("lianjiaAvgPrice")
row[10] = result.opt("cityhouseAvgPrice")
row[11] = result.opt("jiwuAvgPrice")
row[12] = result.opt("taofangAvgPrice")
row[13] = result.opt("maitianAvgPrice")
row[14] = duration
row[15] = ""
} else {
row[14] = duration
row[15] = "接口返回错误(code=${code}): ${json.optString('msg', '')}"
}
} catch (Exception e) {
long duration = System.currentTimeMillis() - startTime
row[14] = duration
row[15] = "异常: ${e.class.simpleName} - ${e.message}"
}
}, 0, "测试结果")
// 6. 读取结果并统计
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[15]?.toString() ?: ""
if (errorMsg == "") {
successCount++
if (row[14] instanceof Number) {
int duration = ((Number) row[14]).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++
}
}
// 7. 保存统计 Sheet
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] }
}
ExcelUtils.write(outputFile, statsData, "统计数据")
// 8. 控制台输出
println "\n========== 测试完成 =========="
println "总测试数:${totalCount}"
println "成功数:${successCount}"
println "失败数:${failCount}"
println "成功率:${totalCount > 0 ? String.format('%.2f%%', successCount * 100.0 / totalCount) : '0.00%'}"
if (successCount > 0) {
println "平均耗时:${(totalTime / successCount).intValue()} ms"
println "\n耗时分布:"
timeDist.each { range, count -> if (count > 0) println " ${range}: ${count} 条" }
}
println "\n结果文件:${outputFile}"
println " - Sheet1: 测试结果(详细数据)"
println " - Sheet2: 统计数据(汇总信息)"
@@ -0,0 +1,54 @@
package com.fangjia.script.opentest
import com.fangjia.core.Constants
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
/**
* 小区第三方均价接口 - 测试数据提取脚本
* 接口: /property/thirdPartyPrice
*
* 从 fangjia.seaweed 提取福州小区数据作为测试用例
* 输出: opentest/data/third_party_price_test_data.xlsx
*/
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}third_party_price_test_data.xlsx"
MongoCollection<Document> districtColl = Mongo3Utils.getDbCollection(
Constants.FANGJIA_MONGO_URI, "fangjia", "seaweed"
)
Document query = new Document("city", "福州").append("cat", "district").append("status", 0)
Document projection = new Document("_id", 1).append("city", 1).append("region", 1).append("name", 1)
MongoCursor<Document> cursor = districtColl.find(query)
.projection(projection)
.limit(1000)
.iterator()
List<List<Object>> excelData = []
excelData << ["序号", "id", "city", "region", "name"]
int index = 0
while (cursor.hasNext()) {
Document doc = cursor.next()
excelData << [
++index,
doc.getObjectId("_id").toHexString(),
doc.getString("city") ?: "",
doc.getString("region") ?: "",
doc.getString("name") ?: ""
]
}
cursor.close()
ExcelUtils.write(outputFile, excelData, "测试数据")
println "提取完成,共 ${index} 条数据"
println "输出文件:${outputFile}"