-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
135 lines (113 loc) · 3.54 KB
/
Rakefile
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
require File.join(File.dirname(__FILE__),'/lib/ruby_path.rb')
require 'oj'
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 :generate_file do
number_of_samples=400000
content=open(File.join(File.dirname(__FILE__),'/spec/fixtures/benefits.json')){ |f| f.read }
open(File.join(File.dirname(__FILE__),'/spec/fixtures/large_file.json'), 'w'){|f|
f.write "["
number_of_samples.times{|tick|
s=content.gsub('"code": 123', "\"code\": #{tick+1}")
f.write s + (tick<(number_of_samples-1) ? ',' : "" )
}
f.write "]"
}
end
task :pathvsdirect do
iterations=1000000
puts Time.now
plans=Oj.load(open(File.join(File.dirname(__FILE__),'/spec/fixtures/large_file.json')){ |f| f.read })
p Time.now
#p "Loading complete - #{Time.now}"
#res=plans[1].compile_path(".groups[?(@['code']==$code)].benefits[?(@['code']=='401k')].funds[?(@['target_year']>=$lower_bound && @['target_year']<$upper_bound)].name")
GC::Profiler.enable
def path_selector(plan, r, upper_bound, lower_bound)
plan['groups'].select{|group| group['code']==r+1}
.flat_map{|group| group['benefits']}
.select{|benefit| benefit['code']=='401k'}
.flat_map{|benefit| benefit['funds']}.compact
.select{|funds_to_analyze| funds_to_analyze['target_year']>=upper_bound && funds_to_analyze['target_year']<lower_bound }
.map{|fund| fund['name']}
end
Benchmark.bmbm{|bm|
bm.report('path'){
iterations.times{
r=Random.rand(plans.length)
res=plans[r].find_all_by_path(".groups[?(@['code']==$code)].benefits[?(@['code']=='401k')].funds[?(@['target_year']>=$lower_bound && @['target_year']<$upper_bound)].name",{code:r+1,lower_bound: 2030,upper_bound: 2060})
#p res
}
#GC::Profiler.disable
puts "\n#{GC::Profiler.result}"
}
bm.report('direct'){
#GC::Profiler.enable
iterations.times{
#group=plans[Random.rand(10)]['groups'].select{|b| b['code']==123}.first
r=Random.rand(plans.length)
path_selector(plans[r], r, 2030, 2060)
}
puts "\n#{GC::Profiler.result}"
}
}
GC::Profiler.disable
end
task :whilevsfor do
iterations=10000#00
objects=[]
iterations.times{objects<<Random.rand(iterations)}
Benchmark.bm{|bm|
bm.report('for'){
iterations.times{
for x in objects do
x*2
end
}
}
bm.report('while'){
iterations.times{
index=0
length=objects.length
while index<length do
x=objects[index]
index+=1
x*2
end
}
}
}
end
task :max_by_vs_while do
iterations=10000
objects=[]
iterations.times{objects<<Random.rand(iterations)}
Benchmark.bmbm{|bm|
bm.report('max_by'){
iterations.times{
my_max=objects.max_by{|x| -1*x}
}
}
bm.report('while'){
iterations.times{
index=0
length=objects.length
my_max=objects[0]
while index<length do
x=objects[index]
index+=1
my_max=x if (-1*x > -1*my_max)
end
}
}
}
end
end