1、Postman:模拟Http请求
2、Apache Bench(AB): Apache 附带的工具,测试网站性能
# ab -n 1000 -c 50 http://localhost:8080/hello
-n: 并发总数
-c: 允许一次请求并发数
3、JMeter: Apche 组织开发的压力测试工具
4、代码测试: 结合Semaphore、CountDownLatch
package com.current.flame.test;
import com.current.flame.annoations.NotThreadSafe;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
/**
* @author haoxiansheng
*/
@Slf4j
@NotThreadSafe
public class ConcurrencyTest {
// 请求总数
public static int clientTotal = 5000;
// 允许同时执行的线程并发数
public static int threadTotal = 200;
public static int count = 0;
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(threadTotal); // 信号量 允许有多少个同时执行
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal); // 闭锁
for (int i = 0; i < clientTotal; i++) {
executorService.execute(() -> {
try {
semaphore.acquire();
add();
semaphore.release();
} catch (Exception e) {
log.info("e=>{}", e.getMessage());
}
countDownLatch.countDown();
});
}
countDownLatch.await();
log.info("count==>{}", count);
executorService.shutdown();
}
/**
* 这是不安全的 加synchronized
*/
private static void add() {
count++;
}
}