Metrics
Metrics Classes
There are two metrics classes currently, MetricDecimal and MetricInteger. They work identically and only store their respective types internally.
The Metrics classes employs a bucketed approach to metric calculations, meaning it only stores unique metric values along with their frequency counts rather than keeping every individual metric entry. This method significantly reduces the amount of memory and storage needed, as repeated values are aggregated into a single entry with an associated count. Consequently, even with large streams of data, the class can track and compute statistics like averages, percentiles, and medians efficiently without the overhead of managing vast amounts of duplicate data.
MetricDecimal
Metrics are set of classes that implements a metric collection for values. It provides functionality to add metric values and compute various statistics such as average, median, percentiles, minimum, maximum, and sum. In addition, it supports serialization and deserialization of the metric data to and from JSON formats.
Properties
MetricName
Represents the name of the metric. This property is used to identify the metric collection.
MetricType
Indicates the type of the metric. For this class, it is set to "Decimal".
Methods
AddMetric(decimal metric, int precision)
Adds a new metric value to the collection after rounding it to the specified number of decimal places. In addition to updating the internal bucket of values, this method updates the maximum, minimum, and sum accordingly.
Parameters:
metric: The decimal value to add.
precision: The number of decimal places used for rounding the metric value.
AddMetric(decimal metric)
Adds a new metric value to the collection after rounding it to 2 decimal places. This method updates the internal bucket as well as the maximum, minimum, and cumulative sum values.
Parameters:
metric: The decimal value to add.
Average()
Calculates and returns the average of the metric values. The average is computed by dividing the cumulative sum of metric values by the total count of entries in the collection.
Max()
Returns the maximum metric value that has been added to the collection.
Median()
Computes and returns the median of the collected metric values. This method identifies the middle value by using the frequencies from the bucket of values. In cases where the cumulative frequency exactly equals the midpoint, the previous value is used.
Min()
Returns the minimum metric value recorded in the collection.
Percentile(decimal k, bool Interpolate = false)
Calculates the kth percentile of the recorded metric values. If k is 50, the method delegates to the Median() calculation. For other percentiles, it determines the appropriate index within the ordered set of unique metric values. If the computed index is a whole number, it returns the average of the two adjacent values; otherwise, it may compute an interpolated result based on the Interpolate parameter or return the ceiling value.
Parameters:
k: The percentile value to compute (e.g., 25, 50, 75).
Interpolate (optional): A boolean flag that, if true, forces interpolation between adjacent values. The default value is false.
PercentileRank(decimal k)
Calculates the percentile rank of a specific metric value k. The percentile rank is computed with the formula:
(cf_l + 0.5 f_i) / N * 100%
where cf_l is the cumulative frequency of values less than k, f_i is the frequency of the value k, and N is the total count of all entries.
Parameters:
k: The metric value for which the percentile rank is to be determined.
Sum()
Returns the cumulative sum of all metric values added to the collection.
FromJson(string json)
Reconstructs the MetricDecimal instance from a JSON string representation of the metric data. This method updates the sum, minimum, maximum, and bucket of frequencies based on the JSON content.
Parameters:
json: A JSON formatted string representing the metric data.
AsJson(bool valuesOnly = true)
Serializes the current state of metric data into a JSON string. Basic statistical information such as sum, min, max, average, median, and count are always included. If the valuesOnly parameter is false, then the full bucket containing all metric frequencies is also included.
Parameters:
valuesOnly (optional): A boolean flag indicating whether only basic stats should be serialized (true by default).
Returns:
A JSON formatted string representing the metric data.
AsJObject(bool valuesOnly = true)
Serializes the metric data into a JObject. Similar to AsJson, it always includes the basic statistical information. When the valuesOnly parameter is false, the complete bucket data is also inserted into the JObject.
Parameters:
valuesOnly (optional): A boolean flag indicating whether only basic stats should be included in the JObject (true by default).
Returns:
A JObject containing the metric data.
Example:
Last updated