一个程序员,需要时刻关注新的技术方向,或者回顾一些老的技术知识,来完善自己的技术栈,或者加深细化某些知识点,思而不学则怠,所以我们需要一些测试来夯实我们的技术理论,如何短时间(数小时)获取数以亿计的测试数据?
总体的思路是:定时任务,多线程,批量插入,优化数据库参数。
通过定时任务,可以让程序按照一定的时间(如几秒)自动触发一次;利用线程池,每次触发的时候,都有一个线程去处理程序,这样是单线程的数倍获取测试数据;在利用批量插入,可以一次插入50000条数据(具体量,可以根据jvm的内存看),每次程序循环执行100次,这样,一个线程,执行一次程序可以获取5000000的数据量,更具mysql的性能是没有问题的,大概需要两根分钟左右,多个线程一起,可以获取跟多的数据,具体程序如下:
测试实体(和数据库的table对应)
public classTbTest {
privateIntegerid;
privateStringname;
privateIntegerage;
privateDatebrithday;
privateIntegerhigh;
privateIntegerwigth;
privateStringschool;
privateStringhappy;
privateShortisboy;
省略getter,setter方法
}
多线程定时任务:
@Scheduled(cron ="0/30 * * * * ?")
public voidschudleThread()throwsException {
ExecutorService tp = Executors.newCachedThreadPool();
tp.execute(newRunnable() {
@Override
public voidrun() {
logger.info("==========开始批处理 当前的线程名称为 ==== "+Thread.currentThread().getName()+"=========");
String[] f =newString[]{"张","王","周","武","李",
"胡","赵","陈","苗","戴","习","毛","朱","韩","陆"};
String[] s =newString[]{"克","明","发","代发","犯的",
"和","我","人","同","娟","娟娟","丽","美丽","利","陆"
,"空间","改","办法","航空",
"留","泰","晨光","长城","层层","莉莉","胡霍","娜娜","大","光荣"};
Random random =newRandom();
longc = System.currentTimeMillis();
List tests =newArrayList<>(100);
for(inti =0; i <500000; i++) {
if(i >0&& (i %40000) !=0) {
TbTest tbTest =newTbTest();
tbTest.setAge(random.nextInt(100));
try{
tbTest.setBrithday(DateUtil.parseDate(random.nextInt(2017) +"-"+ random.nextInt(12)
+"-"+ random.nextInt(30) +" "+ random.nextInt(24) +":"+ random.nextInt(60) +":"+ random.nextInt(60),"yyyy-MM-dd HH:mm:ss"));
}catch(ParseException e) {
e.printStackTrace();
}
tbTest.setHigh(random.nextInt(200));
tbTest.setIsboy((short) random.nextInt(2));
tbTest.setName(f[random.nextInt(f.length-1)] + s[random.nextInt(s.length-1)]);
tbTest.setSchool("学院"+ random.nextInt(10000));
tbTest.setWigth(random.nextInt(200));
tests.add(tbTest);
}
if(i >0&& (i %40000) ==0) {
tbTestMapper.inserts(tests);
tests.clear();
}
}
logger.info("耗时:"+ (System.currentTimeMillis() - c) /1000);
}
});
}
mapper文件
@Component
public interfaceTbTestMapper {
intbatchinsert(@Param("tests") List tests);
}
xml文件sql
insert into tb_test
name,
age,
brithday,
high,
wigth,
school,
happy,
isboy