衡量服务性能的多线程程序 [英] Multithreaded program to measure the performance of service

查看:61
本文介绍了衡量服务性能的多线程程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在衡量我们服务的表现.所以我有一个 URL 可以调用我们的服务.所以我所做的是,在调用服务之前,我记下时间,在响应从服务返回后,我测量响应时间.我编写了一个程序来调用我的服务并通过将数字放入 HashMap 来测量性能-

I am in the process of measuing the performance of our service. So I have a URL that will make the call to our service. So what I did is that before making the call to the service, I make a note of the time and after the response came back from the service, I measure the response time. I wrote a program that was making the call to my service and measuring the performance by putting the numbers in a HashMap-

    while (runs > 0) {

        long start_time = System.currentTimeMillis();
        result = restTemplate.getForObject("Some URL", String.class);
        long difference = (System.currentTimeMillis() - start_time);

        Long count = histogram.get(difference);
        if (count != null) {
            count++;
            histogram.put(Long.valueOf(difference), count);
        } else {
            histogram.put(Long.valueOf(difference), Long.valueOf(1L));
        }
        runs--;
    }

所以我将从直方图中得到的输出将是 - X 次调用在 Y 毫秒内返回

So output I will be getting from the histogram map will be- X number of calls came back in Y ms

现在我在想,与其一次一个调用,不如说我应该并行化对我们服务的调用,就像在我之前的程序中一样,我正在一个接一个地调用服务.所以我写了一个下面的多线程程序将同时调用我的服务.那么下面的程序是否能够准确测量时差?

Now what I was thinking instead of making a single call at a time, why not I should parallelize the calls to our service, like in my previous program, I am hitting the service one by one. So I wrote a multithreading program below which will make a call to my service simultaneously. So the below program will be able to measure the time difference accurately or not?

就像一个线程花费这么多时间,第二个线程花费这么多时间,第三个线程花费这么多时间等等?可以这样做吗?

如果是的话,如果我下面的程序不能很好地工作,谁能告诉我该怎么做?

If yes, can anyone tell me how to do it if my below program doesn't work very well?

public static void main(String[] args) {

    ExecutorService service = Executors.newFixedThreadPool(10);

    for (int i = 0; i < 1 * 2; i++) {
        service.submit(new ThreadTask(i));
    }
    service.shutdown();
    try {
        service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
    } catch (InterruptedException e) {

    }
}


class ThreadTask implements Runnable {
    private int id;
    private RestTemplate restTemplate = new RestTemplate();
    private String result;
    private HashMap<Long, Long> histogram;

    public ThreadTask(int id) {
        this.id = id;
    }

    @Override
    public void run() {

        long start_time = System.currentTimeMillis();

        result = restTemplate.getForObject("Some URL",String.class);
        long difference = (System.currentTimeMillis() - start_time);

        Long count = histogram.get(difference);
        if (count != null) {
            count++;
            histogram.put(Long.valueOf(difference), count);
        } else {
            histogram.put(Long.valueOf(difference), Long.valueOf(1L));
        }

        System.out.println(histogram);
    }
}

因为每当我运行程序时,我从这个多线程程序中得到的数字看起来很奇怪.

Because whenever I run the program, the numbers I am getting from this multithreading program looks very weird.

我从非多线程程序得到的输出

168=1
41=1
43=3

1 个调用在 168 毫秒内返回 依此类推...

我从多线程程序中得到的输出

{119=1}
{179=1}
{150=1}
{202=1}
{149=1}

1 个调用在 119 ms 内返回 依此类推...所以在多线程程序中,我猜要花更多的时间?

1 call came back in 119 ms and so on... So in the multithreaded program, it is taking lot more time I guess?

推荐答案

我不明白你说的奇怪数字是什么意思.我的猜测是,这是因为来自不同线程的输出被穿插在一起.

I do not understand what you mean by getting weird numbers. My wild guess is that it is because output from different threads is getting interspersed.

解决此问题的一种方法是根本不从 run 方法打印 histogram.它已经是一个实例变量(尽管目前不需要),因此您可以:

One way to solve it is to not print the histogram from the run method at all. It is already an instance variable (though it currently does not need to be) so you can:

  1. 与其提交未命名的 ThreadTask 实例,不如将它们存储在列表/数组中.

  1. Instead of submitting unnamed instances of ThreadTask store them in a list/array.

创建一个打印histogram

在所有线程完成后,对每个顺序调用ThreadTask.report.

After all the threads have completed, call ThreadTask.report on each in sequence.

这篇关于衡量服务性能的多线程程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