模拟高并发的测试(多线程开启httpclient请求接口)

模拟高并发的原理就是:利用线程池去执行多个线程通过httpclient访问controller 

package com.xtzn.utils;


import com.alibaba.fastjson.JSONObject;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpStatus;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Create by PSY
 *
 * @date: 2018/10/22 上午9:30
 * @version:4.0
 * @Description:模拟高并发访问controller
 */

public class MyTest {

    final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) throws Exception {

        ThreadPoolExecutor executor = new ThreadPoolExecutor(2000, 5000, 2, TimeUnit.MINUTES,
                new ArrayBlockingQueue<Runnable>(5000));
        //模拟100人并发请求
        CountDownLatch latch = new CountDownLatch(1);
        //模拟100个用户
        for (int i = 0; i < 100; i++) {

            AnalogUser analogUser = new AnalogUser(latch);
            executor.execute(analogUser);

        }
        //计数器減一  所有线程释放 并发访问。
        latch.countDown();

    }


    static class AnalogUser implements Runnable {
        CountDownLatch latch;


        public AnalogUser(CountDownLatch latch) {
            this.latch = latch;

        }


        @Override
        public void run() {
            long starTime = 0;
            try {
                starTime = System.currentTimeMillis();
                latch.await();
                System.out.println("请求开始了");
                JSONObject jsonObject = httpPost("http://192.168.100.43:8080/lua/requestIdfa", "userId=3&bundleName=xxxxxx&country=US&thirdParty=sdfsfs");
                System.out.println("返回的状态码为:"+jsonObject.get("code"));
                System.out.println("返回的状态信息:"+jsonObject.get("desc"));
                System.out.println("返回的内容为:"+jsonObject.get("content"));

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            long endTime = System.currentTimeMillis();
            Long t = endTime - starTime;
            System.out.println(t / 1000F + "秒");
        }


    }

    static JSONObject httpPost(String url, String strParam) {

        // post请求返回结果
        CloseableHttpClient httpClient = HttpClients.createDefault();
        JSONObject jsonResult = null;
        HttpPost httpPost = new HttpPost(url);
        // 设置请求和传输超时时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(2000).setConnectTimeout(2000).build();
        httpPost.setConfig(requestConfig);
        try {
            if (null != strParam) {
                // 解决中文乱码问题
                StringEntity entity = new StringEntity(strParam, "utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/x-www-form-urlencoded");
                httpPost.setEntity(entity);
            }
            CloseableHttpResponse result = httpClient.execute(httpPost);
            //请求发送成功,并得到响应
            if (result.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
                String str;
                try {
                    //读取服务器返回过来的json字符串数据
                    str = EntityUtils.toString(result.getEntity(), "utf-8");
                    //把json字符串转换成json对象
                    jsonResult = JSONObject.parseObject(str);
                } catch (Exception e) {

                }
            }
        } catch (IOException e) {

        } finally {
            httpPost.releaseConnection();
        }
        return jsonResult;
    }


}

模拟的结返回结果如下图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值