欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java多线程测试springboot的并发响应返回正确性

程序员文章站 2024-03-19 13:39:40
...

1.springboot的被测试函数

	/**
	 * 测试多线程返回
	 * @param request
	 * @param response
	 * @param jsonParam
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/api/test", method = RequestMethod.POST)
	public Object test(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject jsonParam)
			throws Exception {
		Map<String, Object> requestmap = new HashMap<String, Object>();// 客户端原始请求数据
		Map<String, Object> resultmap = new HashMap<String, Object>();// 返回给客户端的结果		
		
		requestmap = JsonUtils.JsonToMapObj(jsonParam.toJSONString());
		int j = 0;
		for(int i=0;i<10000000;i++) {
			j = j + 1;
		}
		resultmap.put("threadNo", requestmap.get("threadNo"));
		return resultmap;
	}

通过比对上送和接收的threadNo是否一致来判断springboot并发返回的正确性

当中的循环模拟实际操作开销

2.线程类

package com.gg.lxz.test;

import java.util.HashMap;
import java.util.Map;

import com.gg.lxz.communication.http.GGHttpRequest;
import com.gg.lxz.utils.JsonUtils;

public class SubClass implements Runnable {
	
	private String threadNo;

	public void setThreadNo(String threadNo) {
		this.threadNo = threadNo;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		GGHttpRequest request = new GGHttpRequest();
		String url = "http://127.0.0.1:16555/api/test";
		Map<String, Object> req = new HashMap<String, Object>();
		Map<String, Object> resp = new HashMap<String, Object>();
		req.put("threadNo", this.threadNo);
		resp = request.dopost(url, JsonUtils.MapToJson(req));
		resp.put("reqthreadNo", this.threadNo);
		System.out.println("response threadNo = "+resp);
	}

}

3.测试类 

		for (int i=0;i<500;i++){
			SubClass obj = new SubClass();
			obj.setThreadNo(new Integer(i).toString());
			Thread thread = new Thread(obj);
	        thread.start();
		}