diffbench 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "git"
4
+ group :development do
5
+ gem "rspec", "~> 2.8.0"
6
+ gem "rdoc", "~> 3.12"
7
+ gem "cucumber", ">= 0"
8
+ gem "bundler", "~> 1.1.0"
9
+ gem "jeweler", "~> 1.8.3"
10
+ gem "rcov", ">= 0"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ builder (3.0.0)
5
+ cucumber (1.1.9)
6
+ builder (>= 2.1.2)
7
+ diff-lcs (>= 1.1.2)
8
+ gherkin (~> 2.9.0)
9
+ json (>= 1.4.6)
10
+ term-ansicolor (>= 1.0.6)
11
+ diff-lcs (1.1.3)
12
+ gherkin (2.9.0)
13
+ json (>= 1.4.6)
14
+ git (1.2.5)
15
+ jeweler (1.8.3)
16
+ bundler (~> 1.0)
17
+ git (>= 1.2.5)
18
+ rake
19
+ rdoc
20
+ json (1.6.5)
21
+ rake (0.9.2.2)
22
+ rcov (0.9.9)
23
+ rdoc (3.12)
24
+ json (~> 1.4)
25
+ rspec (2.8.0)
26
+ rspec-core (~> 2.8.0)
27
+ rspec-expectations (~> 2.8.0)
28
+ rspec-mocks (~> 2.8.0)
29
+ rspec-core (2.8.0)
30
+ rspec-expectations (2.8.0)
31
+ diff-lcs (~> 1.1.2)
32
+ rspec-mocks (2.8.0)
33
+ term-ansicolor (1.0.7)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ bundler (~> 1.1.0)
40
+ cucumber
41
+ git
42
+ jeweler (~> 1.8.3)
43
+ rcov
44
+ rdoc (~> 3.12)
45
+ rspec (~> 2.8.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Bogdan Gusiev
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Diffbench
2
+
3
+ Diffbench is tool the I end up during many many performance patches to:
4
+
5
+ * Rails
6
+ * ActiveRecord
7
+ * ActiveModel
8
+ * ActiveSupport
9
+ * Mail
10
+
11
+ It runs a same benchmark code before and after applying a patch
12
+ TODO links to original PRs
13
+
14
+ ## Installation
15
+
16
+ ``` sh
17
+ gem install diffbench
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ Create the following benchmark file in the git root of the project:
23
+
24
+ ``` ruby
25
+ require 'diffbench'
26
+
27
+ $LOAD_PATH << "./lib"
28
+ require "mail"
29
+
30
+ DiffBench.bm do
31
+ report("headers parsing when long") do
32
+ Mail::Header.new("X-Subscriber: 1111\n"* 1000)
33
+ end
34
+ report("headers parsing when tiny") do
35
+ Mail::Header.new("X-Subscriber: 1111\n"* 10)
36
+ end
37
+ report("headers parsing when empty") do
38
+ Mail::Header.new("")
39
+ end
40
+ end
41
+ ```
42
+
43
+ Run:
44
+
45
+ ``` sh
46
+ diffbench <file>
47
+ ```
48
+
49
+ If the working tree is dirty than diffbench will run benchmark against dirty and clean tree.
50
+ If the working tree is not dirty than diffbench will run benchmark against current HEAD and commit previous to HEAD.
51
+
52
+
53
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "diffbench"
18
+ gem.executables = ['diffbench']
19
+ gem.homepage = "http://github.com/bogdan/diffbench"
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{Benchmark your before and after some changes made}
22
+ gem.description = %Q{Diffbench is gem designed to benchmark the performance patches. It can run specified benchmark file before and after some changes made and show performance comparation result}
23
+ gem.email = "agresso@gmail.com"
24
+ gem.authors = ["Bogdan Gusiev"]
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ require 'cucumber/rake/task'
41
+ Cucumber::Rake::Task.new(:features)
42
+
43
+ task :default => :spec
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "diffbench #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/bin/diffbench ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'diffbench'
4
+
5
+ DiffBench.run(*ARGV)
data/diffbench.gemspec ADDED
@@ -0,0 +1,75 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "diffbench"
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Bogdan Gusiev"]
12
+ s.date = "2012-03-15"
13
+ s.description = "Diffbench is gem designed to benchmark the performance patches. It can run specified benchmark file before and after some changes made and show performance comparation result"
14
+ s.email = "agresso@gmail.com"
15
+ s.executables = ["diffbench"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".rspec",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "bin/diffbench",
30
+ "diffbench.gemspec",
31
+ "features/diffbench.feature",
32
+ "features/step_definitions/diffbench_steps.rb",
33
+ "features/support/env.rb",
34
+ "lib/diff_bench.rb",
35
+ "lib/diffbench.rb",
36
+ "spec/diffbench_spec.rb",
37
+ "spec/spec_helper.rb"
38
+ ]
39
+ s.homepage = "http://github.com/bogdan/diffbench"
40
+ s.licenses = ["MIT"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = "1.8.10"
43
+ s.summary = "Benchmark your before and after some changes made"
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<git>, [">= 0"])
50
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
51
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
52
+ s.add_development_dependency(%q<cucumber>, [">= 0"])
53
+ s.add_development_dependency(%q<bundler>, ["~> 1.1.0"])
54
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
55
+ s.add_development_dependency(%q<rcov>, [">= 0"])
56
+ else
57
+ s.add_dependency(%q<git>, [">= 0"])
58
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
59
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
60
+ s.add_dependency(%q<cucumber>, [">= 0"])
61
+ s.add_dependency(%q<bundler>, ["~> 1.1.0"])
62
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
63
+ s.add_dependency(%q<rcov>, [">= 0"])
64
+ end
65
+ else
66
+ s.add_dependency(%q<git>, [">= 0"])
67
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
68
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
69
+ s.add_dependency(%q<cucumber>, [">= 0"])
70
+ s.add_dependency(%q<bundler>, ["~> 1.1.0"])
71
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
72
+ s.add_dependency(%q<rcov>, [">= 0"])
73
+ end
74
+ end
75
+
@@ -0,0 +1,9 @@
1
+ Feature: something something
2
+ In order to something something
3
+ A user something something
4
+ something something something
5
+
6
+ Scenario: something something
7
+ Given inspiration
8
+ When I create a sweet new gem
9
+ Then everyone should see how awesome I am
File without changes
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ begin
3
+ Bundler.setup(:default, :development)
4
+ rescue Bundler::BundlerError => e
5
+ $stderr.puts e.message
6
+ $stderr.puts "Run `bundle install` to install missing gems"
7
+ exit e.status_code
8
+ end
9
+
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
11
+ require 'diffbench'
12
+
13
+ require 'rspec/expectations'
data/lib/diff_bench.rb ADDED
@@ -0,0 +1 @@
1
+ require 'diffbench'
data/lib/diffbench.rb ADDED
@@ -0,0 +1,121 @@
1
+ require "yaml"
2
+ require "benchmark"
3
+ require "git"
4
+
5
+ class DiffBench
6
+
7
+ class Runner
8
+ def initialize(file, *args)
9
+ @file = file
10
+ unless @file
11
+ raise Error, "File not specified"
12
+ end
13
+ end
14
+
15
+ def run
16
+ puts "Running benchmark with current working tree"
17
+ first_run = run_file
18
+ if tree_dirty?
19
+ puts "Stashing changes"
20
+ git_run "stash"
21
+ puts "Running benchmark with clean working tree"
22
+ begin
23
+ second_run = run_file
24
+ ensure
25
+ puts "Applying stashed changes back"
26
+ git_run "stash pop"
27
+ end
28
+ else
29
+ branch = git.current_branch.to_s
30
+ raise Error, "No current branch. TODO: support this use case" if branch == "(no branch)"
31
+ puts "Checkout HEAD^"
32
+ git_run "checkout 'HEAD^'"
33
+ puts "Running benchmark with HEAD^"
34
+ begin
35
+ second_run = run_file
36
+ ensure
37
+ puts "Checkout to previous HEAD again"
38
+ git_run "checkout #{branch}"
39
+ end
40
+
41
+ end
42
+ puts ""
43
+ caption = "Before patch: ".gsub(/./, " ") + Benchmark::Tms::CAPTION
44
+ puts caption
45
+ first_run.keys.each do |test|
46
+ puts ("-"* (caption.size - test.size)) + test
47
+ puts "After patch: #{first_run[test].format}"
48
+ puts "Before patch: #{second_run[test].format}"
49
+ puts ""
50
+ end
51
+ end
52
+
53
+ def run_file
54
+ output = `ruby #{@file}`
55
+ begin
56
+ result = YAML.load(output)
57
+ raise Error, "Can not parse result of ruby script: \n #{output}" unless result.is_a?(Hash)
58
+ result
59
+ rescue Psych::SyntaxError
60
+ raise Error, "Can not run ruby script: \n#{output}"
61
+ end
62
+ end
63
+
64
+ def git_run(command)
65
+ git.lib.send(:command, command)
66
+ end
67
+
68
+ def git
69
+ @git ||= Git.open(discover_git_dir)
70
+ end
71
+
72
+ def discover_git_dir
73
+ tokens = ENV['PWD'].split("/")
74
+ while tokens.any?
75
+ path = tokens.join("/")
76
+ if File.exists?(path + "/.git")
77
+ return path
78
+ end
79
+ tokens.pop
80
+ end
81
+ raise Error, "Git working dir not found"
82
+ end
83
+
84
+ def tree_dirty?
85
+ status = git.status
86
+ status.deleted.any? || status.changed.any? || status.added.any?
87
+ end
88
+ end
89
+
90
+ class << self
91
+
92
+ def run(*args)
93
+ Runner.new(*args).run
94
+ end
95
+ def bm(&block)
96
+ DiffBench::Bm.new(&block)
97
+ end
98
+
99
+ end
100
+
101
+ class Bm
102
+ def initialize(&block)
103
+ @measures = {}
104
+ if block.arity == -1 || block.arity > 0
105
+ block.call(self)
106
+ else
107
+ instance_eval(&block)
108
+ end
109
+
110
+ puts @measures.to_yaml
111
+ end
112
+
113
+
114
+ def report(label)
115
+ @measures[label] = Benchmark.measure do
116
+ yield
117
+ end
118
+ end
119
+ end
120
+ class Error < StandardError; end
121
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Diffbench" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'diffbench'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: diffbench
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bogdan Gusiev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: git
16
+ requirement: &23114340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *23114340
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &23113380 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.8.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *23113380
36
+ - !ruby/object:Gem::Dependency
37
+ name: rdoc
38
+ requirement: &25376960 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.12'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *25376960
47
+ - !ruby/object:Gem::Dependency
48
+ name: cucumber
49
+ requirement: &25375620 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *25375620
58
+ - !ruby/object:Gem::Dependency
59
+ name: bundler
60
+ requirement: &25374660 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.1.0
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *25374660
69
+ - !ruby/object:Gem::Dependency
70
+ name: jeweler
71
+ requirement: &25373700 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 1.8.3
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *25373700
80
+ - !ruby/object:Gem::Dependency
81
+ name: rcov
82
+ requirement: &25373020 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *25373020
91
+ description: Diffbench is gem designed to benchmark the performance patches. It can
92
+ run specified benchmark file before and after some changes made and show performance
93
+ comparation result
94
+ email: agresso@gmail.com
95
+ executables:
96
+ - diffbench
97
+ extensions: []
98
+ extra_rdoc_files:
99
+ - LICENSE.txt
100
+ - README.md
101
+ files:
102
+ - .document
103
+ - .rspec
104
+ - Gemfile
105
+ - Gemfile.lock
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - VERSION
110
+ - bin/diffbench
111
+ - diffbench.gemspec
112
+ - features/diffbench.feature
113
+ - features/step_definitions/diffbench_steps.rb
114
+ - features/support/env.rb
115
+ - lib/diff_bench.rb
116
+ - lib/diffbench.rb
117
+ - spec/diffbench_spec.rb
118
+ - spec/spec_helper.rb
119
+ homepage: http://github.com/bogdan/diffbench
120
+ licenses:
121
+ - MIT
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ segments:
133
+ - 0
134
+ hash: 2548980219837112805
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 1.8.10
144
+ signing_key:
145
+ specification_version: 3
146
+ summary: Benchmark your before and after some changes made
147
+ test_files: []