HiSEN

性能指标的含义与计算方式:TP50、TP90、TP99、TP999

零、本文背景

一个接口的关键指标应该就是响应速度,要想提高响应速度,结果在缓存中最好;
所以通过查询时间与缓存中订单的时间进行对比,得出缓存过期时间的最佳值,以平衡性能与成本;
近期在做日志分析,找到比较理想的一个缓存过期时间,使90%的查询都能被缓存覆盖;

ps:提取日志的关键是要日志规范,方便切割,才好算出调用时间(适用于暴利分析,而不是通过调用链等方式);

一、性能指标含义

常见指标:TP50、TP90、TP99、TP999
正式解释:TP=Top Percentile,Top百分数,是一个统计学里的术语,与平均数、中位数都是一类;
通俗理解:TP99 100ms,99%的查询都能在100ms内返回;

二、性能指标计算

计算方式:拿100次调用的耗时,排序,取第99个的耗时,这就是TP99的值;

具体的代码方式就是,先算出耗时,放入List,然后排序,按指定的下标取值即可;

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private void calTime(List<Long> timeDurations) {
timeDurations.sort(Long::compareTo);
double tp50 = timeDurations.size() * 0.5;
double tp90 = timeDurations.size() * 0.9;
double tp99 = timeDurations.size() * 0.99;
double tp999 = timeDurations.size() * 0.999;

// 基础统计,包含max、min、count、avg信息;
DoubleSummaryStatistics doubleSummaryStatistics = timeDurations.stream().mapToDouble(Long::longValue).summaryStatistics();
System.out.println(JSON.toJSONString(doubleSummaryStatistics));

// Math.ceil() 向上取整
System.out.println("tp50:" + timeDurations.get((int) Math.ceil(tp50)));
System.out.println("tp90:" + timeDurations.get((int) Math.ceil(tp90)));
System.out.println("tp99:" + timeDurations.get((int) Math.ceil(tp99)));
System.out.println("tp999:" + timeDurations.get((int) Math.ceil(tp999)));
}