日期:2014-05-17  浏览次数:20939 次

性能/压力测试工具Apache ab修改系列:Part4——修改原版Apache AB中的bug,由于变量越界导致的Segmentation fault

需求:

在使用原版Apache AB进行压力/性能测试的过程中,当完成请求次数超过2千2百万的时候,Apache AB在统计“Sorted on total connect times”的时候,会出现由于数组越界而引起的Segmentation fault的问题。

?

解决方案:

1. 引发血案的原因:

首先,在32位机器上,int数据类型的最大值为2147483647。而Apache AB源码在统计“Sorted on total connect times”的时候,采用的代码如下所示:

?

?

/* Sorted on total connect times */
        if (percentile && (done > 1)) {
            printf("\nPercentage of the requests served within a certain time (ms)\n");
            for (i = 0; i < sizeof(percs) / sizeof(int); i++) {
                if (percs[i] <= 0)
                    printf(" 0%%  <0> (never)\n");
                else if (percs[i] >= 100)
                    printf(" 100%%  %5" APR_TIME_T_FMT " (longest request)\n",
                           ap_round_ms(stats[done - 1].time));
                else
                    printf("  %d%%  %5" APR_TIME_T_FMT "\n", percs[i],
                           ap_round_ms(stats[(int) (done * percs[i] / 100)].time));
            }
        }

?

?由于源码中 percs数组的值为:

?

int percs[] = {50, 66, 75, 80, 90, 95, 98, 99, 100};

?因此, 当请求次数 done > 2千2百万,并乘与percs[7]=99的时候,已经超出int类型的最大值,因此导致stats数组越界,报出Segmentation fault的错误

?

2. 解决思路:

解决这个问题,其实很简单,只需要将源码:

?

 ap_round_ms(stats[(int) (done * percs[i] / 100)].time)

?

替换为:

?

ap_round_ms(stats[(int) ((double)(done / 100) * percs[i])].time)

?

即可解决问题。

?

ok, 性能/压力测试工具Apache ab修改系列:part4 介绍完毕, 欢迎拍砖。转发请备注转自:100continue.iteye.com。 谢谢。