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.4</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 = 100,threads = 10)
    public void test() {

        TUser load = userService.load(1);
        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 = 100,threads = 10)
    public void test() {

        TUser load = userService.load(1);
        System.out.println(load.getPhone());

    }
    @Test
    public  void  test2()throws Exception{
        ExecutorService executorService = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(200);
        final CountDownLatch countDownLatch = new CountDownLatch(500);
        long l = System.currentTimeMillis();
        for (int i = 0; i < 200; i++) {
            final int count = i;
            executorService.execute(() -> {
                try {
                    semaphore.acquire();
                    TUser load = userService.load(1);
                    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個,直接把我數據庫搞炸了,

后來改了,也還是可以測試的,如果需要使用這個那么需要整合線程池了,不然那么多的連接夯在那里會一直不走


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM