springboot项目中进行并发测试
一 利用工具包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.databene</groupId>
<artifactId>contiperf</artifactId>
<version>2.3.</version>
<scope>test</scope>
</dependency>
引入者两个依赖:
就可以进行测试了,看测试代码:
package com.cxy.springs; import com.cxy.springs.entity.TUser;
import com.cxy.springs.service.TUserService;
import org.databene.contiperf.PerfTest;
import org.databene.contiperf.junit.ContiPerfRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringsApplicationTests {
@Autowired
public TUserService userService;
@Test
public void contextLoads() {
}
//引入 ContiPerf 进行性能测试
@Rule
public ContiPerfRule contiPerfRule = new ContiPerfRule(); @Test
//10个线程 执行10次
@PerfTest(invocations = ,threads = )
public void test() { TUser load = userService.load();
System.out.println(load.getPhone()); }
}
结果:
不知道为什么我的图片挂了
第二种方式:
利用concurrent包下列进行测试,不过他们没有具体的相应时间:
package com.cxy.springs; import com.cxy.springs.entity.TUser;
import com.cxy.springs.service.TUserService;
import org.databene.contiperf.PerfTest;
import org.databene.contiperf.junit.ContiPerfRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringsApplicationTests {
@Autowired
public TUserService userService;
@Test
public void contextLoads() {
}
//引入 ContiPerf 进行性能测试
@Rule
public ContiPerfRule contiPerfRule = new ContiPerfRule(); @Test
//10个线程 执行10次
@PerfTest(invocations = ,threads = )
public void test() { TUser load = userService.load();
System.out.println(load.getPhone()); }
@Test
public void test2()throws Exception{
ExecutorService executorService = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore();
final CountDownLatch countDownLatch = new CountDownLatch();
long l = System.currentTimeMillis();
for (int i = ; i < ; i++) {
final int count = i;
executorService.execute(() -> {
try {
semaphore.acquire();
TUser load = userService.load();
System.out.println(load.getPhone());
semaphore.release();
} catch (Exception e) {
// log.error("exception" , e);
}
countDownLatch.countDown();
});
}
countDownLatch.await();
long a = System.currentTimeMillis();
System.out.println(a-l); executorService.shutdown(); //log.info("size:{}" , map.size());
}
}
这个开始时候给了5000个,直接把我数据库搞炸了,
后来改了,也还是可以测试的,如果需要使用这个那么需要整合线程池了,不然那么多的连接夯在那里会一直不走
springboot项目中进行并发测试的更多相关文章
- SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL
1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...
- Springboot项目中 前端展示本地图片
Springboot项目中 前端展示本地图片 本文使用的是Springboot官方推荐的thymeleaf(一种页面模板技术) 首先在pom文件加依赖 <dependency> <g ...
- 在SpringBoot项目中添加logback的MDC
在SpringBoot项目中添加logback的MDC 先看下MDC是什么 Mapped Diagnostic Context,用于打LOG时跟踪一个“会话“.一个”事务“.举例,有一个web ...
- 自身使用的springboot项目中比较全的pom.xml
在学习的时候常建新的项目,mark下商用的jar <dependency> <groupId>org.mybatis</groupId> <artifactI ...
- springboot 项目中获取默认注入的序列化对象 ObjectMapper
在 springboot 项目中使用 @SpringBootApplication 会自动标记 @EnableAutoConfiguration 在接口中经常需要使用时间类型,Date ,如果想要格式 ...
- springboot项目中js、css静态文件路径访问
springboot静态文件访问的问题,相信大家也有遇到这个问题,如下图项目结构. 项目结构如上所示,静态页面引入js.css如下所示. 大家肯定都是这样写的,但是运行的话就是出不来效果,图片也不显示 ...
- 解决springboot项目中@Value注解参数值为null的问题
1.错误场景: springboot项目中在.properties文件(.yml)文件中配置了属性值,在Bean中使用@Value注解引入该属性,Bean的构造器中使用该属性进行初始化,此时有可能会出 ...
- springboot项目中引用其他springboot项目jar
1. 剔除要引入的springboot项目中不需要的文件:如Application和ApplicationTests等 2.打包 不能使用springboot项目自带的打包插件进行打包: 3.打包 4 ...
- 在前后端分离的SpringBoot项目中集成Shiro权限框架
参考[1].在前后端分离的SpringBoot项目中集成Shiro权限框架 参考[2]. Springboot + Vue + shiro 实现前后端分离.权限控制 以及跨域的问题也有涉及
随机推荐
- (转)Google Protocol Buffer 的使用和原理
转自:https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html 简介 什么是 Google Protocol Buffer? ...
- .Net Core JWT Bearer 的认证
关于JWT原理在这不多说,主要由三部分组成:Header.Payload.Signature,有兴趣自己上网了解. 1.首先创建.Net Core 一个Api项目 2.添加 JWT 配置 2.1 修改 ...
- Windows服务调试状态下用Console启动
最近一直在用服务,发现服务也没有那么难调试. Windows服务调试状态下用Console启动:步骤分两步 第一步改Program,启动代码 static class Program { /// &l ...
- SVN Cannot merge into a working copy that has local modifications
我尝试了 主支,分支都提交,但是依然无法合并. 最终,我在服务器上将分支删除,然后主支在拷贝过去. 一,打开服务器资源 二,删除分支 三,拷贝主支到分支 四,刷新分支,就能看到了. 然后在分支项目中, ...
- Android Studio Gradle无法获取pom文件
错误提示: Error:Execution failed for task ':app:lintVitalRelease'. > Could not resolve all artifacts ...
- CSIC_716_20191031【计算机的组成】
引言 什么是编程语言 语言是人与人之间的沟通介质之一,编程语言是人与机器(包括计算机)之间沟通的介质. 一个完整的计算机系统主要包括 应用程序 .操作系统 和硬件 等. 计算机三大核心 ...
- npm run server报错
从git上clone的vue项目npm install后npm run server报错 $ npm run dev > lufei@1.0.0 dev E:\pythonProject\luf ...
- 洛谷 P2114 [NOI2014]起床困难综合症
题目描述 21世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm一直坚持与起床困难综合症作斗争.通过研究相关文献,他找到了该病的发病原因 ...
- System.String.cs
ylbtech-System.String.cs 1.程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5619 ...
- PAT甲级——A1134 Vertex Cover【25】
A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...