Exemplo n.º 1
0
        /// <summary>
        /// 获取相应计数器
        /// </summary>
        /// <param name="traceInfo">跟踪信息</param>
        /// <returns></returns>
        public RequestCounter GetCounter(UrlTraceInfo traceInfo)
        {
            if (traceInfo == null)
                throw new ArgumentNullException("traceInfo");

            Range requestCostRange = Range.Empty;
            Range responseSizeRange = Range.Empty;

            if (traceInfo.TraceItems.RequestCost)
                requestCostRange = RequestTagProvider.RequestCostRanges.First(r => traceInfo.RequestCost >= r.Lower && traceInfo.RequestCost < r.Upper);

            if (traceInfo.TraceItems.ResponseSize)
                responseSizeRange = RequestTagProvider.ResponseSizeRanges.First(r => traceInfo.ResponseSize >= r.Lower && traceInfo.ResponseSize < r.Upper);

            string key = string.Concat(requestCostRange.Id, "|", responseSizeRange.Id);
            RequestCounter counter;

            countersLock.EnterReadLock();
            counters.TryGetValue(key, out counter);
            countersLock.ExitReadLock();

            if (counter == null)
            {
                countersLock.EnterWriteLock();
                counters.TryGetValue(key, out counter);
                if (counter == null)
                {
                    try
                    {
                        RequestTagInfo tagInfo = new RequestTagInfo();
                        tagInfo.RequestCostRange = requestCostRange;
                        tagInfo.ResponseSizeRange = responseSizeRange;

                        tagInfo.FullTags = new Dictionary<string, string>();
                        tagInfo.FullTags.Add("HostAddress", hostAddress);
                        tagInfo.FullTags.Add("FilePath", filePath);

                        if (tagInfo.RequestCostRange.Id != 0) tagInfo.FullTags.Add("Latency", tagInfo.RequestCostRange.Name);
                        if (tagInfo.ResponseSizeRange.Id != 0) tagInfo.FullTags.Add("ResponseSize", tagInfo.ResponseSizeRange.Name);

                        counter = new RequestCounter(tagInfo, this);
                        counters.Add(key, counter);
                    }
                    catch
                    { }
                    finally
                    {
                        countersLock.ExitWriteLock();
                    }
                }
                else
                {
                    countersLock.ExitWriteLock();
                }
            }

            return counter;
        }
Exemplo n.º 2
0
        /// <summary>
        /// 取得所有计数器
        /// </summary>
        /// <returns></returns>
        public RequestCounter[] GetAllCounters()
        {
            countersLock.EnterReadLock();
            var target = new RequestCounter[counters.Count];

            try
            {
                counters.Values.CopyTo(target, 0);
            }
            catch
            { }
            finally
            {
                countersLock.ExitReadLock();
            }

            return target;
        }