This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Bạn đang xem: Github

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to lớn create this branch?

Launching Visual Studio Code Your codespace will xuất hiện once ready.

There was a problem preparing your codespace, please try again.

Coverband Key Features Installation Redis Gem Installation No custom code or middleware required Rails Sinatra Coverband web UI Mounting as a Rack ứng dụng Standalone Coverband test Example apps Advanced Config Working with environment variables Ignoring Files View Tracking Fixing Coverage Only Shows Loading Hits Avoiding Cache Stampede Redis Hash Store Clear Coverage Coverage Data Migration Adding Rake Tasks outside of Rails Manually Starting Coverband Verbose Debug / Development Mode Solving: stack cấp độ too deep errors Redis Sizing Info Newer Features Dead Method Scanning (ruby 2.6+) Prerequisites Ruby & Rails Version support JRuby tư vấn Contributing to lớn Coverband Tests & Benchmarks Known Issues Debugging Redis Store Diagram logo License




Key Features • Installation • Coverband web UI • Advanced Config • Newer Features • License • Change Log / Roadmap • Code of Conduct

A gem to lớn measure production code usage, showing a counter for the number of times each line of code that is executed. Coverband allows easy configuration lớn collect và report on production code usage. It reports in the background via a thread or can be used as Rack middleware, or manually configured to lớn meet any need.

Note: Coverband is not intended for demo code coverage, for that we recommended using SimpleCov.

Key Features

The primary goal of Coverband is giving deep insight into your production runtime usage of your application code, while having the least impact on performance possible.

Low performance overheadSimple setup and configurationOut of the box tư vấn for all standard code execution paths (web, cron, background jobs, rake tasks, etc)Splits code loading usage (Rails eager load) and runtime usage metricsEasy khổng lồ understand actionable insights from the reportMountable website interface to easily giới thiệu reports



Coverband stores coverage data in Redis. The Redis endpoint is looked for in this order:


The redis store can also be explicitly defined within the config/coverband.rb. See advanced config.

Gem Installation

Add this line to your application"s Gemfile, remember lớn bundle install after updating:

gem "coverband"

No custom code or middleware required

With older versions of coverband, projects would report to lớn redis using rack or sidekiq middleware. After coverband 4.0, this should no longer be required and could cause performance issues. Reporting to lớn redis is now automatically done within a background thread with no custom code needed.

See changelog.


The Railtie integration means you shouldn"t need to do anything else other than ensure Coverband is required after Rails within your Gemfile.


For the best coverage you want this loaded as early as possible. We recommend requiring cover band directly in the Requiring coverband within an initializer could also work, but you may end up missing some boot up coverage. To start collection require Coverband as early as possible.

require "coverband"require File.dirname(__FILE__) + "/config/environment"use Coverband::BackgroundMiddlewarerun

Coverband website UI


The web index as available on the Coverband demo site

View overall coverage information

Drill into individual tệp tin coverage

View individual file details

Clear Coverage - disabled by mặc định as it could be considered a dangerous operation in production. Enable with config.web_enable_clear or leave off and clear from rake task.

Clear coverage report

This will clear the coverage data. This wipes out all collected data.

Clear individual tệp tin coverage

This will clear the details of the file you are looking at. This is helpful if you don"t want to thất bại all Coverage data but made a change that you expect would impact a particular file.

Force coverage collection

This triggers coverage collection on the current webserver process. Useful in development but confusing in production environments where many ruby processes are usually running.

Mounting as a Rack App

Coverband comes with a mountable rack ứng dụng for viewing reports. For Rails this can be done in config/routes.rb with:

Rails.application.routes.draw do mount, at: "/coverage"end
But don"t forget lớn protect your source code with proper authentication. Something lượt thích this when using devise:

Rails.application.routes.draw bởi vì authenticate :user, lambda u.admin? vày mount, at: "/coverage" endend
or you can enable basic auth by setting ENV<"COVERBAND_PASSWORD"> or via your configuration config.password =


The coverage hệ thống can also be started standalone with a rake task:

bundle exec rake coverband:coverage_server
The web UI should then be available here: http://localhost:9022/

If you want lớn run on an alternative port:

