Skip to content

Commit d20c207

Browse files
authored
Merge pull request #368 from grosser/grosser/speed
speed up tests
2 parents f9c570b + a55c3bc commit d20c207

9 files changed

Lines changed: 30 additions & 15 deletions

spec/cases/count_open_pipes.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
# frozen_string_literal: true
22
require './spec/cases/helper'
3-
count = ->(*) { `lsof -l | grep pipe | wc -l`.to_i }
3+
count = ->(*) { Dir.children("/dev/fd").size }
4+
5+
if ENV["SELF_TEST"]
6+
# verify that count detects newly opened pipes
7+
create = 5
8+
before = count.call
9+
pipes = create.times.map { IO.pipe }
10+
after = count.call
11+
expected = before + (create * 2)
12+
raise "expected #{expected} fds but got #{after}" unless after == expected
13+
pipes.each do |r, w|
14+
r.close
15+
w.close
16+
end
17+
end
18+
419
start = count.call
520
results = Parallel.map(Array.new(20), in_processes: 20, &count)
621
puts results.max - start

spec/cases/helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def process_diff
1010

1111
yield
1212

13-
sleep 1
13+
sleep 0.5
1414

1515
processes_after = `#{cmd}`.to_i
1616

spec/cases/parallel_high_fork_rate.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
require './spec/cases/helper'
33

44
Parallel.each((0..200).to_a, in_processes: 200) do |_x|
5-
sleep 1
5+
sleep 0.1
66
end
77
print 'OK'

spec/cases/parallel_map_uneven.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
require './spec/cases/helper'
33

44
Parallel.map([1, 2, 1, 2]) do |x|
5-
sleep 2 if x == 1
5+
sleep 1 if x == 1
66
end

spec/cases/parallel_sleeping_2.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
require './spec/cases/helper'
33

44
Parallel.in_processes(5) do
5-
sleep 2
5+
sleep 1
66
end

spec/cases/progress.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
require './spec/cases/helper'
33

44
title = (ENV["TITLE"] == "true" ? true : "Doing stuff")
5-
Parallel.map(1..50, progress: title) do
5+
Parallel.map(1..10, progress: title) do
66
sleep 1 if $stdout.tty? # for debugging
77
end

spec/cases/progress_with_finish.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
sum = 0
55
finish = ->(_item, _index, result) { sum += result }
66

7-
Parallel.map(1..50, progress: "Doing stuff", finish: finish) do
7+
Parallel.map(1..10, progress: "Doing stuff", finish: finish) do
88
sleep 1 if $stdout.tty? # for debugging
99
2
1010
end

spec/cases/with_worker_number.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
method = ENV.fetch('METHOD')
55
in_worker_type = :"in_#{ENV.fetch('WORKER_TYPE')}"
66

7-
Parallel.public_send(method, 1..100, in_worker_type => 4) do
8-
sleep 0.1 # so all workers get started
7+
Parallel.public_send(method, 1..20, in_worker_type => 4) do
8+
sleep 0.02 # so all workers get started
99
print Parallel.worker_number
1010
end

spec/parallel_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def cpus
161161
it "saves time" do
162162
time_taken do
163163
ruby("spec/cases/parallel_sleeping_2.rb")
164-
end.should < 3.5
164+
end.should < 2.5
165165
end
166166

167167
it "raises when one of the processes raises" do
@@ -203,12 +203,12 @@ def cpus
203203
describe ".in_threads" do
204204
it "saves time" do
205205
time_taken do
206-
Parallel.in_threads(3) { sleep 2 }
207-
end.should < 3
206+
Parallel.in_threads(3) { sleep 1 }
207+
end.should < 2
208208
end
209209

210210
it "does not create new processes" do
211-
-> { Thread.new { Parallel.in_threads(2) { sleep 1 } } }.should_not(change { `ps`.split("\n").size })
211+
-> { Thread.new { Parallel.in_threads(2) { sleep 0.5 } } }.should_not(change { `ps`.split("\n").size })
212212
end
213213

214214
it "returns results as array" do
@@ -245,7 +245,7 @@ def cpus
245245
it "starts new process immediately when old exists" do
246246
time_taken do
247247
ruby("spec/cases/parallel_map_uneven.rb")
248-
end.should <= 3.5
248+
end.should <= 2.5
249249
end
250250

251251
it "does not flatten results" do
@@ -661,7 +661,7 @@ def cpus
661661
ruby("spec/cases/progress_with_finish.rb 2>&1").strip.sub(/=+/, '==').gsub(
662662
/\n+/,
663663
"\n"
664-
).should == "Doing stuff: |==|\n100"
664+
).should == "Doing stuff: |==|\n20"
665665
end
666666

667667
it "takes the title from :progress[:title] and passes options along" do

0 commit comments

Comments
 (0)