项目作者: colinc86

项目描述 :
Examine numerical values that change over time in Go.
高级语言: Go
项目地址: git://github.com/colinc86/probes.git
创建时间: 2019-08-17T22:41:01Z
项目社区:https://github.com/colinc86/probes

开源协议:MIT License

下载


probes

GoDoc Build Status

The probes package helps examine numerical values that change over time by providing methods to collect values safely across goroutines through the Probe structure.

Usage

Activation/Deactivation

Activate a probe by calling its Activate method, and deactivate it by calling Deactivate. While doing work, send values to the probe’s input channel.

  1. p := NewProbe()
  2. p.Activate()
  3. // Some work...
  4. for var i := 0; i < 10; i++ {
  5. p.C <- float64(i)
  6. }
  7. p.Flush()
  8. p.Deactivate()

Examining the Input Signal

Examine the probe’s input signal at any time by using the Signal and RecentValue methods.

  1. // "9"
  2. fmt.Println(p.RecentValue())
  3. // "[0 1 2 3 4 5 6 7 8 9]"
  4. fmt.Println(p.Signal())

Reuse

Calling Activate after a call to Deactivate will continue appending values to the probes previous input signal. To prevent this behavior, call the probe’s ClearSignal method before a call to Activate.

  1. // ... some work
  2. p.Deactivate()
  3. // Log our input signal
  4. fmt.Println(p.Signal())
  5. // Clear the input and re-activate
  6. p.ClearSignal()
  7. p.Activate()

Buffers and State

Set the probe’s input channel buffer length by setting the InputBufferLength property. This value is set to 1 by default and will only take effect after the next call to Activate.

Set the probe’s maximum signal length by setting the MaximumSignalLength property. This value is set to math.MaxInt32 by default and should only be set while the probe is in an inactive state.

Check the state of the probe at any time with its IsActive method.

  1. p.InputBufferLength = 10
  2. if !p.IsActive() {
  3. p.MaximumSignalLength = 100
  4. }

Blocking and Non-Blocking

Probe provides two ways to pass input to the probe:

  • The input channel, C. (non-blocking)
  • The Push method. (blocking)

Use whichever is more appropriate, but be aware that calling Deactivate on a probe does not automatically flush the probes input channel buffer. You must call Flush before calling Deactivate to ensure that all values will be represented in the probe’s signal.

Mixing input methods by using both the channel and push method is supported by the push method’s flush parameter. Set this to true to preserve input order in the probe’s signal.

  1. p := NewProbe()
  2. p.Activate()
  3. // Some work...
  4. for var i := 0; i < 10; i++ {
  5. p.C <- float64(i)
  6. }
  7. p.Push(10, true)
  8. p.Deactivate()