COVERBAND_COVERAGE_PORT=8086 bundle exec rake coverband:coverage_server
This is especially useful for projects that are api only và cannot support the mounted rack app. To get production coverage, point coverband at your production redis server và ensure to checkout the production version of your project code locally.

COVERBAND_REDIS_URL=redis://username:password:redis_production_server:2322 bundle exec rake coverband:coverage_server
Coverband DemoTake Coverband for a spin on the live Heroku deployed Coverband Demo. The full source code for the thử nghiệm is available to lớn help with installation, configuration, and understanding of basic usage.

Example apps

Advanced ConfigIf you need to lớn configure coverband, this can be done by creating a config/coverband.rb tệp tin relative to lớn your project root.

By mặc định Coverband will try to store data khổng lồ Redis * Redis endpoint is looked for in this order: ENV<"COVERBAND_REDIS_URL">, ENV<"REDIS_URL">, or localhost

Below is an example config file for a Rails 5 app:

# config/coverband.rb NOT in the initializersCoverband.configure bởi vì |config| = ENV<"MY_REDIS_URL">)) config.logger = Rails.logger # config options false, true. (defaults to lớn false) # true & debug can give helpful and interesting code usage information # và is safe khổng lồ use if one is investigating issues in production, but it will slightly # hit perf. Config.verbose = false # mặc định false. Button at the top of the web interface which clears all data config.web_enable_clear = true # default false. Experimental tư vấn for tracking view layer tracking. # Does not track line-level usage, only indicates if an entire tệp tin # is used or not. Config.track_views = trueend

Working with environment variables

Do you use figaro, mc-settings, dotenv or something else to inject environment variables into your app? If so ensure you have that done BEFORE coverband is required.

For example if you use dotenv, you need to vì this, see

gem "dotenv-rails", require: "dotenv/rails-now"gem "coverband"gem "other-gem-that-requires-env-variables"

Ignoring Files

Sometimes you have files that are known to lớn be valuable perhaps in other environments or something that is just run very infrequently. Opposed lớn having to lớn mentally filter them out of the report, you can just have them ignored in the Coverband reporting by using config.ignore as shown below. Ignore takes a string but can also match with regex rules see how below ignores all rake tasks as an example.

