Java模拟高并发上传数据

Java模拟高并发上传数据

参考博客:JAVA 模拟瞬间高并发

在这一篇博客,我会记录整个我模拟高并发的过程。

从参考的博客那里,我学会了使用线程池和CountDownLatch。

一、模拟高并发初试(小菜)

这个转载的代码,我自己进行尝试之后,为帮助学习,添加了易于了解的注释。


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 模拟大坏蛋和小小兵 主线程代表大坏蛋,新建n个小黄人,等待坏蛋下指令; 
 * 大坏蛋下了指令,小黄人开始执行任务,执行完之前,坏蛋要一直等着。
 * 小小兵执行完任务,大坏蛋才能下发新的指令。
 * 
 * @author Administrator
 *
 */
public class CountdownLatchTest {

    public static void main(String[] args) {
        // 创建一个线程池(一个军队)
        ExecutorService service = Executors.newCachedThreadPool();
        // 坏蛋指令,默认为1,变为0时,执行命令
        final CountDownLatch cdOrder = new CountDownLatch(1);

        //小黄人数量
        final int soilder_count = 6;

        // n个小黄人,一个小黄人执行完任务,cdAnswer减1,n个小黄人执行完毕,cdAnswer为0
        final CountDownLatch cdAnswer = new CountDownLatch(soilder_count);

        for (int i = 0; i < soilder_count; i++) {

            Runnable runnable = new Runnable() {

                @Override
                public void run() {
                    //小黄人整装待发
                    System.out.println("小黄人 : " + Thread.currentThread().getName() + " 准备好了.");

                    try {
                        cdOrder.await();//等着坏蛋发出指令
                        //小黄人收到指令
                        System.out.println("小黄人 : " + Thread.currentThread().getName() + " 接受命令.");

                        Thread.sleep((long) (Math.random() * 10000));
                        //执行完毕,告诉大坏蛋。
                        System.out.println("小黄人 : " + Thread.currentThread().getName() + " 告诉大坏蛋,任务完成.");

                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } finally {
                        //执行一个减少一次
                        cdAnswer.countDown();
                    }

                }
            };

            service.execute(runnable);// 往执行军队里加派小黄人
        }

        try {
            Thread.sleep((long) (Math.random() * 10000));
            System.out.println("After 10s.....");

            //马上就要下达指令啦!
            System.out.println("大坏蛋 : " + Thread.currentThread().getName() + " 将要下发指令.");

            //发布指令,等待他们执行完
            System.out.println("大坏蛋 : " + Thread.currentThread().getName() + " 发布指令了,快去执行,等你们消息");

            cdOrder.countDown();// 发送指令“小黄人出动!”

            cdAnswer.await(); // 大坏蛋等着小黄人回来

            //收到所有完成的反馈
            System.out.println("大坏蛋 : " + Thread.currentThread().getName() + " 任务都完成了,收到了所有的反馈.");

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        service.shutdown();// 整个军队撤回!
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

代码说明:

1. ExecutorService service = Executors.newCachedThreadPool();
这行代码创建了一个线程池。用于存放阻塞的线程。
2. final CountDownLatch cdOrder = new CountDownLatch(1);
这行代码就是开关,如何让阻塞在线程池里的线程全部都启动,就像军队里的士兵一样,一个命令让他们全部出动。
cdOrder.countDown();则值减一,值为零时,就打开开关了。
3. final CountDownLatch cdAnswer = new CountDownLatch(soilder_count);
和上一个CountDownLatch 类似,但是这里的值将为一时,表示线程都执行结束了。

关于线程池和CountDownLatch ,建议参考博客:
- Java线程池 ExecutorService
- ExecutorService深入理解
- Java线程池类ThreadPoolExecutor、ScheduledThreadPoolExecutor及Executors工厂类

执行结果如下:

这里写图片描述

二、真实上传数据测试接口

模拟上传数据是用okhttp来进行文件的post上传,分别进行了10个并发、20个并发进行测试,每个线程单个文件大小11M。

测试demo:


import java.io.File;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 模拟高并发上传数据
 * 
 * @author Administrator
 *
 */
public class LocalUploadData {


    public static void main(String[] args) {
        // 创建一个线程池
        ExecutorService service = Executors.newCachedThreadPool();

        // 计划放置10个线程
        final int countLatch = 10;

        // 线程结束标记
        final CountDownLatch dataLatch = new CountDownLatch(countLatch);

        // 执行指令
        final CountDownLatch isExecute = new CountDownLatch(1);

        // 上传的数据路径和文件名
        String path = "F:/DataTest/";

        for (int i = 0; i < countLatch; i++) {

            String file_num = "(" + (i + 1) + ")";

            Runnable runnable = new Runnable() {

                String file_name = "data-test-" + file_num + ".zip";// data-test-(1).zip

                String file_path = path + file_name;

                LocalUploadData udc = new LocalUploadData();

                @Override
                public void run() {
                    System.out.println("current file_name=" + file_name);
                    System.out.println(Thread.currentThread().getName() + "准备好了...");
                    try {
                        //等待isExecute的值变为0,然后执行
                        isExecute.await();// 等待执行

                        System.out.println(Thread.currentThread().getName() + "开始上传>>>>>>>>");
                        long start_time = System.currentTimeMillis();

                        // 上传数据
                        int response_code = udc.httpPostData(file_path, file_name);
                        System.out.println("响应结果:"+response_code);

                        long end_time = System.currentTimeMillis();
                        long using_time = end_time - start_time;

                        // 最后一个文件上传目前不启用
                        System.out.println(Thread.currentThread().getName() + "用时 : " + using_time + " ms");

                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } finally {
                        dataLatch.countDown();
                    }
                }
            };
            service.execute(runnable);//把线程都放进线程池里等待
        }

        try {
            Thread.sleep((long) (Math.random() * 10000));
            System.out.println("After 10s.....");

            System.out.println(Thread.currentThread().getName() + " 准备上传!");

            System.out.println(Thread.currentThread().getName() + " 开始上传");

            //isExecute的值减一,开始执行
            isExecute.countDown();

            //等待dataLatch的值变为0,表示线程全部执行结束。
            dataLatch.await();

            System.out.println(Thread.currentThread().getName() + " 上传结束.");

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            service.shutdown();
        }
    }

/**
* okhttp上传数据demo
*/
    public int httpPostData(String file_path, String filename) {

        OkHttpClient.Builder client = new OkHttpClient.Builder().connectTimeout(30, TimeUnit.SECONDS).readTimeout(80,
                TimeUnit.SECONDS);

        File file = new File(file_path);

        RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), file);

        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getName(), fileBody)
                .addFormDataPart("file_name", filename).build();

        Request request = new Request.Builder().url("http://192.168.1.68:8080/upload-data/l/localUploadData").post(body).build();
        Response response;
        try {
            response = client.build().newCall(request).execute();
            String jsonString = response.body().string();
            if (!response.isSuccessful()) {
                System.out.println("NetworkException>>" + response.code() + "\n" + jsonString);
            } else {
                System.out.println("response>>" + response.code() + "\n" + jsonString);
                return response.code();

            }
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }

        return 0;

    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148

一个接口分别在两个地方进行测试:
- 本地jetty-run运行
- 部署在阿里云服务器上

1、本地jetty-run运行的接口
这里写图片描述
这里写图片描述

从上面三组测试结果能看出,10组数据响应时间<6s,这个还能在心理接受范围之内,但是20组数据的时候,最短的响应时间>6s,最长的响应时间超过了27s。

在并发数据大的时候,线程阻塞导致没有一个线程能极速处理,这样每个处理时间都很长,我预想的响应效果,起码要达到从3s到20s之间,就是有的用户响应快,有的响应慢,根据用户的网速从而达到一个梯度,而不是让所有用户都觉得慢。

2、部署在阿里云服务器上的接口

部署到线上的测试更是惨不忍睹,非常非常的慢,已经超出我能容忍的范围,这是让人抓狂的慢。
这里写图片描述
这里写图片描述
这里写图片描述

从这里能看到,其实服务器写出文件速度非常快的,但是接口响应很慢,能达到85s。(╯‵皿′)╯︵┻━┻

以上就是高并发测试上传数据的demo和测试记录。
下一篇就是针对这么慢的响应进行接口的优化。

猜你喜欢

转载自blog.csdn.net/weinichendian/article/details/80291459
今日推荐