heroku-vector 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,61 @@
1
+ require File.absolute_path File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ describe HerokuVector::Sampler do
4
+ describe '#initialize' do
5
+ let(:capacity) { 10 }
6
+ let(:sampler) { HerokuVector::Sampler.new(capacity) }
7
+
8
+ it 'should set capacity' do
9
+ assert_equal capacity, sampler.capacity
10
+ end
11
+
12
+ it 'should initialize data container' do
13
+ assert_equal [], sampler.data
14
+ end
15
+ end
16
+
17
+ describe '#push' do
18
+ let(:sampler) { HerokuVector::Sampler.new(10) }
19
+
20
+ it 'should append item to data' do
21
+ assert_equal [], sampler.data
22
+
23
+ sampler.push 1
24
+ assert_equal [1], sampler.data
25
+
26
+ sampler.push 2
27
+ assert_equal [1, 2], sampler.data
28
+ end
29
+
30
+ it 'should cap capacity' do
31
+ (1..10).each {|i| sampler << i }
32
+ assert_equal (1..10).to_a, sampler.data
33
+
34
+ sampler << 11
35
+ assert_equal (2..11).to_a, sampler.data
36
+ end
37
+
38
+ it 'should alias << to push' do
39
+ assert_equal [], sampler.data
40
+
41
+ sampler << 1
42
+ assert_equal [1], sampler.data
43
+ end
44
+ end
45
+
46
+ describe '#mean' do
47
+ let(:sampler) { HerokuVector::Sampler.new(10) }
48
+
49
+ it 'should handle empty data' do
50
+ assert_equal 0.0, sampler.mean
51
+ end
52
+
53
+ it 'should calculate mean value' do
54
+ sampler << 1
55
+ assert_equal 1.0, sampler.mean
56
+
57
+ (2..4).each {|i| sampler << i }
58
+ assert_equal 2.5, sampler.mean
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,77 @@
1
+ require File.absolute_path File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ describe HerokuVector::Source::NewRelic do
4
+ let(:api_key) { 'api_key' }
5
+ let(:account_id) { 1 }
6
+ let(:app_id) { 2 }
7
+
8
+ let(:source) do
9
+ HerokuVector::Source::NewRelic.new(
10
+ api_key: api_key
11
+ )
12
+ end
13
+
14
+ describe '#initialize' do
15
+ it 'should load newrelic defaults from HerokuVector' do
16
+ HerokuVector.expects(:newrelic_api_key => api_key)
17
+ HerokuVector.expects(:newrelic_app_id => app_id)
18
+ HerokuVector.expects(:newrelic_account_id => account_id)
19
+
20
+ source = HerokuVector::Source::NewRelic.new
21
+
22
+ assert_equal api_key, source.api_key
23
+ assert_equal app_id, source.app_id
24
+ assert_equal account_id, source.account_id
25
+ end
26
+
27
+ it 'should take options on init' do
28
+ HerokuVector.expects(:newrelic_api_key).never
29
+ HerokuVector.expects(:newrelic_app_id).never
30
+ HerokuVector.expects(:newrelic_account_id).never
31
+
32
+ source = HerokuVector::Source::NewRelic.new(api_key: api_key, app_id: app_id, account_id: account_id)
33
+
34
+ assert_equal api_key, source.api_key
35
+ assert_equal app_id, source.app_id
36
+ assert_equal account_id, source.account_id
37
+ end
38
+ end
39
+
40
+ describe '#newrelic_account' do
41
+ it 'should lookup account from New Relic API' do
42
+ VCR.use_cassette('newrelic_account') do
43
+ account = source.newrelic_account
44
+ assert_equal api_key, account.api_key
45
+ end
46
+ end
47
+ end
48
+
49
+ describe '#newrelic_app' do
50
+ it 'should load app from NewRelic API' do
51
+ VCR.use_cassette('newrelic_app') do
52
+ app = source.newrelic_app
53
+
54
+ assert_equal 'polar-rails-staging', app.name
55
+ end
56
+ end
57
+ end
58
+
59
+ describe '#throughput' do
60
+ it 'should load threshold values and throughput from NewRelic API' do
61
+ VCR.use_cassette('newrelic_threshold_values') do
62
+ value = source.throughput
63
+ assert_equal 123, value
64
+ end
65
+ end
66
+ end
67
+
68
+ describe 'source requirements' do
69
+ it 'should implement sample' do
70
+ assert source.respond_to?(:sample)
71
+ end
72
+
73
+ it 'should implement unit' do
74
+ assert source.respond_to?(:unit)
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,78 @@
1
+ require File.absolute_path File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ describe HerokuVector::Source::Sidekiq do
4
+
5
+ let(:source) do
6
+ HerokuVector::Source::Sidekiq.new
7
+ end
8
+
9
+ describe '#initialize' do
10
+ it 'should configure Sidekiq with redis client' do
11
+ redis = mock('redis')
12
+ source.expects(:build_redis_client => redis)
13
+
14
+ Sidekiq.redis {|sidekiq_redis| assert_equal redis, sidekiq_redis }
15
+ end
16
+ end
17
+
18
+ describe '#build_redis_client' do
19
+ it 'should use redis passed as option' do
20
+ redis = mock('redis')
21
+ assert_equal redis, source.build_redis_client(redis: redis)
22
+ end
23
+
24
+ it 'should build client with url passed as option' do
25
+ redis = source.build_redis_client(redis_url: 'redis://localhost:1111')
26
+
27
+ assert_equal 'redis://localhost:1111', redis.client.options[:url]
28
+ end
29
+
30
+ it 'should default to redis URL from app config' do
31
+ HerokuVector.expects(:sidekiq_redis_url => 'redis://localhost:2222')
32
+ redis = source.build_redis_client
33
+
34
+ assert_equal 'redis://localhost:2222', redis.client.options[:url]
35
+ end
36
+
37
+ it 'should use app config namespace' do
38
+ HerokuVector.expects(:sidekiq_redis_namespace => 'sidekiq')
39
+ redis = source.build_redis_client
40
+
41
+ assert redis.is_a? Redis::Namespace
42
+ assert_equal 'sidekiq', redis.namespace
43
+ end
44
+
45
+ it 'should use option namespace' do
46
+ redis = source.build_redis_client(redis_namespace: 'sidekiq')
47
+
48
+ assert redis.is_a? Redis::Namespace
49
+ assert_equal 'sidekiq', redis.namespace
50
+ end
51
+ end
52
+
53
+ describe '#busy_workers' do
54
+ let(:sidekiq_processes) do
55
+ [
56
+ {"concurrency"=>10, "queues"=>["default"], "busy"=>1 },
57
+ {"concurrency"=>10, "queues"=>["default"], "busy"=>3 },
58
+ ]
59
+ end
60
+
61
+ it 'should sum total busy threads from worker processes' do
62
+ source.expects(:sidekiq_processes => sidekiq_processes)
63
+
64
+ assert_equal 4, source.busy_workers
65
+ end
66
+ end
67
+
68
+ describe 'source requirements' do
69
+ it 'should implement sample' do
70
+ assert source.respond_to?(:sample)
71
+ end
72
+
73
+ it 'should implement unit' do
74
+ assert source.respond_to?(:unit)
75
+ end
76
+ end
77
+
78
+ end
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'heroku_vector'
5
+
6
+ require 'minitest/autorun'
7
+ require 'mocha/mini_test'
8
+ require 'minitest/pride'
9
+
10
+ require 'webmock/minitest'
11
+ require 'vcr'
12
+
13
+ require 'timecop'
14
+
15
+ require 'pry'
16
+
17
+ WebMock.disable_net_connect! unless ENV['RECORD_NET']
18
+
19
+ VCR.configure do |c|
20
+ c.cassette_library_dir = 'test/fixtures/vcr_cassettes'
21
+ c.hook_into :webmock
22
+ end
23
+
24
+ HerokuVector.logger = Logger.new('/dev/null') if File.exist?('/dev/null')
25
+
26
+ # Webmock monkey-patch for Excon bug :(
27
+ # See: https://github.com/bblimke/webmock/issues/403
28
+ class WebMock::HttpLibAdapters::ExconAdapter
29
+
30
+ def self.request_params_from(hash)
31
+ hash = hash.dup
32
+ if Excon::VERSION >= '0.27.5'
33
+ request_keys = Excon::VALID_REQUEST_KEYS
34
+ hash.reject! {|key,_| !request_keys.include?(key) }
35
+ end
36
+ PARAMS_TO_DELETE.each { |key| hash.delete(key) }
37
+ hash
38
+ end
39
+
40
+ end
metadata ADDED
@@ -0,0 +1,313 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heroku-vector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Winfield Peterson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activeresource
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: heroku-api
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: newrelic_api
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: redis-namespace
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sidekiq
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: dotenv
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: eventmachine
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: minitest
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: mocha
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: bundler
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '1.6'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '1.6'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rake
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: timecop
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: webmock
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ - !ruby/object:Gem::Dependency
210
+ name: vcr
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - ">="
221
+ - !ruby/object:Gem::Version
222
+ version: '0'
223
+ - !ruby/object:Gem::Dependency
224
+ name: pry
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - ">="
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ type: :development
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - ">="
235
+ - !ruby/object:Gem::Version
236
+ version: '0'
237
+ description: Linearly scale Heroku dyno counts based on sampled metrics
238
+ email:
239
+ - winfield.peterson@gmail.com
240
+ executables:
241
+ - heroku_vector
242
+ extensions: []
243
+ extra_rdoc_files: []
244
+ files:
245
+ - ".gitignore"
246
+ - CHANGELOG.md
247
+ - Gemfile
248
+ - Gemfile.lock
249
+ - README.md
250
+ - Rakefile
251
+ - bin/heroku_vector
252
+ - config.rb.example
253
+ - diagrams/Architecture.pptx
254
+ - diagrams/Architecture/Slide1.png
255
+ - examples/upstart_conf
256
+ - heroku-vector.gemspec
257
+ - lib/heroku_vector.rb
258
+ - lib/heroku_vector/dyno_scaler.rb
259
+ - lib/heroku_vector/engine/heroku.rb
260
+ - lib/heroku_vector/helper.rb
261
+ - lib/heroku_vector/process_manager.rb
262
+ - lib/heroku_vector/sampler.rb
263
+ - lib/heroku_vector/source/new_relic.rb
264
+ - lib/heroku_vector/source/sidekiq.rb
265
+ - lib/heroku_vector/version.rb
266
+ - lib/heroku_vector/worker.rb
267
+ - test/fixtures/vcr_cassettes/heroku_dyno_types.yml
268
+ - test/fixtures/vcr_cassettes/heroku_scale_dynos.yml
269
+ - test/fixtures/vcr_cassettes/newrelic_account.yml
270
+ - test/fixtures/vcr_cassettes/newrelic_app.yml
271
+ - test/fixtures/vcr_cassettes/newrelic_threshold_values.yml
272
+ - test/heroku_vector/dyno_scaler_test.rb
273
+ - test/heroku_vector/engine/heroku_test.rb
274
+ - test/heroku_vector/sampler_test.rb
275
+ - test/heroku_vector/source/new_relic_test.rb
276
+ - test/heroku_vector/source/sidekiq_test.rb
277
+ - test/test_helper.rb
278
+ homepage: https://github.com/wpeterson/heroku-vector
279
+ licenses:
280
+ - MIT
281
+ metadata: {}
282
+ post_install_message:
283
+ rdoc_options: []
284
+ require_paths:
285
+ - lib
286
+ required_ruby_version: !ruby/object:Gem::Requirement
287
+ requirements:
288
+ - - ">="
289
+ - !ruby/object:Gem::Version
290
+ version: '0'
291
+ required_rubygems_version: !ruby/object:Gem::Requirement
292
+ requirements:
293
+ - - ">="
294
+ - !ruby/object:Gem::Version
295
+ version: '0'
296
+ requirements: []
297
+ rubyforge_project:
298
+ rubygems_version: 2.2.2
299
+ signing_key:
300
+ specification_version: 4
301
+ summary: Sampling Auto-scaler for Heroku dynos
302
+ test_files:
303
+ - test/fixtures/vcr_cassettes/heroku_dyno_types.yml
304
+ - test/fixtures/vcr_cassettes/heroku_scale_dynos.yml
305
+ - test/fixtures/vcr_cassettes/newrelic_account.yml
306
+ - test/fixtures/vcr_cassettes/newrelic_app.yml
307
+ - test/fixtures/vcr_cassettes/newrelic_threshold_values.yml
308
+ - test/heroku_vector/dyno_scaler_test.rb
309
+ - test/heroku_vector/engine/heroku_test.rb
310
+ - test/heroku_vector/sampler_test.rb
311
+ - test/heroku_vector/source/new_relic_test.rb
312
+ - test/heroku_vector/source/sidekiq_test.rb
313
+ - test/test_helper.rb