-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- use bundler to generate the scafold - update the README to the target - update the gemspec
- Loading branch information
Showing
14 changed files
with
211 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--format documentation | ||
--color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
sudo: false | ||
language: ruby | ||
rvm: | ||
- 2.3.1 | ||
before_install: gem install bundler -v 1.12.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in complexity_assert.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 TODO: Write your name | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,101 @@ | ||
# Complexity Assert | ||
# ComplexityAssert | ||
|
||
A utility to assert time complexity of an algorithm in rspec. | ||
They are some performance critical pieces of code that will be executed on huge data sets, which we want to make sure will run fast enough. Unfortunately, enforcing this is not easy, often requiring large scale and slow benchmarks. This rspec library (the result of an experiment to learn machine learning) uses linear regression to determine the time complexity (Big O notation, O(x)) of a piece of code and to check that it is at least as good as what we expect. This does not require huge data sets (only a few large ones) and can be written as any unit test (not as fast though). | ||
|
||
## installation | ||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
```ruby | ||
gem 'complexity_assert', :group => [:test] | ||
``` | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install complexity_assert | ||
|
||
## Usage | ||
|
||
In order to test the complexity of an algorithm, you need to provide 2 things : | ||
|
||
1. the algorithm | ||
2. a way to generate some inputs of varying size | ||
|
||
For this, you need to provide an object that answers to messages `generate_args` and `run`. Here is an example | ||
|
||
``` ruby | ||
class LinearSearch | ||
|
||
def generate_args(size) | ||
[ Array.new(size) { rand(1..size) }, rand(1..size) ] | ||
end | ||
|
||
def run(array, searched) | ||
found = false; | ||
array.each do |element| | ||
if element == array | ||
found = true | ||
end | ||
end | ||
found | ||
end | ||
end | ||
|
||
describe "Linear search" do | ||
|
||
it "performs linearly" do | ||
expect(LinearSearch.new).to be_linear() | ||
expect(LinearSearch.new).not_to be_quadratic() | ||
end | ||
|
||
end | ||
``` | ||
|
||
There are currently 3 matchers : | ||
|
||
* be_constant | ||
* be_linear | ||
* be_quadratic | ||
|
||
More could be added in the future. Every matcher passes if a faster complexity is identified (`be_linear` will pass if the algorithm is detected to be constant). That means that for the moment, `be_quadratic` always passes, and only makes sense to be used with `expect(...).not_to`. | ||
|
||
## Development | ||
|
||
It uses rubybox, simply clone this repo, build the image, and start on. | ||
|
||
```shell | ||
``` | ||
git clone ... | ||
cd ... | ||
docker-compose build | ||
docker-compose run rubybox | ||
``` | ||
|
||
From then on, you're inside the ruby box. | ||
From then on, you're inside the ruby box, 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/[USERNAME]/complexity_assert. | ||
|
||
Here is a quick and uncomplete list of things that could improve this library : | ||
|
||
* Cache the linear regression (it's done twice) | ||
* Factorize / find a way to better generate the sizes, or allow the assertion to specify the sizes | ||
* Allow the assertion to specify the warmup and run rounds | ||
* Robustness against GC : use gc intensive ruby methods, and see how the regression behaves | ||
* O(lnx) : pre-treat with exp() | ||
* O(?lnx) : use exp, then a search for the coefficient (aka polynomial) | ||
* O(xlnx) : there is no well known inverse for that, we can compute it numericaly though | ||
* O(x?) : do some kind of dichotomy or search to find the most probable model | ||
* ... | ||
|
||
As I said, this is still experimental ! Any help is welcome ! | ||
|
||
## License | ||
|
||
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task :default => :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require "bundler/setup" | ||
require "complexity_assert" | ||
|
||
# You can add fixtures and/or initialization code here to make experimenting | ||
# with your gem easier. You can also use a different console, if you like. | ||
|
||
# (If you use this, don't forget to add pry to your Gemfile!) | ||
# require "pry" | ||
# Pry.start | ||
|
||
require "irb" | ||
IRB.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -vx | ||
|
||
bundle install | ||
|
||
# Do any other automated setup that you need to do here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'complexity_assert/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "complexity_assert" | ||
spec.version = ComplexityAssert::VERSION | ||
spec.authors = ["Philippe Bourgau"] | ||
spec.email = ["[email protected]"] | ||
|
||
spec.summary = %q{An experimental rspec library to check the time complexity (Big O(x) notation) of an algorithm.} | ||
spec.description = %q{They are some performance critical pieces of code that will be executed on huge data sets, which we want to make sure will run fast enough. Unfortunately, enforcing this is not easy, often requiring large scale and slow benchmarks. This rspec library (the result of an experiment to learn machine learning) uses linear regression to determine the time complexity (Big O notation, O(x)) of a piece of code and to check that it is at least as good as what we expect. This does not require huge data sets (only a few large ones) and can be written as any unit test (not as fast though).} | ||
spec.homepage = "https://github.com/philou/complexity-assert" | ||
spec.license = "MIT" | ||
|
||
if spec.respond_to?(:metadata) | ||
spec.metadata['allowed_push_host'] = "http://mygemserver.com" | ||
else | ||
raise "RubyGems 2.0 or newer is required to protect against public gem pushes." | ||
end | ||
|
||
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } | ||
spec.bindir = "exe" | ||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_development_dependency "bundler", "~> 1.12" | ||
spec.add_development_dependency "rake", "~> 10.0" | ||
spec.add_development_dependency "rspec", "~> 3.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require "complexity_assert/version" | ||
|
||
module ComplexityAssert | ||
# Your code goes here... | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module ComplexityAssert | ||
VERSION = "0.1.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'spec_helper' | ||
|
||
describe ComplexityAssert do | ||
it 'has a version number' do | ||
expect(ComplexityAssert::VERSION).not_to be nil | ||
end | ||
|
||
it 'does something useful' do | ||
expect(false).to eq(true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) | ||
require 'complexity_assert' |