RPC的TPS的计算

    RPC里面如何计算TPS,如果让你来计算这个TPS,你会怎么去实现呢?

    这是个有意思的问题,恰好这几天看SOFA-RPC的example,看到了里面是如果计算TPS的,这里分享下。

    如下List-1中是调用方的代码

  • 创建线程池,线程池的大小可按机器配置情况设置
  • 每个线程循环发送请求,然后将AtomicLong加1
  • 之后另一个线程,每隔1s,获取当前AtomicLong的值,然后减去上次AtomicLong的值,这样就是1s内请求的数量,即TPS

    List-1

final int threads = 50;
final AtomicLong cnt = new AtomicLong(0);
final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS,
    new SynchronousQueue<Runnable>());// 无队列
for (int i = 0; i < threads; i++) {
    service1.execute(new Runnable() {
        @Override
        public void run() {
            int n = 0;
            while (true) {
                try {
                    //echoService.echoStr("1234567890");
                    // 1k
                    //echoService.echoStr("..省略,1k个字符");
                    // 5k
                    LOGGER.info("send msg");
                    echoService.echoStr("..省略,5k个字符");
                    cnt.incrementAndGet();
                } catch (Exception e) {
                    LOGGER.error("", e);
                }
            }
        }
    });
}

Thread thread = new Thread(new Runnable() {
    private long last = 0;

    @Override
    public void run() {
        while (true) {
            long count = cnt.get();
            long tps = count - last;
            LOGGER.error("last 1s invoke: {}, queue: {}", tps, service1.getQueue().size());
            last = count;

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
    }
}, "Print-tps-THREAD");
thread.start();

    参考:SOFA-RPC的com.alipay.sofa.rpc.bolt.start.BoltClientMultipleMain

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章