config.ignore += <"config/application.rb", "config/boot.rb", "config/puma.rb", "config/schedule.rb", "bin/*", "config/environments/*", "lib/tasks/*">
Ignoring Custom Gem Locations: Note, if you have your gems in a custom location under your app thư mục you likely want to showroom them khổng lồ config.ignore. For example, if you have your gems not in a default ignored location of app/vendor but have them in app/gems you would need to add gems/* khổng lồ your ignore list.

View Tracking

Coverband allows an optional feature khổng lồ track all view files that are used by an application.

To opt-in lớn this feature... Enable the feature in your Coverband config.

config.track_views = true


Fixing Coverage Only Shows Loading Hits

If all your coverage is being counted as loading or eager_loading coverage, & nothing is showing as runtime Coverage the initialization hook failed for some reason. The most likely reason for this issue is manually calling eager_load! on some Plugin/Gem. If you or a plugin is altering the Rails initialization process, you can manually flip Coverband to runtime coverage by calling these two lines, in an after_initialize block, in application.rb.

Xem thêm: Top 15 Game Nhập Vai Rpg Là Gì Và Top 10 Game Nhập Vai Hay Cho Pc 2020

config.after_initialize vày unless Coverband.tasks_to_ignore? Coverband.report_coverage # record the last of the loading coverage Coverband.runtime_coverage! # mix all future coverage to lớn runtime endend
or if you know you are manually calling eager load anywhere in your initialization process immediately after điện thoại tư vấn those two lines. A user reported an issue after calling ResqueWeb::Engine.eager_load! for example.

Rails.application.routes.draw vị ResqueWeb::Engine.eager_load! Coverband.report_coverage Coverband.runtime_coverage!end

Avoiding Cache Stampede

If you have many servers and they all hit Redis at the same time you can see spikes in your Redis CPU, & memory. This is due khổng lồ a concept called cache stampede. It is better khổng lồ spread out the reporting across your servers. A simple way to vì this is to địa chỉ a random wiggle on your background reporting. This configuration option allows a wiggle. The right amount of wiggle depends on the numbers of servers you have và how willing you are khổng lồ have delays in your coverage reporting. I would recommend at least 1 second per server. Note, the default wiggle is set lớn 30 seconds.

Add a wiggle (in seconds) lớn the background thread to lớn avoid all your servers reporting at the same time:

config.reporting_wiggle = 30

Redis Hash Store

Coverband on very high volume sites with many vps processes reporting can have a race condition which can cause hit counts lớn be inaccurate. Khổng lồ resolve the race condition and reduce Ruby memory overhead we have introduced a new Redis storage option. This moves the some of the work from the Ruby processes khổng lồ Redis. It is worth noting because of this, it has a larger demands on the Redis server. So adjust your Redis instance accordingly. To lớn help reduce the extra redis load you can also change the background reporting frequency.

Use a dedicated coverband redis instance: = redis_url))Adjust from mặc định 30s reporting config.background_reporting_sleep_seconds = 120

See more discussion here.

Clear Coverage

Now that Coverband uses MD5 hashes there should be no reason lớn manually clear coverage unless one is testing, changing versions, possibly debugging Coverband itself.

rake coverband:clear

This can also be done through the website if config.web_enable_clear is enabled.

Coverage Data Migration

Between the release of 4.0 and 4.1 our data format changed. This resets all your coverage data. If you want to restore your previous coverage data, feel không lấy phí to migrate.

rake coverband:migrate

We will be working to tư vấn migrations going forward, when possible

Adding Rake Tasks outside of Rails

Rails apps should automatically include the tasks via the Railtie.

For non Rails apps, either showroom the below lớn your Rakefile or lớn a tệp tin included in your Rakefile such as lib/tasks/coverband.rake if you want lớn break it up that way.

rake -T coverbandrake coverband:clear # reset coverband coverage datarake coverband:coverage # report runtime coverband code coverage

Manually Starting Coverband

Coverband starts on require of the the library which is usually done within the Gemfile. This can be disabled by setting the COVERBAND_DISABLE_AUTO_START environment variable. This environment variable can be useful khổng lồ toggle coverband on and off in certain environments.

NOTE: That any value phối for COVERBAND_DISABLE_AUTO_START is considered true, it does not match the string content but only checks the presence of the ENV variable.

In order khổng lồ start coverband manually yourself when this flag is enabled, call Coverband.configure followed by Coverband.start.

Verbose Debug / Development Mode

Note: to debug issues getting Coverband working. I recommend running in development mode, by turning verbose logging on config.verbose = true và passing in the Rails.logger config.logger = Rails.logger lớn the Coverband config. We respect the log level, and I would recommend log cấp độ info generally, but if you are investigating a problem Coverband logs additional data at the debug level. This makes it easy lớn follow in development mode. Be careful lớn not leave these on in production as they will affect performance.

If you are trying lớn debug locally wondering what code is being run during a request. The verbose modes config.verbose = true && Rails.logger.level = :debug. With true phối it will output the number of lines executed per file, to the passed in log.

Solving: stack cấp độ too deep errors

If you start seeing SystemStackError: stack level too deep errors from background jobs after installing Coverband, this means there is another patch for ResqueWorker that conflicts with Coverband"s patch in your application. Khổng lồ fix this, change coverband gem line in your Gemfile khổng lồ the following:

If you currently have require: false, remove the "coverband" string from the require array above so the gem line becomes like this:

This conflict happens when a ruby method is patched twice, once using module prepend, và once using method aliasing. See this ruby issue for details. The fix is khổng lồ apply all patches the same way. Coverband by default will apply its patch using prepend, but you can change that khổng lồ method aliasing by adding require: <"alternative_coverband_patch"> to the gem line as shown above.

Redis Sizing Info

A few folks have asked about what kích cỡ of Redis is needed lớn run coverband. I have some of our largest services with hundreds of servers on cache.m3.medium with plenty of room to spare. I run most apps on the smallest AWS Redis instances available and bump up only if needed or if I am forced to be on a shared Redis instance, which I try to lớn avoid. On Heroku, I have used it with most of the 3rd các buổi tiệc nhỏ and also been fine on the smallest Redis instances, if you have hundreds of dynos you would likely need to scale up. Also lưu ý there is a tradeoff one can make, Coverband::Adapters::HashRedisStore will use LUA on Redis & increase the Redis load, while being nicer lớn your tiện ích servers and avoid potential lost data during race conditions. While the Coverband::Adapters::RedisStore uses in app memory and merging & has lower load on Redis.

Newer Features

Dead Method Scanning (ruby 2.6+)

Rake task that outputs dead methods based on current coverage data:

---------------------------------------------------------------------------------------------------| tệp tin | class | method | line_number || ./config/routes.rb | AdminConstraint | matches? | 20 || ./app/controllers/home_controller.rb | HomeController | trigger_jobs | 8 || ./app/controllers/home_controller.rb | HomeController | data_tracer | 14 || ./app/controllers/posts_controller.rb | PostsController | edit | 22 || ./app/controllers/posts_controller.rb | PostsController | destroy_bad_dangerously | 73 |---------------------------------------------------------------------------------------------------
PrerequisitesCoverband 3.0.X+ requires Ruby 2.3+Coverband currently requires Redis for production usage

Ruby & Rails Version Support

We will match Heroku và Ruby"s tư vấn lifetime, supporting the last 3 major Ruby releases. For details see supported runtimes.

For Rails, we will follow the policy of the Rails team maintenance policy. We officially support the last two major release versions, while providing minimal support (major bugs / security fixes) for an additional version. This means at the moment we primarily target Rails 6.x, 5.x, & will try khổng lồ keep current functionality working for Rails 4.x but may release new features that bởi vì not work on that target.

JRuby Support

Coverband is compatible with JRuby. If you want to lớn run on JRuby lưu ý that I haven"t benchmarked và I believe the perf impact on older versions of JRuby could be significant. Improved Coverage tư vấn is in JRuby master, & will be in the next release.

older versions of JRuby need tracing enabled khổng lồ work (and this could cause bad performance)run Jruby with the --debug optionadd into your .jrubyrc the debug.fullTrace=true settingFor best performance the oneshot_lines is recommended, và in the latest releases should have very low overheadJRuby is tested via CI against Rails 5 và 6

Contributing to lớn CoverbandIf you are working on adding features, PRs, or bugfixes lớn Coverband this section should help get you going.

Fork itCreate your feature branch (git checkout -b my-new-feature)Commit your changes (git commit -am "Add some feature")Push to the branch (git push origin my-new-feature)Make sure all tests are passing (run bundle install, make sure Redis is running, & then execute rake test)Create new Pull Request

Tests & Benchmarks

If you submit a change please make sure the tests and benchmarks are passing.

run tests:bundle exec rakeBUNDLE_GEMFILE=Gemfile.rails6 bundle exec rake (Same tests using rails 6 instead of 5)view chạy thử coverage: xuất hiện coverage/index.htmlrun the benchmarks before và after your change to lớn see impactrake benchmarksrun a single chạy thử by line number like rspec: bundle exec m test/coverband/reporters/html_test.rb:29

Known Issues

total fail on front over code, for line for line coverage, because of the precompiled template step basically coverage doesn"t work well for erb, slim, và the like.related it will try to report something, but the line numbers reported for ERB files are often off and aren"t considered useful. I recommend filtering out .erb using the config.ignore option. The mặc định configuration excludes these filesNOTE: We now have file cấp độ coverage for view files, but don"t tư vấn line level detailCoverage does NOT work when used alongside Scout APM tự động hóa InstrumentationIn an environment that uses Scout"s AUTO_INSTRUMENT=true (usually production or staging) it stops reporting any coverage, it will show one or two files that have been loaded at the start but everything else will show up as having 0% coverageCoverband, Elastic APM and resqueIn an environment that uses the Elastic APM ruby agent, resque jobs will fail with Transactions may not be nested. Already inside # if the elastic-apm gem is loaded before the coverband gemPut coverage ahead of elastic-apm in your Gemfile

Debugging Redis Store

What files have been synced lớn Redis?

What is the coverage data in Redis?


A diagram of the code.

Xem thêm: Top 9 Game Thú Cưng Hay Nhất Dành Cho Android Hiện Nay, Tải Game Pou Miễn Phí Về Máy



The Coverband hình ảnh was created by Dave Woodall. Thanks Dave!

LicenseThis is a MIT License project...See the file license.txt for copying permission.