相关
雪崩 - 如何重试 - sla和重试风暴的双保证_个人渣记录仅为自己搜索用的博客-CSDN博客
接口耗时公式
耗时= cpu时间 + io时间
线程池数量
最佳数目 = 1s / 平均cpu时间 * 内核数.
最大平均cpu时间 = 接口耗时- all外部io时间. 结合gc , linux本身其他线程, 只会还少点.
当前我们是没有统计的.
小实验:
如附录, 200次, 2000次调用 11毫秒, 20ms 基本是50个线程池 * 8核 = 400 .
线程队列的作用
线程队列的好处就是 线程数目满了之后, 放到队列里 .
坏处是 堆积, 堆积的时间也会提现在api接口耗时中.
修正后的接口耗时
耗时 = 队列等待时间 + cpu时间 + io时间
注意 : 队列等待时间 不是 真正执行时间.
突破资源边界后的稳定性问题
队列等待时间 = 队列数 * 接口真正执行平均耗时 ( cpu时间 + io时间 ) > 上游超时时间.
继续重试, 队列继续填满 ,队列永远满.
附录:
200次方法调用.
public void test(){
StopWatch stopWatch=new StopWatch("t1");
stopWatch.start("initStopWatch");
stopWatch.stop();
stopWatch.start("newHashMap");
Map<String,Object> map= Maps.newHashMap();
int count=200;
for (int i = 0; i < 200; i++) {
put(map, i);
}
stopWatch.stop();
stopWatch.start("initJson");
Map<String,Object> map2= Maps.newHashMap();
map2.put("1",1);
String value1= JSON.toJSONString(map2);
String nu1=value1;
stopWatch.stop();
stopWatch.start("json");
String value= JSON.toJSONString(map);
String nu=value;
stopWatch.stop();
System.out.println("count="+count+",costTimeMs="+stopWatch.getTotalTimeMillis()+",cost="+stopWatch.prettyPrint());
}
private void put(Map<String,Object> map, int i) {
map.put("i"+i,1+i);
}
200次方法调用 8毫秒
count=200,costTimeMs=103,cost=StopWatch 't1': running time = 103530566 ns
---------------------------------------------
ns % Task name
---------------------------------------------
000002786 000% initStopWatch
008225391 008% newHashMap
094716069 091% initJson
000586320 001% json
2000次调用 11毫秒
count=2000,costTimeMs=121,cost=StopWatch 't1': running time = 121049705 ns
---------------------------------------------
ns % Task name
---------------------------------------------
000002787 000% initStopWatch
011 254 439 009% newHashMap
106417291 088% initJson
003375188 003% json
2万次调用 36毫秒
count=20000,costTimeMs=224,cost=StopWatch 't1': running time = 224199250 ns
---------------------------------------------
ns % Task name
---------------------------------------------
000004506 000% initStopWatch
036 672 628 016% newHashMap
167984038 075% initJson
019538078 009% json