coverband 0.0.19 → 0.0.20
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +94 -55
- data/lib/coverband/reporter.rb +1 -0
- data/lib/coverband/tasks.rb +1 -1
- data/lib/coverband/version.rb +1 -1
- metadata +4 -4
data/README.md
CHANGED
@@ -4,12 +4,14 @@ A gem to measure production code coverage. Coverband allows easy configuration t
|
|
4
4
|
|
5
5
|
* Allow sampling to avoid the performance overhead on every request.
|
6
6
|
* Ignore directories to avoid overhead data collection on vendor, lib, etc.
|
7
|
-
* Take a baseline to get
|
7
|
+
* Take a baseline to get initial app loading coverage.
|
8
8
|
|
9
9
|
At the moment, Coverband relies on Ruby's `set_trace_func` hook. I attempted to use the standard lib's `Coverage` support but it proved buggy when sampling or stoping and starting collection. When [Coverage is patched](https://www.ruby-forum.com/topic/1811306) in future Ruby versions it would likely be better. Using `set_trace_func` has some limitations where it doesn't collect covered lines, but I have been impressed with the coverage it shows for both Sinatra and Rails applications.
|
10
10
|
|
11
11
|
###### Success:
|
12
|
-
After running in production for 30 minutes, we were able very easily delete 2000 LOC after looking through the data. We expect to be able to clean up much more after it has collected more data.
|
12
|
+
After running in production for 30 minutes, we were able very easily delete 2000 LOC after looking through the data. We expect to be able to clean up much more after it has collected more data.
|
13
|
+
|
14
|
+
This has now been running in production on many applications for months. I will clean up configurations, documentation, and strive to get a 1.0 release out soon.
|
13
15
|
|
14
16
|
## Installation
|
15
17
|
|
@@ -33,7 +35,7 @@ $ gem install coverband
|
|
33
35
|
|
34
36
|
## Example Output
|
35
37
|
|
36
|
-
Since Coverband is [Simplecov](https://github.com/colszowka/simplecov) output compatible it should work with any of the `SimpleCov::Formatter`'s available. The output below is produced using the default Simplecov HTML
|
38
|
+
Since Coverband is [Simplecov](https://github.com/colszowka/simplecov) output compatible it should work with any of the `SimpleCov::Formatter`'s available. The output below is produced using the default Simplecov HTML formatter.
|
37
39
|
|
38
40
|
Index Page
|
39
41
|
![image](https://raw.github.com/danmayer/coverband/master/docs/coverband_index.png)
|
@@ -52,59 +54,49 @@ Details on a example Sinatra app
|
|
52
54
|
|
53
55
|
## Usage
|
54
56
|
|
55
|
-
After installing the gem. There are a few steps to gather data, view reports, and
|
57
|
+
After installing the gem. There are a few steps to gather data, view reports, and for cleaning up the data.
|
56
58
|
|
57
|
-
1. First configure
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
* I
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
5. Now to view changes in live coverage run `rake coverband` again, previously it should have only shown the baseline data of your app initializing. After using it in development it hsould show increased coverage from the actions you have exercised.
|
59
|
+
1. First configure cover band options using the config file, See the section below
|
60
|
+
2. Then configure Rake, with helpful tasks. Make sure things are working by recording your Coverband baseline. See the section below
|
61
|
+
3. Setup the rack middleware, the middleware is what makes Coverband gather metrics when your app runs. See below for details
|
62
|
+
* I setup Coverband in my rackup `config.ru` you can also set it up in rails middleware, but it may miss recording some code coverage early in the rails process. It does improve performance to have it later in the middleware stack. So there is a tradeoff there.
|
63
|
+
* To debug in development mode, I recommend turning verbose logging on `config.verbose = true` and passing in the Rails.logger `config.logger = Rails.logger` to the Coverband config. This makes it easy to follow in development mode. Be careful to not leave these on in production as they will effect performance.
|
64
|
+
4. Start your server with `rackup config.ru` If you use `rails s` make sure it is using your `config.ru` or Coverband won't be recording any data.
|
65
|
+
5. Hit your development server exercising the endpoints you want to verify Coverband is recording.
|
66
|
+
6. Now to view changes in live coverage run `rake coverband:coverage` again, previously it should have only shown the baseline data of your app initializing. After using it in development it should show increased coverage from the actions you have exercised.
|
66
67
|
|
67
|
-
####
|
68
|
+
#### Configure Coverband Options
|
68
69
|
|
69
|
-
|
70
|
+
You need to configure cover band you can either do that passing in all configuration options to `Coverband.configure` in block format, or a much simpler style is to call `Coverband.configure` with nothing while will load `config/coverband.rb` expecting it to configure the app correctly. Below is an example config file for a Sinatra app:
|
70
71
|
|
71
72
|
```ruby
|
72
|
-
require '
|
73
|
+
require 'json'
|
74
|
+
|
75
|
+
baseline = if File.exist?('./tmp/coverband_baseline.json')
|
76
|
+
JSON.parse(File.read('./tmp/coverband_baseline.json'))
|
77
|
+
else
|
78
|
+
{}
|
79
|
+
end
|
80
|
+
|
73
81
|
Coverband.configure do |config|
|
74
|
-
config.
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
config.coverage_baseline =
|
82
|
+
config.root = Dir.pwd
|
83
|
+
if defined? Redis
|
84
|
+
config.redis = Redis.new(:host => 'redis.host.com', :port => 49182, :db => 1)
|
85
|
+
end
|
86
|
+
config.coverage_baseline = baseline
|
79
87
|
config.root_paths = ['/app/']
|
80
88
|
config.ignore = ['vendor']
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
desc "get coverage baseline"
|
89
|
-
task :coverband_baseline do
|
90
|
-
Coverband::Reporter.baseline {
|
91
|
-
#rails
|
92
|
-
require File.expand_path("../config/environment", __FILE__)
|
93
|
-
#sinatra
|
94
|
-
#require File.expand_path("./app", __FILE__)
|
95
|
-
}
|
89
|
+
config.percentage = 60.0
|
90
|
+
if defined? Statsd
|
91
|
+
config.stats = Statsd.new('statsd.host.com', 8125)
|
92
|
+
end
|
93
|
+
config.verbose = true
|
96
94
|
end
|
97
95
|
```
|
98
|
-
|
99
|
-
#### Configure rack middleware
|
100
96
|
|
101
|
-
|
97
|
+
Here is a alternative configuration example:
|
102
98
|
|
103
99
|
```ruby
|
104
|
-
require File.dirname(__FILE__) + '/config/environment'
|
105
|
-
|
106
|
-
require 'coverband'
|
107
|
-
|
108
100
|
Coverband.configure do |config|
|
109
101
|
config.root = Dir.pwd
|
110
102
|
config.redis = Redis.new
|
@@ -117,6 +109,51 @@ Coverband.configure do |config|
|
|
117
109
|
config.startup_delay = 10
|
118
110
|
config.percentage = 15.0
|
119
111
|
end
|
112
|
+
```
|
113
|
+
|
114
|
+
#### Configuring Rake
|
115
|
+
|
116
|
+
Either add the below to your `Rakefile` or to a file included in your Rakefile such as `lib/tasks/coverband` if you want to break it up that way.
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
require 'coverband'
|
120
|
+
Coverband.configure
|
121
|
+
require 'coverband/tasks'
|
122
|
+
```
|
123
|
+
This should give you access to a number of cover band tasks
|
124
|
+
|
125
|
+
```bash
|
126
|
+
bundle exec rake -T coverband
|
127
|
+
rake coverband:baseline # record coverband coverage baseline
|
128
|
+
rake coverband:clear # reset coverband coverage data
|
129
|
+
rake coverband:coverage # report runtime coverband code coverage
|
130
|
+
```
|
131
|
+
|
132
|
+
The default Coverband baseline task will try to load the Rails environment. For a non Rails application you can make your own baseline. Below for example is how I take a baseline on a Sinatra app.
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
namespace :coverband do
|
136
|
+
desc "get coverage baseline"
|
137
|
+
task :baseline_app do
|
138
|
+
Coverband::Reporter.baseline {
|
139
|
+
require 'sinatra'
|
140
|
+
require './app.rb'
|
141
|
+
}
|
142
|
+
end
|
143
|
+
end
|
144
|
+
```
|
145
|
+
|
146
|
+
To verify that rake is working run `rake coverband:baseline` and then run `rake coverband:coverage` to view what your baseline coverage looks like before any runtime traffic has been recorded.
|
147
|
+
|
148
|
+
#### Configure rack middleware
|
149
|
+
|
150
|
+
For the best coverage you want this loaded as early as possible. I have been putting it directly in my `config.ru` but you could use an initializer, though you may end up missing some boot up coverage.
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
require File.dirname(__FILE__) + '/config/environment'
|
154
|
+
|
155
|
+
require 'coverband'
|
156
|
+
Coverband.configure
|
120
157
|
|
121
158
|
use Coverband::Middleware
|
122
159
|
run ActionController::Dispatcher.new
|
@@ -124,9 +161,9 @@ run ActionController::Dispatcher.new
|
|
124
161
|
|
125
162
|
#### Configure Manually (for example for background jobs)
|
126
163
|
|
127
|
-
It is easy to use
|
164
|
+
It is easy to use Coverband outside of a Rack environment. Make sure you configure Coverband in whatever environment you are using (such as `config/initializers/*.rb`). Then you can hook into before and after events to add coverage around background jobs, or for any non Rack code.
|
128
165
|
|
129
|
-
For example if you had a base
|
166
|
+
For example if you had a base Resque class, you could use the `before_perform` and `after_perform` hooks to add Coverband
|
130
167
|
|
131
168
|
```ruby
|
132
169
|
def before_perform(*args)
|
@@ -147,7 +184,7 @@ def after_perform(*args)
|
|
147
184
|
end
|
148
185
|
```
|
149
186
|
|
150
|
-
In general you can run
|
187
|
+
In general you can run Coverband anywhere by using the lines below
|
151
188
|
|
152
189
|
```ruby
|
153
190
|
require 'coverband'
|
@@ -173,11 +210,11 @@ coverband.sample {
|
|
173
210
|
## Clearing Line Coverage Data
|
174
211
|
|
175
212
|
After a deploy where code has changed.
|
176
|
-
The line numbers previously recorded in
|
213
|
+
The line numbers previously recorded in Redis may no longer match the current state of the files.
|
177
214
|
If being slightly out of sync isn't as important as gathering data over a long period,
|
178
|
-
you can live with minor
|
215
|
+
you can live with minor inconsistency for some files.
|
179
216
|
|
180
|
-
As often as you like or as part of a deploy hook you can clear the recorded
|
217
|
+
As often as you like or as part of a deploy hook you can clear the recorded Coverband data with the following command.
|
181
218
|
|
182
219
|
```ruby
|
183
220
|
# defaults to the currently configured Coverband.configuration.redis
|
@@ -185,19 +222,21 @@ Coverband::Reporter.clear_coverage
|
|
185
222
|
# or pass in the current target redis
|
186
223
|
Coverband::Reporter.clear_coverage(Redis.new(:host => 'target.com', :port => 6789))
|
187
224
|
```
|
225
|
+
You can also do this with the included rake tasks.
|
226
|
+
|
188
227
|
|
189
228
|
## TODO
|
190
229
|
|
191
|
-
* Improve the
|
192
|
-
*
|
193
|
-
* this is a bit crazy at the moment
|
230
|
+
* Improve the baseline task, hard coded for rails, for Sinatra you need to override.
|
231
|
+
* perhaps allow in the config to pass in baseline_requires array? If not present do the rails thing?
|
194
232
|
* Fix performance by logging to files that purge later
|
195
233
|
* Add support for [zadd](http://redis.io/topics/data-types-intro) so one could determine single hits versus multiple hits on a line, letting us determine the most executed code in production.
|
196
234
|
* Add stats optional support on the number of total requests recorded
|
197
235
|
* Possibly add ability to record code run for a given route
|
198
|
-
*
|
199
|
-
*
|
200
|
-
*
|
236
|
+
* Improve client code api, around manual usage of sampling (like event usage)
|
237
|
+
* Provide a better lighter example app, to show how to use Coverband.
|
238
|
+
* blank rails app
|
239
|
+
* blank Sinatra app
|
201
240
|
|
202
241
|
## Resources
|
203
242
|
|
data/lib/coverband/reporter.rb
CHANGED
data/lib/coverband/tasks.rb
CHANGED
data/lib/coverband/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coverband
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.20
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-03-
|
12
|
+
date: 2014-03-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -184,7 +184,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
184
184
|
version: '0'
|
185
185
|
segments:
|
186
186
|
- 0
|
187
|
-
hash:
|
187
|
+
hash: 834847330280210116
|
188
188
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
189
189
|
none: false
|
190
190
|
requirements:
|
@@ -193,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
193
193
|
version: '0'
|
194
194
|
segments:
|
195
195
|
- 0
|
196
|
-
hash:
|
196
|
+
hash: 834847330280210116
|
197
197
|
requirements: []
|
198
198
|
rubyforge_project:
|
199
199
|
rubygems_version: 1.8.23
|