leafy 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ require 'leafy/core/histogram'
2
+
3
+ RSpec.describe Leafy::Core::Histogram do
4
+
5
+ let(:reservoir) do
6
+ reservoir = double('Reservoir')
7
+ allow(reservoir).to receive(:update).with(100)
8
+ reservoir
9
+ end
10
+
11
+ let(:histogram) { Leafy::Core::Histogram.new(reservoir) }
12
+
13
+ it 'updatesTheCountOnUpdates' do
14
+ expect(histogram.count).to eq 0
15
+
16
+ histogram.update(100);
17
+
18
+ expect(histogram.count).to eq 1
19
+ end
20
+ end
@@ -0,0 +1,40 @@
1
+ require 'leafy/core/meter'
2
+
3
+ RSpec.describe Leafy::Core::Meter do
4
+
5
+ let(:clock) do
6
+ clock = Leafy::Core::Clock.new
7
+ def clock.tick
8
+ @ticks ||= [0, 0] + [10000000000] * 10
9
+ @ticks.shift
10
+ end
11
+ clock
12
+ end
13
+
14
+ let(:meter) { Leafy::Core::Meter.new(clock) }
15
+
16
+ it 'starts out with no rates or count' do
17
+ expect(meter.count).to eq 0
18
+
19
+ expect(meter.mean_rate).to eq 0
20
+
21
+ expect(meter.one_minute_rate).to eq 0
22
+
23
+ expect(meter.five_minute_rate).to eq 0
24
+
25
+ expect(meter.fifteen_minute_rate).to eq 0
26
+ end
27
+
28
+ it 'marksEventsAndUpdatesRatesAndCount' do
29
+ meter.mark
30
+ meter.mark(2)
31
+
32
+ expect(meter.mean_rate).to eq 0.3
33
+
34
+ expect(Number[meter.one_minute_rate]).to eql Number[0.1840, 0.1]
35
+
36
+ expect(Number[meter.five_minute_rate]).to eql Number[0.1966, 0.1]
37
+
38
+ expect(Number[meter.fifteen_minute_rate]).to eql Number[0.1988, 0.1]
39
+ end
40
+ end
@@ -0,0 +1,168 @@
1
+ require 'leafy/core/metric_registry'
2
+
3
+ RSpec.describe Leafy::Core::MetricRegistry do
4
+
5
+ class MetricMock
6
+
7
+ def initialize(klass)
8
+ @klass = klass
9
+ end
10
+ def is_a?(klass)
11
+ @klass == klass
12
+ end
13
+ end
14
+
15
+ let(:registry) { subject }
16
+
17
+ let(:gauge) { MetricMock.new(Leafy::Core::Gauge) }
18
+ let(:counter) { MetricMock.new(Leafy::Core::Counter) }
19
+ let(:timer) { MetricMock.new(Leafy::Core::Timer) }
20
+ let(:histogram) { MetricMock.new(Leafy::Core::Histogram) }
21
+ let(:meter) { MetricMock.new(Leafy::Core::Meter) }
22
+
23
+ it 'registeringAGaugeTriggersANotification' do
24
+ expect(registry.register("thing", gauge)).to eq gauge
25
+
26
+ # verify(listener).onGaugeAdded("thing", gauge);
27
+ end
28
+
29
+ it 'removingAGaugeTriggersANotification' do
30
+ registry.register("thing", gauge);
31
+
32
+ expect(registry.remove("thing")).to eq true
33
+
34
+ # verify(listener).onGaugeRemoved("thing");
35
+ end
36
+
37
+ it 'registeringACounterTriggersANotification' do
38
+ expect(registry.register("thing", counter)).to eq counter
39
+
40
+ # verify(listener).onCounterAdded("thing", counter);
41
+ end
42
+
43
+ it 'accessingACounterRegistersAndReusesTheCounter' do
44
+ counter1 = registry.counter("thing");
45
+ counter2 = registry.counter("thing");
46
+
47
+ expect(counter1).to equal counter2
48
+
49
+ # verify(listener).onCounterAdded("thing", counter1);
50
+ end
51
+
52
+ it 'removingACounterTriggersANotification' do
53
+ registry.register("thing", counter);
54
+
55
+ expect(registry.remove("thing")).to eq true
56
+
57
+ # verify(listener).onCounterRemoved("thing");
58
+ end
59
+
60
+ it 'registeringAHistogramTriggersANotification' do
61
+ expect(registry.register("thing", histogram)).to eq histogram
62
+
63
+ # verify(listener).onHistogramAdded("thing", histogram);
64
+ end
65
+
66
+ it 'accessingAHistogramRegistersAndReusesIt' do
67
+ histogram1 = registry.histogram("thing");
68
+ histogram2 = registry.histogram("thing");
69
+
70
+ expect(histogram1).to be histogram2
71
+
72
+ # verify(listener).onHistogramAdded("thing", histogram1);
73
+ end
74
+
75
+ it 'removingAHistogramTriggersANotification' do
76
+ registry.register("thing", histogram);
77
+
78
+ expect(registry.remove("thing")).to eq true
79
+
80
+ # verify(listener).onHistogramRemoved("thing");
81
+ end
82
+
83
+ it 'registeringAMeterTriggersANotification' do
84
+ expect(registry.register("thing", meter)).to eq meter
85
+
86
+ # verify(listener).onMeterAdded("thing", meter);
87
+ end
88
+
89
+ it 'accessingAMeterRegistersAndReusesIt' do
90
+ meter1 = registry.meter("thing");
91
+ meter2 = registry.meter("thing");
92
+
93
+ expect(meter1).to be meter2
94
+
95
+ # verify(listener).onMeterAdded("thing", meter1);
96
+ end
97
+
98
+ it 'removingAMeterTriggersANotification' do
99
+ registry.register("thing", meter);
100
+
101
+ expect(registry.remove("thing")).to eq true
102
+
103
+ # verify(listener).onMeterRemoved("thing");
104
+ end
105
+
106
+ it 'registeringATimerTriggersANotification' do
107
+ expect(registry.register("thing", timer)).to eq timer
108
+
109
+ # verify(listener).onTimerAdded("thing", timer);
110
+ end
111
+
112
+ it 'accessingATimerRegistersAndReusesIt' do
113
+ timer1 = registry.timer("thing");
114
+ timer2 = registry.timer("thing");
115
+
116
+ expect(timer1).to be timer2
117
+
118
+ # verify(listener).onTimerAdded("thing", timer1);
119
+ end
120
+
121
+ it 'removingATimerTriggersANotification' do
122
+ registry.register("thing", timer);
123
+
124
+ expect(registry.remove("thing")).to eq true
125
+
126
+ # verify(listener).onTimerRemoved("thing");
127
+ end
128
+
129
+ it 'hasAMapOfRegisteredGauges' do
130
+ registry.register("gauge", gauge)
131
+
132
+ expect(registry.gauges['gauge']).to eq gauge
133
+ end
134
+
135
+ it 'hasAMapOfRegisteredCounters' do
136
+ registry.register("counter", counter);
137
+
138
+ expect(registry.counters['counter']).to eq counter
139
+ end
140
+
141
+ it 'hasAMapOfRegisteredHistograms' do
142
+ registry.register("histogram", histogram);
143
+
144
+ expect(registry.histograms['histogram']).to eq histogram
145
+ end
146
+
147
+ it 'hasAMapOfRegisteredMeters' do
148
+ registry.register("meter", meter);
149
+
150
+ expect(registry.meters['meter']).to eq meter
151
+ end
152
+
153
+ it 'hasAMapOfRegisteredTimers' do
154
+ registry.register("timer", timer);
155
+
156
+ expect(registry.timers['timer']).to eq timer
157
+ end
158
+
159
+ it 'hasASetOfRegisteredMetricNames' do
160
+ registry.register("gauge", gauge);
161
+ registry.register("counter", counter);
162
+ registry.register("histogram", histogram);
163
+ registry.register("meter", meter);
164
+ registry.register("timer", timer);
165
+
166
+ expect(registry.names).to match_array ["gauge", "counter", "histogram", "meter", "timer"]
167
+ end
168
+ end
@@ -0,0 +1,47 @@
1
+ require 'leafy/core/ratio_gauge'
2
+
3
+ RSpec.describe Leafy::Core::RatioGauge do
4
+
5
+
6
+ it 'ratiosAreHumanReadable' do
7
+ ratio = Leafy::Core::RatioGauge::Ratio.of(100, 200);
8
+
9
+ expect(ratio.to_s).to eq "100.0:200.0"
10
+ end
11
+
12
+
13
+ it 'calculatesTheRatioOfTheNumeratorToTheDenominator' do
14
+ regular = Leafy::Core::RatioGauge.new do
15
+ Leafy::Core::RatioGauge::Ratio.of(2.0, 4.0)
16
+ end
17
+
18
+ expect(regular.value).to eq 0.5
19
+ end
20
+
21
+
22
+ it 'handlesDivideByZeroIssues' do
23
+ divByZero = Leafy::Core::RatioGauge.new do
24
+ Leafy::Core::RatioGauge::Ratio.of(100, 0)
25
+ end
26
+
27
+ expect(divByZero.value.nan?).to eq true
28
+ end
29
+
30
+
31
+ it 'handlesInfiniteDenominators' do
32
+ infinite = Leafy::Core::RatioGauge.new do
33
+ Leafy::Core::RatioGauge::Ratio.of(10, Float::INFINITY)
34
+ end
35
+
36
+ expect(infinite.value.nan?).to eq true
37
+ end
38
+
39
+
40
+ it 'handlesNaNDenominators' do
41
+ nan = Leafy::Core::RatioGauge.new do
42
+ Leafy::Core::RatioGauge::Ratio.of(10, Float::NAN)
43
+ end
44
+
45
+ expect(nan.value.nan?).to eq true
46
+ end
47
+ end
@@ -0,0 +1,135 @@
1
+ require 'leafy/core/scheduled_reporter'
2
+ require 'concurrent/atomic/atomic_fixnum'
3
+
4
+
5
+ RSpec.describe Leafy::Core::ScheduledReporter do
6
+
7
+ class MetricMock
8
+
9
+ def initialize(klass)
10
+ @klass = klass
11
+ end
12
+ def is_a?(klass)
13
+ @klass == klass
14
+ end
15
+ end
16
+
17
+ class Leafy::Core::DummyReporter < Leafy::Core::ScheduledReporter
18
+
19
+ attr_reader :execution_count, :args
20
+ def initialize(registry, name, *)#executor = nil, shutdownExecutorOnStop = false)
21
+ super
22
+ @execution_count = Concurrent::AtomicFixnum.new
23
+ @args = []
24
+ end
25
+
26
+ def do_report(*args)
27
+ @args << args
28
+ @execution_count.increment
29
+ end
30
+ end
31
+
32
+ let(:gauge) { MetricMock.new(Leafy::Core::Gauge) }
33
+ let(:counter) { MetricMock.new(Leafy::Core::Counter) }
34
+ let(:timer) { MetricMock.new(Leafy::Core::Timer) }
35
+ let(:histogram) { MetricMock.new(Leafy::Core::Histogram) }
36
+ let(:meter) { MetricMock.new(Leafy::Core::Meter) }
37
+ let(:registry) do
38
+ registry = Leafy::Core::MetricRegistry.new
39
+ registry.register("gauge", gauge)
40
+ registry.register("counter", counter)
41
+ registry.register("histogram", histogram)
42
+ registry.register("meter", meter)
43
+ registry.register("timer", timer)
44
+ registry
45
+ end
46
+
47
+ let(:custom_executor) { Concurrent::SingleThreadExecutor.new }
48
+ let(:external_executor) { Concurrent::SingleThreadExecutor.new }
49
+ let(:reporter) { Leafy::Core::DummyReporter.new(registry, 'example') }
50
+ let(:reporter_with_custom_executor) { Leafy::Core::DummyReporter.new(registry, 'example', custom_executor) }
51
+ let(:reporter_with_externally_managed_executor) { Leafy::Core::DummyReporter.new(registry, 'example', external_executor, false) }
52
+ let(:reporters) { [reporter, reporter_with_custom_executor,
53
+ reporter_with_externally_managed_executor] }
54
+
55
+ after :each do
56
+ reporter.stop
57
+ custom_executor.shutdown
58
+ external_executor.shutdown
59
+ end
60
+
61
+ it 'pollsPeriodically' do
62
+ reporter.start(0.1)
63
+
64
+ sleep(0.55)
65
+ expect(reporter.execution_count.value).to eq 5
66
+ expect(reporter.args.size).to eq 5
67
+ reporter.args.each do |arg|
68
+ expect(arg[0]['gauge']).to eq gauge
69
+ expect(arg[1]['counter']).to eq counter
70
+ expect(arg[2]['histogram']).to eq histogram
71
+ expect(arg[3]['meter']).to eq meter
72
+ expect(arg[4]['timer']).to eq timer
73
+ end
74
+ end
75
+
76
+ it 'shouldDisallowToStartReportingMultiple' do
77
+ expect { reporter.start(0.2) }.not_to raise_error
78
+ expect { reporter.start(0.2) }.to raise_error ArgumentError
79
+ end
80
+
81
+ it 'shouldDisallowToStartReportingMultipleTimesOnCustomExecutor' do
82
+ expect { reporter_with_custom_executor.start(0.2) }.not_to raise_error
83
+ expect { reporter_with_custom_executor.start(0.2) }.to raise_error ArgumentError
84
+ end
85
+
86
+ it 'shouldDisallowToStartReportingMultipleTimesOnExternallyManagedExecutor' do
87
+ expect { reporter_with_externally_managed_executor.start(0.2) }.not_to raise_error
88
+ expect { reporter_with_externally_managed_executor.start(0.2) }.to raise_error ArgumentError
89
+ end
90
+
91
+ it 'shouldNotFailOnStopIfReporterWasNotStared' do
92
+ reporters.each do |reporter|
93
+ expect { reporter.stop }.not_to raise_error
94
+ end
95
+ end
96
+
97
+ it 'shouldNotFailWhenStoppingMultipleTimes' do
98
+ reporters[2..2].each do |reporter|
99
+ reporter.start(0.2)
100
+ reporter.stop
101
+ reporter.stop
102
+ reporter.stop
103
+ end
104
+ end
105
+
106
+ it 'shouldShutdownExecutorOnStopByDefault' do
107
+ reporter_with_custom_executor.start(0.2)
108
+ reporter_with_custom_executor.stop
109
+ expect(custom_executor.shutdown?).to eq true
110
+ end
111
+
112
+ it 'shouldNotShutdownExternallyManagedExecutorOnStop' do
113
+ reporter_with_externally_managed_executor.start(0.2)
114
+ reporter_with_externally_managed_executor.stop
115
+ expect(external_executor.shutdown?).to eq false
116
+ end
117
+
118
+
119
+ it 'shouldCancelScheduledFutureWhenStoppingWithExternallyManagedExecutor' do
120
+ # configure very frequency rate of execution
121
+ reporter_with_externally_managed_executor.start(0.001)
122
+ reporter_with_externally_managed_executor.stop
123
+ sleep(0.1)
124
+
125
+ # executionCount should not increase when scheduled future is canceled properly
126
+ execution_count = reporter_with_externally_managed_executor.execution_count.value
127
+ sleep(0.5)
128
+ expect(execution_count).to eq reporter_with_externally_managed_executor.execution_count.value
129
+ end
130
+
131
+
132
+ it 'shouldConvertDurationToMillisecondsPrecisely' do
133
+ expect(2.0E-5).to eq reporter.send(:convert_duration, 20)
134
+ end
135
+ end
@@ -0,0 +1,22 @@
1
+ require 'leafy/core/sliding_window_reservoir'
2
+
3
+ RSpec.describe Leafy::Core::SlidingWindowReservoir do
4
+
5
+ let(:reservoir) { Leafy::Core::SlidingWindowReservoir.new(3) }
6
+
7
+ it 'handles small data streams' do
8
+ reservoir.update(1)
9
+ reservoir.update(2)
10
+
11
+ expect(reservoir.snapshot.values).to eq [1,2]
12
+ end
13
+
14
+ it 'onlyKeepsTheMostRecentFromBigDataStreams' do
15
+ reservoir.update(1)
16
+ reservoir.update(2)
17
+ reservoir.update(3)
18
+ reservoir.update(4)
19
+
20
+ expect(reservoir.snapshot.values).to eq [2, 3, 4]
21
+ end
22
+ end
@@ -0,0 +1,123 @@
1
+ require 'pry'
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+
5
+ class Number
6
+
7
+ def self.[](number, offset = 1)
8
+ new(number, offset)
9
+ end
10
+
11
+ attr_reader :number, :offset
12
+
13
+ def initialize(number, offset = 1)
14
+ @number = number
15
+ @offset = offset
16
+ end
17
+
18
+ def eql?(other)
19
+ offset = other.offset / 100.0
20
+ (number / offset + 0.5).to_i == (other.number / offset + 0.5).to_i
21
+ end
22
+ end
23
+
24
+ # This file was generated by the `rspec --init` command. Conventionally, all
25
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
26
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
27
+ # this file to always be loaded, without a need to explicitly require it in any
28
+ # files.
29
+ #
30
+ # Given that it is always loaded, you are encouraged to keep this file as
31
+ # light-weight as possible. Requiring heavyweight dependencies from this file
32
+ # will add to the boot time of your test suite on EVERY test run, even for an
33
+ # individual file that may not need all of that loaded. Instead, consider making
34
+ # a separate helper file that requires the additional dependencies and performs
35
+ # the additional setup, and require it from the spec files that actually need
36
+ # it.
37
+ #
38
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
39
+ RSpec.configure do |config|
40
+ # rspec-expectations config goes here. You can use an alternate
41
+ # assertion/expectation library such as wrong or the stdlib/minitest
42
+ # assertions if you prefer.
43
+ config.expect_with :rspec do |expectations|
44
+ # This option will default to `true` in RSpec 4. It makes the `description`
45
+ # and `failure_message` of custom matchers include text for helper methods
46
+ # defined using `chain`, e.g.:
47
+ # be_bigger_than(2).and_smaller_than(4).description
48
+ # # => "be bigger than 2 and smaller than 4"
49
+ # ...rather than:
50
+ # # => "be bigger than 2"
51
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
52
+ end
53
+
54
+ # rspec-mocks config goes here. You can use an alternate test double
55
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
56
+ config.mock_with :rspec do |mocks|
57
+ # Prevents you from mocking or stubbing a method that does not exist on
58
+ # a real object. This is generally recommended, and will default to
59
+ # `true` in RSpec 4.
60
+ mocks.verify_partial_doubles = true
61
+ end
62
+
63
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
64
+ # have no way to turn it off -- the option exists only for backwards
65
+ # compatibility in RSpec 3). It causes shared context metadata to be
66
+ # inherited by the metadata hash of host groups and examples, rather than
67
+ # triggering implicit auto-inclusion in groups with matching metadata.
68
+ config.shared_context_metadata_behavior = :apply_to_host_groups
69
+
70
+ # The settings below are suggested to provide a good initial experience
71
+ # with RSpec, but feel free to customize to your heart's content.
72
+ =begin
73
+ # This allows you to limit a spec run to individual examples or groups
74
+ # you care about by tagging them with `:focus` metadata. When nothing
75
+ # is tagged with `:focus`, all examples get run. RSpec also provides
76
+ # aliases for `it`, `describe`, and `context` that include `:focus`
77
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
78
+ config.filter_run_when_matching :focus
79
+
80
+ # Allows RSpec to persist some state between runs in order to support
81
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
82
+ # you configure your source control system to ignore this file.
83
+ config.example_status_persistence_file_path = "spec/examples.txt"
84
+
85
+ # Limits the available syntax to the non-monkey patched syntax that is
86
+ # recommended. For more details, see:
87
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
88
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
89
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
90
+ config.disable_monkey_patching!
91
+
92
+ # This setting enables warnings. It's recommended, but in some cases may
93
+ # be too noisy due to issues in dependencies.
94
+ config.warnings = true
95
+
96
+ # Many RSpec users commonly either run the entire suite or an individual
97
+ # file, and it's useful to allow more verbose output when running an
98
+ # individual spec file.
99
+ if config.files_to_run.one?
100
+ # Use the documentation formatter for detailed output,
101
+ # unless a formatter has already been configured
102
+ # (e.g. via a command-line flag).
103
+ config.default_formatter = "doc"
104
+ end
105
+
106
+ # Print the 10 slowest examples and example groups at the
107
+ # end of the spec run, to help surface which specs are running
108
+ # particularly slow.
109
+ config.profile_examples = 10
110
+
111
+ # Run specs in random order to surface order dependencies. If you find an
112
+ # order dependency and want to debug it, you can fix the order by providing
113
+ # the seed, which is printed after each run.
114
+ # --seed 1234
115
+ config.order = :random
116
+
117
+ # Seed global randomization in this process using the `--seed` CLI option.
118
+ # Setting this allows you to use `--seed` to deterministically reproduce
119
+ # test failures related to randomization by passing the same `--seed` value
120
+ # as the one that triggered the failure.
121
+ Kernel.srand config.seed
122
+ =end
123
+ end