生成10w条数据工具类

This commit is contained in:
2026-03-27 14:54:01 +08:00
parent 1b00900808
commit 7b10c1f239
3 changed files with 163 additions and 2 deletions

View File

@@ -53,8 +53,8 @@ public class ExcelTransfer {
public static void main(String[] args) { public static void main(String[] args) {
String sourceFile = "/Users/hengspire/data/111.xlsx"; String sourceFile = "/Users/hengspire/Desktop/111.xlsx";
String targetFile = "/Users/hengspire/data/222.xlsx"; String targetFile = "/Users/hengspire/Desktop/222.xlsx";
ExcelReader reader = ExcelUtil.getReader(sourceFile); ExcelReader reader = ExcelUtil.getReader(sourceFile);

View File

@@ -0,0 +1,108 @@
package com.example.demo.util;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 多线程批量生成运证通APP问卷测试数据调用后台接口
* 速度极快10万条数据几十秒完成
*/
public class MultiThreadSurveyGenerator {
// ====================== 【必须修改这里】 ======================
// 后台新增问卷接口地址
private static final String ADD_API_URL = "http://localhost:9080/app/survey/satisfaction";
// 总生成数量
private static final int TOTAL_COUNT = 100000;
// 线程数(根据你的电脑/接口性能调整8~32 最合适)
private static final int THREAD_NUM = 20;
// ===========================================================
// 成功计数(原子类,线程安全)
private static final AtomicInteger successCount = new AtomicInteger(0);
// 失败计数
private static final AtomicInteger failCount = new AtomicInteger(0);
public static void main(String[] args) {
System.out.println("===== 开始多线程生成【10万条】问卷数据 =====");
long startTime = System.currentTimeMillis();
// 创建线程池
ExecutorService executor = Executors.newFixedThreadPool(THREAD_NUM);
// 计数器:等待所有线程执行完
CountDownLatch latch = new CountDownLatch(TOTAL_COUNT);
// 循环提交任务
for (int i = 0; i < TOTAL_COUNT; i++) {
executor.submit(() -> {
try {
// 生成 8/9/10 随机分数
int q1 = (int) (Math.random() * 3) + 8;
int q2 = (int) (Math.random() * 3) + 8;
int q3 = (int) (Math.random() * 3) + 8;
int q4 = (int) (Math.random() * 3) + 8;
int q5 = (int) (Math.random() * 3) + 8;
// 构造请求参数
Map<String, Object> param = new HashMap<>();
param.put("q1", q1);
param.put("q2", q2);
param.put("q3", q3);
param.put("q4", q4);
param.put("q5", q5);
// 发送POST请求JSON格式
HttpResponse response = HttpRequest.post(ADD_API_URL)
.header("Content-Type", "application/json")
.body(JSONUtil.toJsonStr(param))
.timeout(5000)
.execute();
if (response.isOk()) {
successCount.incrementAndGet();
} else {
failCount.incrementAndGet();
}
// 每1000条打印一次进度
int current = successCount.get() + failCount.get();
if (current % 1000 == 0) {
System.out.println("已完成:" + current + "/" + TOTAL_COUNT
+ " 成功:" + successCount.get()
+ " 失败:" + failCount.get());
}
} catch (Exception e) {
failCount.incrementAndGet();
} finally {
latch.countDown();
}
});
}
try {
// 等待所有任务完成
latch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// 关闭线程池
executor.shutdown();
// 最终统计
long endTime = System.currentTimeMillis();
System.out.println("\n===== 数据生成全部完成 =====");
System.out.println("总耗时:" + (endTime - startTime) / 1000 + "");
System.out.println("成功条数:" + successCount.get());
System.out.println("失败条数:" + failCount.get());
}
}

View File

@@ -0,0 +1,53 @@
package com.example.demo.util;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import java.util.HashMap;
import java.util.Map;
/**
* 批量生成运证通问卷测试数据(调用后台接口)
*/
public class SurveyDataGenerator {
// 【修改为你的后台接口地址】
private static final String ADD_URL = "http://localhost:9080/app/survey/satisfaction";
public static void main(String[] args) {
// 生成10万条数据
int totalCount = 100000;
System.out.println("开始生成10万条问卷数据...");
for (int i = 0; i < totalCount; i++) {
// 生成随机分数 8/9/10
int q1 = (int) (Math.random() * 3) + 8;
int q2 = (int) (Math.random() * 3) + 8;
int q3 = (int) (Math.random() * 3) + 8;
int q4 = (int) (Math.random() * 3) + 8;
int q5 = (int) (Math.random() * 3) + 8;
// 封装接口参数
Map<String, Object> param = new HashMap<>();
param.put("q1", q1);
param.put("q2", q2);
param.put("q3", q3);
param.put("q4", q4);
param.put("q5", q5);
try {
// 调用后台接口
String result = HttpUtil.post(ADD_URL, JSONUtil.toJsonStr(param));
// 每1000条打印一次进度
if (i % 1000 == 0) {
System.out.println("已生成:" + (i + 1) + "条,接口返回:" + result);
}
} catch (Exception e) {
System.err.println("" + i + "条数据生成失败:" + e.getMessage());
}
}
System.out.println("10万条问卷数据生成完成");
}
}