Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多线程测试以测试站点/web服务的响应时间_Java_Multithreading_Concurrency_Junit_Java.util.concurrent - Fatal编程技术网

Java 多线程测试以测试站点/web服务的响应时间

Java 多线程测试以测试站点/web服务的响应时间,java,multithreading,concurrency,junit,java.util.concurrent,Java,Multithreading,Concurrency,Junit,Java.util.concurrent,下面的代码测试将www.google.com读入BufferedReader的响应时间。我计划使用这段代码测试intranet中其他站点和web服务的响应时间。以下测试运行20秒,每秒打开4个请求: import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.Map.Entry; impor

下面的代码测试将www.google.com读入BufferedReader的响应时间。我计划使用这段代码测试intranet中其他站点和web服务的响应时间。以下测试运行20秒,每秒打开4个请求:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.junit.Test;

public class ResponseTimeTest {

    private static final int NUMBER_REQUESTS_PER_SECOND = 4;
    private static final int TEST_EXECUTION_TIME = 20000;
    private static final ConcurrentHashMap<Long, Long> timingMap = new ConcurrentHashMap<Long, Long>();

    @Test
    public void testResponseTime() throws InterruptedException {

        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10);
        scheduler.scheduleAtFixedRate(new RequestThreadCreator(), 0, 1, TimeUnit.SECONDS);      
        Thread.sleep(TEST_EXECUTION_TIME);

        System.out.println("Start Time, End Time, Total Time");
        for (Entry<Long, Long> entry : timingMap.entrySet())
        {
            System.out.println(entry.getKey() + "," + entry.getValue() +","+(entry.getValue() - entry.getKey()));
        }


    }

    private final class RequestThreadCreator implements Runnable {

        public void run() {

            ExecutorService es = Executors.newCachedThreadPool();

            for (int i = 1; i <= NUMBER_REQUESTS_PER_SECOND; i++) {
                es.execute(new RequestThread());
            }
            es.shutdown();

        }
    }

    private final class RequestThread implements Runnable {

        public void run() {

            long startTime = System.currentTimeMillis();
            try {
                URL oracle = new URL("http://www.google.com/");
                URLConnection yc = oracle.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));

                while ((in.readLine()) != null) {
                }
                in.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
            long endTime = System.currentTimeMillis();
            timingMap.put(startTime, endTime);
        }
    }

}

由于System.out.println是同步的,为了不扭曲结果,我将计时添加到ConcurrentHashMap中,并且不在RequestThread本身内输出计时。在上面的代码中,我应该注意其他的gotcha,这样就不会扭曲结果。或者我应该专注于哪些方面来提高精度,或者它是否足够精确,精确到大约100毫秒。

只是一个意见,你应该使用JMeter来测试。明白了:计时图。putstartTime,…-startTime很可能在不同的线程中具有相同的值,尤其是,如果您多次运行此操作,则最好让您的映射由某个唯一的id设置键,原子整数将执行并保存开始、结束对的值。@VictorSorokin为什么计时映射在不同的线程中包含相同的值?访问System.currentTimeMillis不会修改状态,所以线程安全吗?为什么不能?您的线程将在同一毫秒内调用它,这是完全可能的。@Victor Sorokin我接受两个线程可能会同时调用System.currentTimeMillis,但为什么这是一个问题?如果两个线程调用System.currentTimeMillis,那么这是该线程的正确开始或结束时间,startTime和endTime变量包含在线程中,因此状态不应该重叠?你是说System.currentTimeMillis不是一个原子操作,所以线程可以在读取时修改它的值?也可以使用Thread.currentThread.getId代替原子整数?
Start Time, End Time, Total Time
1417692221531,1417692221956,425
1417692213530,1417692213869,339
1417692224530,1417692224983,453
1417692210534,1417692210899,365
1417692214530,1417692214957,427
1417692220530,1417692221041,511
1417692209530,1417692209949,419
1417692215532,1417692215950,418
1417692214533,1417692215075,542
1417692213531,1417692213897,366
1417692212530,1417692212924,394
1417692219530,1417692219897,367
1417692226532,1417692226876,344
1417692211530,1417692211955,425
1417692209529,1417692209987,458
1417692222531,1417692222967,436
1417692215533,1417692215904,371
1417692219531,1417692219954,423
1417692215530,1417692215870,340
1417692217531,1417692218035,504
1417692207547,1417692207882,335
1417692208535,1417692208898,363
1417692207544,1417692208095,551
1417692208537,1417692208958,421
1417692226533,1417692226899,366
1417692224531,1417692224951,420
1417692225529,1417692225957,428
1417692216530,1417692216963,433
1417692223541,1417692223884,343
1417692223546,1417692223959,413
1417692222530,1417692222954,424
1417692208532,1417692208871,339
1417692207536,1417692207988,452
1417692226538,1417692226955,417
1417692220531,1417692220992,461
1417692209531,1417692209953,422
1417692226531,1417692226959,428
1417692217532,1417692217944,412
1417692210533,1417692210964,431
1417692221530,1417692221870,340
1417692216531,1417692216959,428
1417692207535,1417692208021,486
1417692223548,1417692223957,409
1417692216532,1417692216904,372
1417692214535,1417692215071,536
1417692217530,1417692217835,305
1417692213529,1417692213954,425
1417692210531,1417692210964,433
1417692212529,1417692212993,464
1417692213532,1417692213954,422
1417692215531,1417692215957,426
1417692210529,1417692210868,339
1417692218531,1417692219102,571
1417692225530,1417692225907,377
1417692208536,1417692208966,430
1417692218533,1417692219168,635