forked from PigAndChicken/WiKey_API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.rb
More file actions
47 lines (38 loc) · 1.27 KB
/
Copy pathtest2.rb
File metadata and controls
47 lines (38 loc) · 1.27 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
def syn(inputs)
inputs.map do |input|
build_entity(input,get_raw_data(input))
end
end
def concurrent(inputs)
m = inputs.size
n = 1
promises = inputs.map do |input|
Concurrent::Promise.new { get_raw_data(input) }.then {|raw_data| build_entity(input,raw_data)}.then{|article| store(article)}.rescue {{error: "#{input[:topic]} not found."}}
end
promises.map do |promise|
puts n/m.to_f
n = n + 1
promise.execute.value
end
end
def concurrent(inputs)
inputs.map do |input|
Concurrent::Promise.new { get_raw_data(input) }.then {|raw_data| build_entity(input,raw_data)}.rescue {{error: "#{input} not found."}}
end.map(&:execute).map(&:value)
end
def store(article)
WiKey::Repository::Article.create(article)
end
def build_entity(input, raw_data)
WiKey::Wiki::ArticleMapper.new(input[:gateway]).build_entity(raw_data)
end
def get_raw_data(input)
raw_data = input[:gateway].article_data(input[:topic])
key = raw_data['query']['pages'].keys[0]
raw_data['query']['pages'][key]
end
Benchmark.bm(10) do |bench|
bench.report('sync:') { syn(inputs) }
bench.report('concurrent:') { concurrent(inputs) }
end
sqs = Aws::SQS::Client.new(access_key_id: config.AWS_ACCESS_KEY_ID, secret_access_key: config.AWS_SECRET_ACCESS_KEY, region: config.AWS_REGION)