-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_processor.rb
197 lines (154 loc) · 3.41 KB
/
file_processor.rb
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
require 'benchmark'
require 'csv'
DATAFILE_NAME = "../data/npidata_20050523-20140413.csv"
# def parse(csv, thread_index, num_threads)
# limit = (8000 / num_threads)
# csv.each_line.with_index do |line, line_index|
# if (line_index % num_threads - thread_index) == 0
# values = line.split(",")
# if limit == (8000 / num_threads) || limit == (8000 / num_threads - 1)
# print values
# print "\n"
# end
# if values.size != 329
# puts values.size
# print values
# print "\n"
# print line
# print "\n"
# end
# limit = limit - 1
# sleep (0.02)
# if limit < 0
# break
# end
# end
# end
# end
#
# ThreadedCSVProcessor class
#
class ThreadedCSVProcessor
def initialize(csv_name, num_threads)
@csv_name = csv_name
@number_of_threads = num_threads
@threads = Array.new
end
def iterate
create_threads
join_threads
end
private
def create_threads
@number_of_threads.times do |thread_number|
@threads[thread_number] = Thread.new { new_iterator(thread_number) }
end
end
def join_threads
@threads.each do |thread|
thread.join
end
end
def new_iterator(thread_number)
iterator = ThreadedCSVIterator.new(@csv_name, thread_number, @number_of_threads)
iterator.iterate_and_call(:geocode, :save)
end
end
#
# ThreadedCSVIterator class
#
class ThreadedCSVIterator
def initialize(csv_name, thread_number, number_of_threads)
@csv_name = csv_name
@thread_number = thread_number
@number_of_threads = number_of_threads
end
def iterate_and_call(*methods)
CSV.foreach(@csv_name) do |row|
@row_index = $.
process(row, methods) if row_assigned?
break if @row_index > 180
raise "Wrong Size" if row.size != 329
end
end
private
def process(row, methods)
rp = RowProcessor.new(row)
sleep 0.3
# puts "processing"
methods.each do |method|
rp.send(method)
end
end
def row_assigned?
@row_index % @number_of_threads - @thread_number == 0
end
end
#
# RowProcessor class
#
class RowProcessor
def initialize(row)
@row = Row.new(row)
end
def geocode
end
def save
end
end
#
# Row class
#
class Row
def initialize(row)
@values = row#.split(",")
puts @values[5]
if @values.size != 329
puts @values.size
print @values
print "\n"
print row
print "\n"
end
end
def geocode
end
def save
end
end
def benchmark(*thread_counts)
puts Process.pid
Benchmark.bm do |x|
thread_counts.each do |count|
x.report do
processor = ThreadedCSVProcessor.new(DATAFILE_NAME, count)
processor.iterate
end
end
end
end
def run(num)
puts Process.pid
processor = ThreadedCSVProcessor.new(DATAFILE_NAME, num)
processor.iterate
end
benchmark(10, 20, 30)
# run(20)
class Numeric
def to_rad
self * Math::PI / 180
end
end
# http://www.movable-type.co.uk/scripts/latlong.html
# loc1 and loc2 are arrays of [latitude, longitude]
def distance(loc1, loc2)
lat1, lon1 = loc1
lat2, lon2 = loc2
dLat = (lat2-lat1).to_rad;
dLon = (lon2-lon1).to_rad;
a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.to_rad) * Math.cos(lat2.to_rad) *
Math.sin(dLon/2) * Math.sin(dLon/2);
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
d = 3958.756 * c; # Multiply by 3958.756 to get Miles
end