三分钟,调用一个新增接口十万次

如下图,teacher表只有一条数据:

下面是调用teacher表新增接口十万次的代码:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class ApiCaller {
    public static void main(String[] args) throws Exception {
        // 开始时间
        long stime = System.currentTimeMillis();
        String apiUrl = "http://localhost:8888/teacher/addTeacherInfo";
        ObjectMapper objectMapper = new ObjectMapper();

        for (int i = 1; i <= 100000; i++) {
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setDoOutput(true);

            ObjectNode json = objectMapper.createObjectNode();
            String name = "张三" + i;
            json.put("tname", name);
            String hobby = "学习";
            json.put("hobby", hobby);

            try (OutputStream outputStream = connection.getOutputStream()) {
                objectMapper.writeValue(outputStream, json);
            }

            int responseCode = connection.getResponseCode();
            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
        }
        // 结束时间
        long etime = System.currentTimeMillis();
        // 计算执行时间
        System.out.printf("执行时长:%d 毫秒.", (etime - stime));
    }
}

开始调用:

 执行了140496毫秒,相当于2.3416分钟,三分钟都不到!!!

查看teacher表此刻有多少条数据:

操作成功,三分钟调用一个新增接口十万次!!!

猜你喜欢

转载自blog.csdn.net/weixin_55076626/article/details/129775059