Skip to content

Commit

Permalink
Add files for a well formed gem
Browse files Browse the repository at this point in the history
- use bundler to generate the scafold
- update the README to the target
- update the gemspec
  • Loading branch information
philou committed Jan 3, 2017
1 parent 26f9324 commit e4bbad8
Show file tree
Hide file tree
Showing 14 changed files with 211 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
5 changes: 5 additions & 0 deletions .travis.yml
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
24 changes: 10 additions & 14 deletions DayBook.org
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
**** go through all starting from the first. Keep the min rmse
**** if a new rmse is found smaller than half the min, we'll pick this one
*** Sometimes, if finds O(n) instead of O(n²). Maybe that's not so much of an issue if the assertion checks that it's at worst O(n²)
** DONE Add +6 pomodoro to the task title
** TODO write the basic rspec integration
*** DONE define the API we want
**** class Algorithm; def generate_args(n) ...; def run(args) ...; end;
Expand All @@ -78,17 +79,12 @@
****** logarithmic : some kind of exponential
****** quadratic : some kind of squar root
**** We just need to assert that it's at worst O(something).
*** TODO package a simple rspec lib
**** Get an internet access
**** download rspec
**** see how to create matchers with rspec3
*** TODO time the overall execution and make it faster
**** DONE reuse the same datapoint for all models
**** TODO cache the linear regression (it's done twice)
*** TODO factorize / find a way to better generate the sizes
** TODO Add +6 pomodoro to the task title
** TODO Robustness against GC : use gc intensive ruby methods, and see how the regression behaves
** TODO O(x?) : do some kind of dichotomy or search to find the most probable model
** TODO O(lnx) : pre-treat with exp()
** TODO O(?lnx) : use exp, then a search for the coefficient (aka polynomial)
** TODO O(xlnx) : there is no well known inverse for that, we can compute it numericaly though
*** DONE Get an internet access
*** TODO fill the readme
*** TODO Check that the gem is well formed
*** TODO download rspec
*** TODO see how to create matchers with rspec3
*** TODO move the files around
*** TODO write a few specs to make sure it works
*** TODO add travis
*** TODO add code climate
3 changes: 3 additions & 0 deletions Gemfile
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
21 changes: 21 additions & 0 deletions LICENSE.txt
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.
95 changes: 90 additions & 5 deletions README.md
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).
6 changes: 6 additions & 0 deletions Rakefile
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
14 changes: 14 additions & 0 deletions bin/console
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
8 changes: 8 additions & 0 deletions bin/setup
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
31 changes: 31 additions & 0 deletions complexity_assert.gemspec
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
5 changes: 5 additions & 0 deletions lib/complexity_assert.rb
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
3 changes: 3 additions & 0 deletions lib/complexity_assert/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ComplexityAssert
VERSION = "0.1.0"
end
11 changes: 11 additions & 0 deletions spec/complexity_assert_spec.rb
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
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
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'

0 comments on commit e4bbad8

Please sign in to comment.