您的位置:首页 > 编程语言 > Java开发

java性能测试比较模拟代码

2017-11-01 18:10 417 查看
package com.eloancn.back.schedule.core.hero;

import java.util.Map;
import java.util.concurrent.CountDownLatch;

public class Hero {

public static void main(String[] args) {
Hero hero = new Hero();
try {
hero.yibu();
} catch (Exception e) {
e.printStackTrace();
}
}

//cost time:30021
private void tongbu() {
try {
System.out.println("begin ");
Long begin = System.currentTimeMillis();
RPCService.getRpcResult();
HTTPService.getHttpResult();
long end = System.currentTimeMillis();
System.out.println("cost time:" + (end - begin));
} catch (Exception e) {
e.printStackTrace();
}
}

//cost time:20008
private void yibu() {
System.out.println("begin ");
Long begin = System.currentTimeMillis();
final CountDownLatch countDownLatch = new CountDownLatch(2);
new Thread(new Runnable() {
@Override
public void run() {
try {
RPCService.getRpcResult();
countDownLatch.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();

new Thread(new Runnable() {
@Override
public void run() {
try {
HTTPService.getHttpResult();
countDownLatch.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();

try {
countDownLatch.await();
} catch (Exception e) {
e.printStackTrace();
}

long end = System.currentTimeMillis();
System.out.println("cost time:" + (end - begin));
}
}

class RPCService {
static void getRpcResult() throws Exception {
try {
//休眠10秒
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
}
}

class HTTPService {
static void getHttpResult() throws Exception {
try {
//休眠20秒
Thread.sleep(20000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: