背景
性能测试中,我们经常选择TP90、TP95、TP99等指标项作为性能对比的参考水位, 在本文中,我们给出一种计算 TP90、TP95 和 TP99 等水位线的方法,首先我们解释一下TP90、TP95、TP99的含义.
- TP90: 即 90% 的数据都满足某一条件.
- TP95: 即 95% 的数据都满足某一条件.
- TP99: 即 99% 的数据都满足某一条件.
我们之所以说其“满足某一条件”,是因为在计算的时候,我们既可以向前计算也可以向后计算,例如:
- 1, 2, 3, …, 98, 99, 100
如上所示,这是一个从 1 至 100 的数列,如果我们想计算其 TP99 的值,其方法为用数列中数值的总个数乘以 99%,即100 * 99% = 99,显然在这个数列中有两个数值满足这个 99 的概念,分别为:
2: 即数列中 99% 的数值都大于等于299: 即数列中 99% 的数值都小于等于99
因此,TP90、TP95 或者 TP99 等水位线是有两种含义的,具体选择哪一种,我们可以按需求自己选择。
TreeMap
: key为耗时,value为该耗时下的数据个数,TreeMap是红黑色树数据结构实现,可在初始化时指定其key的排序规则(从小到大):
TreeMap
treeMap = new TreeMap<>(Comparator.naturalOrder());
TreeMap
treeMap = new TreeMap<>(Comparator.naturalOrder());
Long total=treeMap.values().stream().mapToLong(o->o).sum(); total:20
接下来完成对TP90、TP95和TP99的index计算:
Double idx90= Math.ceil((double)total*90/100); idx90:19 Double idx95= Math.ceil((double)total*95/100); idx95:20 Double idx99= Math.ceil((double)total*99/100); idx99:20
此时我们可以看到TP90的index为19,TP95和TP99的index为20,根据TreeMap的耗时分部情况如下:

那如何将index和我们的区间数据进行映射找寻出该区间对应的耗时呢? 我们可以通过对treeMap数据进行一次recordMap转换,recordMap key记录耗时的第一个idx,value则为该耗时数值:
TreeMap
transMap = new TreeMap<>(Comparator.naturalOrder()); AtomicReference
idx= new AtomicReference<>(0l); treeMap.forEach((key,value)->{ transMap.put(idx.get(),key); idx.updateAndGet(v -> v + value); });
则转换后的数据如下:
{0=1, 1=2, 3=3, 6=4, 8=5, 11=6, 12=7, 14=9, 17=10}
然后可以通过java8提供的floorEntry返回小于或等于给定的键的值映射:
Long total=treeMap.values().stream().mapToLong(o->o).sum(); Double idx90= Math.ceil((double)total*90/100); Double idx95= Math.ceil((double)total*95/100); Double idx99= Math.ceil((double)total*99/100); Map.Entry
idx90Entry = transMap.floorEntry(idx90.longValue()); Map.Entry
idx95Entry = transMap.floorEntry(idx95.longValue()); Map.Entry
idx99Entry = transMap.floorEntry(idx99.longValue()); Long TP90=idx90Entry.getValue(); Long TP95=idx95Entry.getValue(); Long TP99=idx99Entry.getValue();
代码实现
整体代码实现如下:
@Test public void test03(){ TreeMap
treeMap = new TreeMap<>(Comparator.naturalOrder()); // <1,1> <5,3> <6,1> <2,2> <3,3> <4,2> <7,2> <9,3> <10,3> treeMap.put(1l,1l); treeMap.put(5l,3l); treeMap.put(6l,1l); treeMap.put(2l,2l); treeMap.put(3l,100l); treeMap.put(4l,2l); treeMap.put(7l,2l); treeMap.put(9l,3l); treeMap.put(10l,3l); TreeMap
transMap = new TreeMap<>(Comparator.naturalOrder()); AtomicReference
idx= new AtomicReference<>(0l); treeMap.forEach((key,value)->{ transMap.put(idx.get(),key); idx.updateAndGet(v -> v + value); }); Long total=treeMap.values().stream().mapToLong(o->o).sum(); Double idx90= Math.ceil((double)total*90/100); Double idx95= Math.ceil((double)total*95/100); Double idx99= Math.ceil((double)total*99/100); Map.Entry
idx90Entry = transMap.floorEntry(idx90.longValue()); Map.Entry
idx95Entry = transMap.floorEntry(idx95.longValue()); Map.Entry
idx99Entry = transMap.floorEntry(idx99.longValue()); Long TP90=idx90Entry.getValue(); Long TP95=idx95Entry.getValue(); Long TP99=idx99Entry.getValue(); System.out.println("TP90:"+TP90+", TP95:"+TP95+", TP99:"+TP99); }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/227396.html原文链接:https://javaforall.net
