https://github.com/sveredyuk/perforator
Simple and pretty stupid way to measure execution time of your code
https://github.com/sveredyuk/perforator
logging measurements performance ruby
Last synced: about 1 month ago
JSON representation
Simple and pretty stupid way to measure execution time of your code
- Host: GitHub
- URL: https://github.com/sveredyuk/perforator
- Owner: sveredyuk
- License: mit
- Created: 2017-06-02T11:27:03.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-06-25T07:57:27.000Z (about 8 years ago)
- Last Synced: 2025-03-03T21:18:07.750Z (over 1 year ago)
- Topics: logging, measurements, performance, ruby
- Language: Ruby
- Homepage: https://github.com/sveredyuk/perforator
- Size: 14.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://travis-ci.org/sveredyuk/perforator)
# Perforator
### Simple and pretty stupid way to measure execution time of your code
Quick example
```ruby
meter = Perforator::Meter.new(puts: true)
meter.call do
sleep 1 # doing some serious job
end
# =======>
# Start: 2017-06-02 14:24:53 +0300
# Finish: 2017-06-02 14:24:54 +0300
# Spent: 1.000919
```
More details below
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'perforator'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install perforator
## Usage
### Options
You can init new meter object with following arguments:
```ruby
my_meter = Perforator::Meter.new(
name: 'your label', # -> meter name, just label for passing in start line # =======> your label
logger: Logger.new('my_logger.log'), # -> logger object, will receive :info at each step
puts: true, # -> true/false output to STDOUT
expected_time: 10, # -> time in seconds (!) that expected for execution
positive_callback: proc { puts ':)' }, # -> executed if real execution less than expected
negative_callback: proc { puts ':(' } # -> executed if real execution more than expected
)
```
You can skip any option if you don't need it.
But for callbacks :expected_time is necessary:
```ruby
Perforator::Meter.new(positive_callback: -> { puts ':)' }) #=> NoExpectedTimeError
```
Callbacks must be callable:
```ruby
Perforator::Meter.new(positive_callback: Hash.new) #=> NotCallableCallbackError
```
Expeted time must be fixnum:
```ruby
Perforator::Meter.new(expected_time: '10') #=> NotFixnumExpectedTimeError
```
Just wrap your code with meter:
```ruby
my_meter.call do
# put your code here
end
```
Use `log!` method to log additional info
```ruby
my_meter.call do |meter|
meter.log! 'Any start value'
# put your code here
meter.some_finish 'Some finish value'
end
# =======> your label
# Any start value
# Start: 2017-06-02 14:24:53 +0300
# Finish: 2017-06-02 14:24:54 +0300
# Spent: 1.000919
# Some finish value
```
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/sveredyuk/perforator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).