multitest 0.1.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
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ tmp
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Nick Gauthier
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.rdoc ADDED
@@ -0,0 +1,89 @@
1
+ = multitest
2
+
3
+ == Caveats
4
+
5
+ Multitest is still very young, it is in active development and is no where near complete or robust.
6
+
7
+ Multitest currently only works in a rails environment. It's only because I use "constantize".
8
+
9
+ If you depend on fixture data in your database, you may have data inconsistency errors. I like to use an empty DB and then use factories with transactional tests, and this runs great.
10
+
11
+ == Installation
12
+
13
+ gem install ngauthier-multitest
14
+
15
+ == Usage
16
+
17
+ in your Rakefile:
18
+
19
+ require 'multitest'
20
+ require 'multitest/tasks'
21
+
22
+ on the commandline:
23
+
24
+ rake multitest
25
+ rake multitest:units multitest:functionals multitest:integration
26
+
27
+ == Configuration
28
+
29
+ in your Rakefile:
30
+
31
+ Multitest.cores = 8
32
+
33
+ == Benchmarks
34
+
35
+ This is one of our largest apps.
36
+
37
+ === Normal suite
38
+
39
+ time rake test
40
+ 259.03user 27.50system 5:14.76elapsed 91%CPU
41
+
42
+ === 2 Cores
43
+
44
+ time rake multitest
45
+ 213.86user 23.38system 2:41.24elapsed 147%CPU
46
+
47
+ === 4 Cores
48
+
49
+ time rake multitest
50
+ 223.18user 24.53system 2:08.96elapsed 192%CPU
51
+
52
+ Note the wall-time differences of 5:14 vs 2:41. 195% speedup on two cores. On 4 cores there is only a speedup of 243%, but that's still quicker. If you send me impressive benchmarks, I'll be happy to put them up here (8 cores anyone?).
53
+
54
+ == Require Errors
55
+
56
+ If you get require errors like:
57
+
58
+ Running multitest:units
59
+ rake aborted!
60
+ no such file to load -- test_helper
61
+
62
+ Then you may want to use more absolute paths in your tests. For example, if you had:
63
+
64
+ # test/unit/my_class.rb
65
+ require 'test_helper'
66
+ class MyClass < ActiveSupport::TestCase
67
+ end
68
+
69
+ You should change it to:
70
+
71
+ # test/unit/my_class.rb
72
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
73
+ class MyClass < ActiveSupport::TestCase
74
+ end
75
+
76
+ == Note on Patches/Pull Requests
77
+
78
+ * Fork the project.
79
+ * Make your feature addition or bug fix.
80
+ * Add tests for it. This is important so I don't break it in a
81
+ future version unintentionally.
82
+ * Commit, do not mess with rakefile, version, or history.
83
+ (if you want to have your own version, that is fine but
84
+ bump version in a commit by itself I can ignore when I pull)
85
+ * Send me a pull request. Bonus points for topic branches.
86
+
87
+ == Copyright
88
+
89
+ Copyright (c) 2009 Nick Gauthier. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "multitest"
8
+ gem.summary = "Run your tests in parallel"
9
+ gem.description = "Run your tests across multiple cores automatically."
10
+ gem.email = "nick@smartlogicsolutions.com"
11
+ gem.homepage = "http://github.com/ngauthier/multitest"
12
+ gem.authors = ["Nick Gauthier"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ gem.files.include(['lib/**/*.rb'])
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/multitest_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "multitest #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/lib/multitest.rb ADDED
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__), 'multitest', 'multitest.rb')
2
+ #require File.join(File.dirname(__FILE__), 'multitest', 'tasks.rb')
@@ -0,0 +1,130 @@
1
+ require File.join(File.dirname(__FILE__), 'pipe_dream')
2
+ require File.join(File.dirname(__FILE__), 'safe_fork')
3
+ require 'test/unit/testresult'
4
+ require 'test/unit'
5
+
6
+ # The ability to test multiple files across multiple cores
7
+ # simultaneously
8
+ class Multitest
9
+ @@cores = 2
10
+ def self.cores
11
+ @@cores
12
+ end
13
+ def self.cores=(c)
14
+ @@cores = c
15
+ end
16
+ # Takes an options hash.
17
+ # :files => files to test
18
+ # :cores => number of cores to use
19
+ def initialize(files, cores = Multitest.cores)
20
+ @files = files
21
+ @cores = cores
22
+ @children = []
23
+ @threads = []
24
+ @pipes = []
25
+
26
+ Test::Unit.run = true
27
+ @files.each{|f| load f}
28
+ end
29
+
30
+ # Run the tests that have been setup
31
+ def run
32
+ return if @files.empty?
33
+ $stderr.write @files.inspect+"\n"; $stderr.flush
34
+ @cores.times do |c|
35
+ @pipes << PipeDream.new
36
+ @children << SafeFork.fork do
37
+ Signal.trap("TERM") { exit }
38
+ Signal.trap("HUP") { exit }
39
+ pipe = @pipes.last
40
+ pipe.identify_as_child
41
+ pipe.write("[Worker #{c}] Booted\n")
42
+ @result = Test::Unit::TestResult.new
43
+ @result.add_listener(Test::Unit::TestResult::FAULT) do |value|
44
+ $stderr.write value
45
+ $stderr.write "\n\n"
46
+ $stderr.flush
47
+ end
48
+ while !pipe.eof?
49
+ file = pipe.gets.chomp
50
+ begin
51
+ pipe.write "[Worker #{c} Starting: #{file}\n"
52
+ start = Time.now
53
+
54
+ klasses = Multitest.find_classes_in_file(file)
55
+ klasses.each{|k| k.suite.run(@result){|status, name| ;}}
56
+
57
+ finish = Time.now
58
+ pipe.write "[Worker #{c}] Completed: #{file} (#{finish-start})\n"
59
+ rescue => ex
60
+ pipe.write "[Worker #{c}] Failed: #{file} - #{ex.to_s}\n"
61
+ end
62
+ end
63
+ $stderr.write @result.to_s
64
+ $stderr.write "\n"
65
+ $stderr.flush
66
+ pipe.close
67
+ end
68
+ end
69
+
70
+ total_files = @files.size
71
+ @threads = []
72
+ @pipes.each do |_p|
73
+ @threads << Thread.new(_p) do |p|
74
+ Signal.trap("TERM") { exit }
75
+ p.identify_as_parent
76
+ # boot message
77
+ p.gets
78
+ while !@files.empty?
79
+ # puts "#{total_files - @files.size}/#{total_files}"
80
+ # send a file
81
+ p.write("#{@files.pop}\n")
82
+ # print the start message
83
+ msg = p.gets
84
+ # $stdout.write msg; $stdout.flush
85
+ # wait for the complete message
86
+ msg = p.gets
87
+ # print complete message
88
+ if msg =~ /Completed/
89
+ # $stdout.write msg; $stdout.flush
90
+ else
91
+ $stderr.write msg; $stderr.flush
92
+ end
93
+ end
94
+ p.close
95
+ end
96
+ end
97
+
98
+ Signal.trap("TERM") do
99
+ puts "Exiting"
100
+ @children.each{|c| Process.kill("TERM",c)}
101
+ @threads.each{|t| Thread.kill(t)}
102
+ end
103
+
104
+ @threads.each{|t| t.join}
105
+ @children.each{|c| Process.wait(c)}
106
+ end
107
+
108
+ def self.find_classes_in_file(f)
109
+ code = ""
110
+ File.open(f) {|buffer| code = buffer.read}
111
+ matches = code.scan(/class\s+([\S]+)/)
112
+ klasses = matches.collect do |c|
113
+ begin
114
+ if c.first.respond_to? :constantize
115
+ c.first.constantize
116
+ else
117
+ eval(c.first)
118
+ end
119
+ rescue NameError
120
+ # $stderr.write "Could not load [#{c.first}] from [#{f}]\n"
121
+ nil
122
+ rescue SyntaxError
123
+ # $stderr.write "Could not load [#{c.first}] from [#{f}]\n"
124
+ nil
125
+ end
126
+ end
127
+ return klasses.select{|k| k.respond_to? 'suite'}
128
+ end
129
+
130
+ end
@@ -0,0 +1,52 @@
1
+ class PipeDream
2
+ def initialize
3
+ @child_read, @parent_write = IO.pipe
4
+ @parent_read, @child_write = IO.pipe
5
+ end
6
+
7
+ def gets
8
+ @reader.gets
9
+ end
10
+
11
+ def write(str)
12
+ @writer.write(str)
13
+ @writer.flush
14
+ str
15
+ end
16
+
17
+ def eof?
18
+ @reader.eof?
19
+ end
20
+
21
+ def identify_as_child
22
+ @parent_write.close
23
+ @parent_read.close
24
+ @reader = @child_read
25
+ @writer = @child_write
26
+ end
27
+
28
+ def identify_as_parent
29
+ @child_write.close
30
+ @child_read.close
31
+ @reader = @parent_read
32
+ @writer = @parent_write
33
+ end
34
+
35
+ def close
36
+ done_reading
37
+ done_writing
38
+ end
39
+
40
+ def done_writing
41
+ @writer.close unless @writer.closed?
42
+ end
43
+
44
+ def done_reading
45
+ @reader.close unless @reader.closed?
46
+ end
47
+
48
+ private
49
+ def force_identification
50
+ return "Must identify as child or parent" if @reader.nil? or @writer.nil?
51
+ end
52
+ end
@@ -0,0 +1,24 @@
1
+ class SafeFork
2
+ def self.fork
3
+ begin
4
+ # remove our connection so it doesn't get cloned
5
+ ActiveRecord::Base.remove_connection if defined?(ActiveRecord)
6
+ # fork a process
7
+ child = Process.fork do
8
+ begin
9
+ # create a new connection and perform the action
10
+ ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
11
+ yield
12
+ ensure
13
+ # make sure we remove the connection before we're done
14
+ ActiveRecord::Base.remove_connection if defined?(ActiveRecord)
15
+ end
16
+ end
17
+ ensure
18
+ # make sure we re-establish the connection before returning to the main instance
19
+ ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
20
+ end
21
+ return child
22
+ end
23
+ end
24
+
@@ -0,0 +1,31 @@
1
+ module Multitest::Tasks
2
+ desc "Test all your code in parallel"
3
+ task :multitest => ['multitest:units', 'multitest:functionals', 'multitest:integration']
4
+
5
+ namespace :multitest do
6
+ desc "Multi-core test:units"
7
+ task :units => [:environment] do
8
+ pattern = 'test/unit/**/*_test.rb'
9
+ files = Dir.glob(pattern)
10
+ $stderr.write "Running multitest:units\n"
11
+ Multitest.new(files).run
12
+ $stderr.write "Completed multitest:units\n\n"
13
+ end
14
+ desc "Multi-core test:functionals"
15
+ task :functionals => [:environment] do
16
+ pattern = 'test/functional/**/*_test.rb'
17
+ files = Dir.glob(pattern)
18
+ $stderr.write "Running multitest:functionals\n"
19
+ Multitest.new(files).run
20
+ $stderr.write "Completed multitest:functionals\n\n"
21
+ end
22
+ desc "Multi-core test:integration"
23
+ task :integration => [:environment] do
24
+ pattern = 'test/integration/**/*_test.rb'
25
+ files = Dir.glob(pattern)
26
+ $stderr.write "Running multitest:integration\n"
27
+ Multitest.new(files).run
28
+ $stderr.write "Completed multitest:integration\n\n"
29
+ end
30
+ end
31
+ end
data/multitest.gemspec ADDED
@@ -0,0 +1,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
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 = %q{multitest}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nick Gauthier"]
12
+ s.date = %q{2009-09-25}
13
+ s.description = %q{Run your tests across multiple cores automatically.}
14
+ s.email = %q{nick@smartlogicsolutions.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/multitest.rb",
27
+ "lib/multitest.rb",
28
+ "lib/multitest/multitest.rb",
29
+ "lib/multitest/multitest.rb",
30
+ "lib/multitest/pipe_dream.rb",
31
+ "lib/multitest/pipe_dream.rb",
32
+ "lib/multitest/safe_fork.rb",
33
+ "lib/multitest/safe_fork.rb",
34
+ "lib/multitest/tasks.rb",
35
+ "lib/multitest/tasks.rb",
36
+ "multitest.gemspec",
37
+ "test/multitest_test.rb",
38
+ "test/test_helper.rb",
39
+ "test/tests/another_test.rb",
40
+ "test/tests/sample_test.rb"
41
+ ]
42
+ s.homepage = %q{http://github.com/ngauthier/multitest}
43
+ s.rdoc_options = ["--charset=UTF-8"]
44
+ s.require_paths = ["lib"]
45
+ s.rubygems_version = %q{1.3.5}
46
+ s.summary = %q{Run your tests in parallel}
47
+ s.test_files = [
48
+ "test/test_helper.rb",
49
+ "test/multitest_test.rb",
50
+ "test/tests/another_test.rb",
51
+ "test/tests/sample_test.rb"
52
+ ]
53
+
54
+ if s.respond_to? :specification_version then
55
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
56
+ s.specification_version = 3
57
+
58
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
59
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
60
+ else
61
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
65
+ end
66
+ end
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+ require 'multitest'
3
+
4
+ class MultitestTest < Test::Unit::TestCase
5
+ context "a Multitest" do
6
+ setup do
7
+ FileUtils.mkdir_p(File.join(File.dirname(__FILE__), '..', 'tmp'))
8
+ FileUtils.rm_rf(File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp', 'test.log')))
9
+ @mt = Multitest.new([
10
+ File.expand_path(File.join(File.dirname(__FILE__), 'tests', 'sample_test.rb')),
11
+ File.expand_path(File.join(File.dirname(__FILE__), 'tests', 'another_test.rb'))
12
+ ])
13
+ end
14
+
15
+ should "run three instances" do
16
+ @mt.run
17
+ assert File.exists?(File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp', 'test.log')))
18
+ File.open(File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp', 'test.log'))) do |f|
19
+ assert_equal %w(1 1 3).sort, f.read.split("\n").sort
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+
8
+ class Test::Unit::TestCase
9
+ end
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class AnotherTest < Test::Unit::TestCase
4
+ should "output some text to a file" do
5
+ File.open(File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'test.log')), 'a') do |f|
6
+ f.write "3\n"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class SampleTest < Test::Unit::TestCase
4
+ should "output some text to a file" do
5
+ File.open(File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'test.log')), 'a') do |f|
6
+ f.write "1\n"
7
+ end
8
+ end
9
+ end
10
+
11
+ class SampleTestEnhanced < SampleTest
12
+ # will inherite the test above
13
+ end
14
+
15
+ class SampleTestHelper
16
+ def help
17
+ # do something
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multitest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Nick Gauthier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-25 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Run your tests across multiple cores automatically.
26
+ email: nick@smartlogicsolutions.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/multitest.rb
42
+ - lib/multitest/multitest.rb
43
+ - lib/multitest/pipe_dream.rb
44
+ - lib/multitest/safe_fork.rb
45
+ - lib/multitest/tasks.rb
46
+ - multitest.gemspec
47
+ - test/multitest_test.rb
48
+ - test/test_helper.rb
49
+ - test/tests/another_test.rb
50
+ - test/tests/sample_test.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/ngauthier/multitest
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --charset=UTF-8
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.3.5
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Run your tests in parallel
79
+ test_files:
80
+ - test/test_helper.rb
81
+ - test/multitest_test.rb
82
+ - test/tests/another_test.rb
83
+ - test/tests/sample_test.rb