C# Class Elders.Hystrix.NET.Util.HystrixRollingNumber

A number which can be used to track counters (increment) or set values over time.

It is "rolling" in the sense that a 'timeInMilliseconds' is given that you want to track (such as 10 seconds) and then that is broken into buckets (defaults to 10) so that the 10 second window doesn't empty out and restart every 10 seconds, but instead every 1 second you have a new bucket added and one dropped so that 9 of the buckets remain and only the newest starts from scratch.

This is done so that the statistics are gathered over a rolling 10 second window with data being added/dropped in 1 second intervals (or whatever granularity is defined by the arguments) rather than each 10 second window starting at 0 again.

Performance-wise this class is optimized for writes, not reads. This is done because it expects far higher write volume (thousands/second) than reads (a few per second).

For example, on each read to getSum/getCount it will iterate buckets to sum the data so that on writes we don't need to maintain the overall sum and pay the synchronization cost at each write to ensure the sum is up-to-date when the read can easily iterate each bucket to get the sum when it needs it.

Mostrar archivo Open project: Elders/Hystrix.NET Class Usage Examples

Private Properties

Property Type Description
GetCurrentBucket Bucket
HystrixRollingNumber System
HystrixRollingNumber System

Public Methods

Method Description
Add ( HystrixRollingNumberEvent type, long value ) : void

Add to the counter in the current bucket for the given HystrixRollingNumberEvent type.

GetCumulativeSum ( HystrixRollingNumberEvent type ) : long

Get the cumulative sum of all buckets ever since the application started without rolling for the given HystrixRollingNumberEvent type. See GetRollingSum(HystrixRollingNumberEvent) for the rolling sum.

GetRollingMaxValue ( HystrixRollingNumberEvent type ) : long

Get the max value of values in all buckets for the given HystrixRollingNumberEvent type. The HystrixRollingNumberEvent must be a "MaxUpdater" type (HystrixRollingNumberEvent.IsMaxUpdater() == true).

GetRollingSum ( HystrixRollingNumberEvent type ) : long

Get the sum of all buckets in the rolling counter for the given HystrixRollingNumberEvent type. The HystrixRollingNumberEvent must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true).

GetValueOfLatestBucket ( HystrixRollingNumberEvent type ) : long

Get the value of the latest (current) bucket in the rolling counter for the given HystrixRollingNumberEvent type. The HystrixRollingNumberEvent must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true).

GetValues ( HystrixRollingNumberEvent type ) : long[]

Get an array of values for all buckets in the rolling counter for the given HystrixRollingNumberEvent type. Index 0 is the oldest bucket. The HystrixRollingNumberEvent must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true).

HystrixRollingNumber ( IHystrixProperty timeInMilliseconds, IHystrixProperty numberOfBuckets ) : System

Initializes a new instance of the HystrixRollingNumber class.

Increment ( HystrixRollingNumberEvent type ) : void

Increment the counter in the current bucket by one for the given HystrixRollingNumberEvent type.

Reset ( ) : void

Force a reset of all rolling counters (clear all buckets) so that statistics start being gathered from scratch. This does NOT reset the CumulativeSum values.

UpdateRollingMax ( HystrixRollingNumberEvent type, long value ) : void

Update a value and retain the max value.

Private Methods

Method Description
GetCurrentBucket ( ) : Bucket

Gets the current bucket. If the time is after the window of the current bucket, a new one will be created. Internal because it's used in unit tests.

HystrixRollingNumber ( ITime time, IHystrixProperty timeInMilliseconds, IHystrixProperty numberOfBuckets ) : System

Initializes a new instance of the HystrixRollingNumber class.

HystrixRollingNumber ( ITime time, int timeInMilliseconds, int numberOfBuckets ) : System

Initializes a new instance of the HystrixRollingNumber class. This constructor is used for unit testing to be able to inject mocked time measurement.

Method Details

Add() public method

Add to the counter in the current bucket for the given HystrixRollingNumberEvent type.
public Add ( HystrixRollingNumberEvent type, long value ) : void
type HystrixRollingNumberEvent Defining which counter to add to, must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true).
value long Value to be added to the current bucket.
return void

GetCumulativeSum() public method

Get the cumulative sum of all buckets ever since the application started without rolling for the given HystrixRollingNumberEvent type. See GetRollingSum(HystrixRollingNumberEvent) for the rolling sum.
public GetCumulativeSum ( HystrixRollingNumberEvent type ) : long
type HystrixRollingNumberEvent Must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true).
return long

GetRollingMaxValue() public method

Get the max value of values in all buckets for the given HystrixRollingNumberEvent type. The HystrixRollingNumberEvent must be a "MaxUpdater" type (HystrixRollingNumberEvent.IsMaxUpdater() == true).
public GetRollingMaxValue ( HystrixRollingNumberEvent type ) : long
type HystrixRollingNumberEvent HystrixRollingNumberEvent defining which "MaxUpdater" to retrieve values from
return long

GetRollingSum() public method

Get the sum of all buckets in the rolling counter for the given HystrixRollingNumberEvent type. The HystrixRollingNumberEvent must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true).
public GetRollingSum ( HystrixRollingNumberEvent type ) : long
type HystrixRollingNumberEvent defining which counter to retrieve values from
return long

GetValueOfLatestBucket() public method

Get the value of the latest (current) bucket in the rolling counter for the given HystrixRollingNumberEvent type. The HystrixRollingNumberEvent must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true).
public GetValueOfLatestBucket ( HystrixRollingNumberEvent type ) : long
type HystrixRollingNumberEvent HystrixRollingNumberEvent defining which counter to retrieve value from
return long

GetValues() public method

Get an array of values for all buckets in the rolling counter for the given HystrixRollingNumberEvent type. Index 0 is the oldest bucket. The HystrixRollingNumberEvent must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true).
public GetValues ( HystrixRollingNumberEvent type ) : long[]
type HystrixRollingNumberEvent HystrixRollingNumberEvent defining which counter to retrieve values from
return long[]

HystrixRollingNumber() public method

Initializes a new instance of the HystrixRollingNumber class.
public HystrixRollingNumber ( IHystrixProperty timeInMilliseconds, IHystrixProperty numberOfBuckets ) : System
timeInMilliseconds IHystrixProperty The total time window to track.
numberOfBuckets IHystrixProperty The number of parts to break the time window.
return System

Increment() public method

Increment the counter in the current bucket by one for the given HystrixRollingNumberEvent type.
public Increment ( HystrixRollingNumberEvent type ) : void
type HystrixRollingNumberEvent Defining which counter to increment, must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true).
return void

Reset() public method

Force a reset of all rolling counters (clear all buckets) so that statistics start being gathered from scratch. This does NOT reset the CumulativeSum values.
public Reset ( ) : void
return void

UpdateRollingMax() public method

Update a value and retain the max value.
public UpdateRollingMax ( HystrixRollingNumberEvent type, long value ) : void
type HystrixRollingNumberEvent Defining which counter to update, must be a "MaxUpdater" type (HystrixRollingNumberEvent.IsMaxUpdater() == true).
value long Value to be updated to the current bucket
return void