为什么用时平均图表高低不平? [英] Why is the graph spiky when using average?

查看:202
本文介绍了为什么用时平均图表高低不平?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用im messageInspectors在我的WCF服务来衡量这样每个服务方法经过时间:

Im using messageInspectors in my WCF service to measure elapsed time on each service method like this :

public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
    if (_activated)
    {
        _startTime.Stop();
        PerformanceCounterHandler.Instance.SetAveragePerformanceCounter(operationName, _startTime.ElapsedTicks);
    }
}

public object BeforeCall(string operationName, object[] inputs)
{
    Guid correlationState;

    if (_activated)
    {
        correlationState = Guid.NewGuid();
        _startTime = new Stopwatch();
        _startTime.Start();
        return correlationState;
    }
    return null;
}

这是计数器是如何registred

This is how the counters is registred

foreach (string methodName in ServiceMethodNames)
                {
                    counter = new CounterCreationData(methodName, methodName + " > Genomsnittlig tid(ms)", PerformanceCounterType.AverageTimer32);
                    col.Add(counter);
                    counter = new CounterCreationData(methodName + "_base", methodName + " > Genomsnittlig tid(ms)", PerformanceCounterType.AverageBase);
                    col.Add(counter);
                }



设置性能计数器的方法是这样的:

The method for setting the performance counter looks like this :

public Boolean SetAveragePerformanceCounter(string performanceCounterName, long value)
        {
            PerformanceCounter performCounter;

            if (_useOrbitPerformanceCounters && (performCounter = _performanceCounters.Values.Where(c=> c.CounterName == performanceCounterName).FirstOrDefault()) != null)
            {
                performCounter.IncrementBy(value);
                performanceCounterName = performanceCounterName + "_base";
                performCounter = _performanceCounters[performanceCounterName];
                performCounter.Increment();
                return true;
            }
            return false;
        }



性能计数器但是显示出来,而不是尖峰平均?如果我改变了看法报告一切都是0,只要我什么都不做?我需要能够看到calltime平均的模样,现在即使有暂时的呼叫。我在做什么错了?

The performance counter does however show spikes instead of average? If I change the view to report everything is 0 as long as I do nothing? I need to be able to see how the calltime average looks like right now even if there is no calls for the moment. What am I doing wrong?

推荐答案

的问题是,是, AverageTimer32 不显示一些alltime平均值。从文档

The problem is, that the AverageTimer32 doesn't show some alltime average value. From the documentation:

这是衡量它需要的平均时间平均计数器,完成一个过程或操作。这种类型的计数器显示的采样间隔对操作过程中的时间内完成处理或操作数的总经过时间的比率。此计数器类型测量系统时钟滴答的时间。
。结构式:((N1 - N0)/ F)/(B 1 - B 0),其中N1和N0是性能计数器读数,B1和B0的是其相应AverageBase值,以及F是每秒蜱的数目。

An average counter that measures the time it takes, on average, to complete a process or operation. Counters of this type display a ratio of the total elapsed time of the sample interval to the number of processes or operations completed during that time. This counter type measures time in ticks of the system clock. Formula: ((N1 - N0)/F)/(B1 - B0), where N1 and N0 are performance counter readings, B1 and B0 are their corresponding AverageBase values, and F is the number of ticks per second.

有趣的部分是公式。性能计数器只显示了两个性能计数器读数之间的平均值。所以,如果没有在那里提出的请求,得到的值是0,因为分子为0。

The interesting part is the formula. The performance counter just shows the average between two performance counter readings. So if no requests where made, the resulting value is 0 because the numerator is 0.

也许是某种方式可以计算出你自己的价值,并通过其他一些揭露它。类型的性能计数器

Maybe it's somehow possible to calculate the value on your own and expose it through some other type of performance counter.

编辑:
我写了一个演示应用程序演示一种解决方法,但感觉相当混乱。我创建了一个 NumberOfItems32 性能计数器。

var counterDataCollection = new CounterCreationDataCollection();

var averageRandomNumer = new CounterCreationData
{
    CounterType = PerformanceCounterType.NumberOfItems32,
    CounterName = averageRandomNumberCounterName,
    CounterHelp = "Views the average random number."
};
counterDataCollection.Add(averageRandomNumer);

PerformanceCounterCategory.Create(
    categoryName,
    "Displays the various performance counters of a test application",
    PerformanceCounterCategoryType.MultiInstance,
    counterDataCollection);

和设置的值,像这样:

var averageRandomNumberPerfCounter = new PerformanceCounter(categoryName, averageRandomNumberCounterName, "firstInstance", false);
var random = new Random();
var currentAverage = 0d;
var numberOfReadings = 0L;

while (true)
{
    var nextRandom = random.Next(1, 101);

    // ATTENTION: real code should handle overflow properly
    numberOfReadings++;
    currentAverage = (((numberOfReadings - 1) * currentAverage) + nextRandom) / numberOfReadings;

    averageRandomNumberPerfCounter.RawValue = (long)currentAverage;

    Thread.Sleep(1000);
}



该解决方案的缺点是显而易见的。由于性能计数器只能存储你失去平均值的小数位。此问题的另一个解决办法是扩展您的值,例如他们与10相乘,然后选择性能监视器一个较低的比例。

The disadvantage of this solution is obvious. Since the performance counter can only store long you lose the decimal places of your average value. Another workaround for this problem would be to scale your values, for example multiplying them with 10 and then choose an lower scaling in performance monitor.

这篇关于为什么用时平均图表高低不平?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