api接口高并发测试
pom.xml
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> </dependency>
URLTest .java
package com.example.demo.controller; import java.io.File; import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; /**** * Executors.newSingleThreadExecutor()取得的Executor实例有以下特性: 任务顺序执行. 比如: executor.submit(task1); executor.submit(task2); 必须等task1执行完,task2才能执行。 task1和task2会被放入一个队列里,由一个工作线程来处理。即:一共有2个线程(主线程、处理任务的工作线程)。 以上如果是单机可以解决,如果是多台服务器的话,仍然还是会存在并发的问题. 或者使用mysql数据库修改的特性,update排序依次来修改,增加否决字段,修改的时候带上条件值,可以解决并发问题.(多个租客租时间段重叠的同一辆车且自动接单,这个情况就很明显凸现出来.) * @author admin * */ public class URLTest { private static int thread_num = 500; private static int client_num = 500; public static void main(String[] args) throws IOException { ExecutorService exec = Executors.newCachedThreadPool(); // thread_num个线程可以同时访问 final Semaphore semp = new Semaphore(thread_num); // 模拟client_num个客户端访问 for (int index = 0; index < client_num; index++) { final int NO = index; Runnable run = new Runnable() { public void run() { System.out.println("------------------------"); CloseableHttpClient httpclient = null; CloseableHttpResponse response = null; try { httpclient = HttpClients.createDefault(); HttpPost post = new HttpPost( "http://ip:8099/appDevice/cardInfo"); FileBody fileHead = new FileBody(new File("D:\\0fc756c37caf4f2cb63998892f81e7fc1538037176605MWYTMX.jpeg")); FileBody filePic = new FileBody(new File("D:\\3dea9aeb81bf457c8090ddfd3bcbb6431531478197207WEAJIR.jpg")); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); StringBody macAddress = new StringBody("10:d0:7a:de:89:5a"); StringBody name = new StringBody("赖真"); StringBody sex = new StringBody("1"); StringBody nation = new StringBody("汉"); StringBody birthday = new StringBody("1990-10-29"); StringBody address = new StringBody("江西省赣州市宁都县固村镇上旻村横石组"); StringBody idcardNo = new StringBody("XXXXXXXXXXXXXX"); StringBody signOrganization = new StringBody("宁都县公安局"); StringBody validDate = new StringBody("2017-03-24"); StringBody expireDate = new StringBody("2037-03-24"); StringBody signDate = new StringBody("2018-10-12 10:48:00"); StringBody tel = new StringBody("15123232323"); StringBody threshold = new StringBody("90.00000"); StringBody matchScore = new StringBody("12"); StringBody confidence = new StringBody("14"); builder.addPart("fileHead", fileHead); builder.addPart("filePic", filePic); builder.addPart("macAddress", macAddress); builder.addPart("name", name); builder.addPart("sex", sex); builder.addPart("nation", nation); builder.addPart("birthday", birthday); builder.addPart("address", address); builder.addPart("idcardNo", idcardNo); builder.addPart("signOrganization", signOrganization); builder.addPart("validDate", validDate); builder.addPart("expireDate", expireDate); builder.addPart("signDate", signDate); builder.addPart("tel", tel); builder.addPart("threshold", threshold); builder.addPart("matchScore", matchScore); //builder.addPart("pixel", pixel); builder.addPart("confidence", confidence); HttpEntity entity = builder.build(); post.setEntity(entity); response = httpclient.execute(post); if (200 == response.getStatusLine().getStatusCode()) { HttpEntity resultentity = response.getEntity(); String entityString = EntityUtils.toString(resultentity, "utf-8"); EntityUtils.consume(resultentity); System.out.println(entityString); } else { } } catch (Exception e) { e.printStackTrace(); } finally { if (response != null) { try { response.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (httpclient != null) { try { httpclient.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }; exec.execute(run); } // 退出线程池 exec.shutdown(); } }
好记性不如烂笔头
浙公网安备 33010602011771号