需要测量GET请求在多个线程上的时间

时间:2019-02-21 10:17:52

标签: multithreading api time

在我下面有一份工作,我想不通。

  

这是我编写的代码,我想执行多个调用,每次调用之后   迭代,以获取执行GET调用所需的确切时间。

     

最后,我想提取该数目的总执行时间   电话。

     

希望你能帮助我。

import org.junit.Assert;
    import org.slf4j.LoggerFactory;
    import org.springframework.util.StopWatch;
    import java.net.HttpURLConnection;
    import java.net.URL;

    public class PerformanceTests_Parallel {

        private static final org.slf4j.Logger logger = LoggerFactory.getLogger("Automation");

        public Thread threadStarter(int start, int end, int threadCount, String url) {
            Thread thread = null;

            for (int i = 1; i <= end; i++) {
                start = i;
                thread = getThread(start, end, threadCount, url);
                thread.start();
            }
            return null;
        }

        public static Thread getThread(int start, int end, int threadCount, String url) {
            Thread thread = new Thread() {
                public void run() {
                    try {
                        createGetCall(start, end, url);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

        private void createGetCall(int start, int end, String url) throws Exception {
                    URL obj = new URL(url);
                    HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
                    URL endpoint = new URL(url);
                    connection = (HttpURLConnection) endpoint.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setRequestProperty("Subject-id", "ma.ghigea@ab.com");
                    connection.setRequestProperty("Content-Type", "application/json");
                    connection.setDoOutput(true);
                    connection.setDoInput(true);
                    connection.setUseCaches(false);
                    connection.connect();



                   StopWatch watch = new org.springframework.util.StopWatch();
                   watch.start("IBAN");

                    int responseCode = connection.getResponseCode();

                    Assert.assertEquals(200, responseCode);

                    if(responseCode == 200) {
                        logger.info("Accessing endpoint was successful, response code is: " + responseCode);

                    }else {

                        logger.info("Accessing endpoint has failed, response code is: " + responseCode);
                    }

                 watch.stop();
                 logger.info("Time taken at iteration " +start+ " for the task " + watch.getLastTaskName() +
                           " is: " +watch.getLastTaskTimeMillis() + " miliseconds (" +watch.getTotalTimeSeconds() + " seconds)");

                }
            };
            return thread;

        }

        public static void main(String[] args) {
            PerformanceTests_Parallel obj = new PerformanceTests_Parallel();
            int start = 0;
            int end = 100;
            int threadCount = 10;
            String url = "http://someEndPoint/Accounts/BY86AKBB10500000002966008667";

            obj.threadStarter(start, end, threadCount, url);
        }

    }

请注意,在我的代码中,每次迭代的时间都在增加,因此我无法正确地测量时间。

0 个答案:

没有答案