forked from actions-im/ruby_path
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
81 lines (66 loc) · 2.34 KB
/
Rakefile
File metadata and controls
81 lines (66 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require File.join(File.dirname(__FILE__),'/lib/ruby_path.rb')
require 'json'
require 'benchmark'
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
namespace :spec do
RSpec::Core::RakeTask.new(:unit) do |spec|
spec.pattern = 'spec/unit/*_spec.rb'
spec.rspec_opts = ['--backtrace']
end
end
task :default => 'spec:unit'
namespace :benchmarking do
task :path do
iterations=1000000
path="$.groups[?(@['code']==123)].benefits[?(@['code']=='401k')].funds[?(@['target_year']>$lower_year && @['target_year']<=$higher_year)].name"
plans=[]
PathCache.clear
10.times{
plans<<JSON.parse(open(File.join(File.dirname(__FILE__),'/spec/fixtures/benefits.json')){ |f| f.read })
}
Benchmark.bm{ |bm|
bm.report ('By Path') {
iterations.times{
year=Random.rand(10000)
plans[Random.rand(10)].path(path, {lower_year:year, higher_year:10000-year})
}
}
bm.report('Native'){
iterations.times{
year=Random.rand(10000)
benefit=plans[Random.rand(10)]['groups'].select{|b| b['code']==123}.first['benefits'].select{|b| b['code']=='401k'}.first
funds=benefit['funds'].select{|el| (el['target_year']>year && el['target_year']<=(10000-year))}
funds.flat_map{|el| el['name']}
}
}
}
end
task :proc do
iterations=1000
plans=[]
10.times{
plans<<JSON.parse(open(File.join(Rails.root.join('public','plans') , "jetblue.json")){ |f| f.read })
}
Benchmark.bm{|bm|
bm.report('By Proc'){
iterations.times{
year=Random.rand(10000)
lambda{|plan|
benefit=plan['groups'].select{|b| b['code']==123}.first['benefits'].select{|b| b['code']=='401k'}.first
funds=benefit['funds'].select{|el| (el['target_year']>year && el['target_year']<=(10000-year))}
funds.flat_map{|el| el['name']}
}[plans[Random.rand(10)]]
}
}
bm.report('Native'){
iterations.times{
year=Random.rand(10000)
benefit=plans[Random.rand(10)]['groups'].select{|b| b['code']==123}.first['benefits'].select{|b| b['code']=='401k'}.first
funds=benefit['funds'].select{|el| (el['target_year']>year && el['target_year']<=(10000-year))}
funds.flat_map{|el| el['name']}
}
}
}
end
end