Esempio n. 1
0
        public static RequestStat aggregate(List <RequestInfo> requestInfos, long durationInMillis)
        {
            double maxRespTime  = Double.MinValue;
            double minRespTime  = Double.MaxValue;
            double avgRespTime  = -1;
            double p999RespTime = -1;
            double p99RespTime  = -1;
            double sumRespTime  = 0;
            long   count        = 0;

            foreach (RequestInfo requestInfo in requestInfos)
            {
                ++count;
                double respTime = requestInfo.responseTime;
                if (maxRespTime < respTime)
                {
                    maxRespTime = respTime;
                }
                if (minRespTime > respTime)
                {
                    minRespTime = respTime;
                }
                sumRespTime += respTime;
            }
            if (count != 0)
            {
                avgRespTime = sumRespTime / count;
            }
            long tps = (long)(count / durationInMillis * 1000);

            // 在Lambda表达式中使用Comparison<T>进行排序
            requestInfos.Sort((x, y) => x.responseTime.CompareTo(y.responseTime));
            // 等价于
            //requestInfos.Sort(delegate (RequestInfo o1, RequestInfo o2)
            //{
            //    return o1.responseTime.CompareTo(o2.responseTime);
            //});

            int idx999 = (int)(count * 0.999);
            int idx99  = (int)(count * 0.99);

            if (count != 0)
            {
                //  请求时间List 按照请求时间排序后的(总请求次数*0.999)的index 对应的 List(index) 的时间值
                p999RespTime = requestInfos[idx999].responseTime;
                p99RespTime  = requestInfos[idx99].responseTime;
            }
            RequestStat requestStat = new RequestStat
            {
                maxResponseTime  = maxRespTime,
                minResponseTime  = minRespTime,
                avgResponseTime  = avgRespTime,
                p999ResponseTime = p999RespTime,
                p99ResponseTime  = p99RespTime,
                count            = count,
                tps = tps
            };

            return(requestStat);
        }
Esempio n. 2
0
        public void TimerCallback(object o)
        {
            // 第1个代码逻辑:根据给定的时间区间,从数据库中拉取数据;
            long durationInMillis  = 1 * 1000;
            long endTimeInMillis   = DateTime.Now.Millisecond;
            long startTimeInMillis = endTimeInMillis - durationInMillis;
            Dictionary <string, List <RequestInfo> > requestInfos =
                metricsStorage.getRequestInfos(startTimeInMillis, endTimeInMillis);
            Dictionary <string, RequestStat> stats = new Dictionary <string, RequestStat>();

            foreach (var entry in requestInfos)
            {
                string             apiName            = entry.Key;
                List <RequestInfo> requestInfosPerApi = entry.Value;
                // 第2个代码逻辑:根据原始数据,计算得到统计数据;
                RequestStat requestStat = Aggregator.aggregate(requestInfosPerApi, durationInMillis);
                stats.Add(apiName, requestStat);
            }
            // 第3个代码逻辑:将统计数据显示到终端(命令行或邮件);
            Console.WriteLine("Time Span: [" + startTimeInMillis + ", " + endTimeInMillis + "]");
        }