Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[potashin] optimization 5 #103

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.1
3.3.2
24 changes: 24 additions & 0 deletions client.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
https://localhost:9292/b?value=1
https://localhost:9292/a?value=11
https://localhost:9292/a?value=12
https://localhost:9292/a?value=13
https://localhost:9292/b?value=2
https://localhost:9292/a?value=21
https://localhost:9292/a?value=22
https://localhost:9292/a?value=23
https://localhost:9292/b?value=3
https://localhost:9292/a?value=31
https://localhost:9292/a?value=32
https://localhost:9292/a?value=33
AB1 = 6512bd43d9caa6e02c990b0a82652dca-c20ad4d76fe97759aa27a0c99bff6710-c51ce410c124a10e0db5e4b97fc2af39-6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b
https://localhost:9292/c?value=6512bd43d9caa6e02c990b0a82652dca-c20ad4d76fe97759aa27a0c99bff6710-c51ce410c124a10e0db5e4b97fc2af39-6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b
C1 = ee54918569d9620a4947a184c097ab1a90b33db6b8a009e01cbbe7238f800ecf846068a36ac7ae289a0796face8b471dcfd7e7d55f9081bccd8f2f50c1d4f888
AB2 = 37693cfc748049e45d87b8c7d8b9aacd-3c59dc048e8850243be8079a5c74d079-b6d767d2f8ed5d21a44b0e5886680cb9-d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35
https://localhost:9292/c?value=37693cfc748049e45d87b8c7d8b9aacd-3c59dc048e8850243be8079a5c74d079-b6d767d2f8ed5d21a44b0e5886680cb9-d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35
C2 = 4bc5a33ec7c74626144b5277e887501074b07819ecd9cc506b08e765be1f26a3d7c9fbb59bde46379ca7f0ebf2ef066f38c0a4652d4017a635fc8bdddc070557
AB3 = 182be0c5cdcd5072bb1864cdee4d3d6e-6364d3f0f495b6ab9dcf8d3b5c6e0b01-c16a5320fa475530d9583c34fd356ef5-4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce
https://localhost:9292/c?value=182be0c5cdcd5072bb1864cdee4d3d6e-6364d3f0f495b6ab9dcf8d3b5c6e0b01-c16a5320fa475530d9583c34fd356ef5-4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce
C3 = 35bf8dfb6c327259fd05a56a6d47ecfc21965b0f4ff8926b164c32e31cc91d825da1a73bc145cdec04dfbb0862fb14b687f15a8233be15092c4c85da4b7bf94a
https://localhost:9292/a?value=35bf8dfb6c327259fd05a56a6d47ecfc21965b0f4ff8926b164c32e31cc91d825da1a73bc145cdec04dfbb0862fb14b687f15a8233be15092c4c85da4b7bf94a-4bc5a33ec7c74626144b5277e887501074b07819ecd9cc506b08e765be1f26a3d7c9fbb59bde46379ca7f0ebf2ef066f38c0a4652d4017a635fc8bdddc070557-ee54918569d9620a4947a184c097ab1a90b33db6b8a009e01cbbe7238f800ecf846068a36ac7ae289a0796face8b471dcfd7e7d55f9081bccd8f2f50c1d4f888
FINISHED in 6.078406s.
RESULT = 0bbe9ecf251ef4131dd43e1600742cfb
57 changes: 31 additions & 26 deletions client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require 'openssl'
require 'faraday'
require 'async'
require 'async/semaphore'
require 'async/barrier'

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

Expand Down Expand Up @@ -38,41 +41,43 @@ def collect_sorted(arr)

start = Time.now

a11 = a(11)
a12 = a(12)
a13 = a(13)
b1 = b(1)
@a = Hash.new { |h, k| h[k] = [] }
@b = {}
@c = {}

ab1 = "#{collect_sorted([a11, a12, a13])}-#{b1}"
puts "AB1 = #{ab1}"
Async do
semaphores = { a: 3, b: 2, c: 1 }.transform_values { |value| Async::Semaphore.new(value) }
barriers = Hash.new { |h, k| h[k] = Hash.new { |hh, kk| hh[kk] = Async::Barrier.new } }

c1 = c(ab1)
puts "C1 = #{c1}"
{1 => [11, 12, 13], 2 => [21, 22, 23], 3 => [31, 32, 33]}.each do |index, batch|
semaphores[:b].async(parent: barriers[:b][index]) do
@b[index] = b(index)
end

a21 = a(21)
a22 = a(22)
a23 = a(23)
b2 = b(2)
batch.each do |value|
semaphores[:a].async(parent: barriers[:a][index]) do
@a[index] << a(value)
end
end
end

ab2 = "#{collect_sorted([a21, a22, a23])}-#{b2}"
puts "AB2 = #{ab2}"
[1, 2, 3].each do |index|
semaphores[:c].async do
barriers[:a][index].wait
barriers[:b][index].wait

c2 = c(ab2)
puts "C2 = #{c2}"
ab_value = "#{collect_sorted(@a[index])}-#{@b[index]}"

a31 = a(31)
a32 = a(32)
a33 = a(33)
b3 = b(3)
puts "AB#{index} = #{ab_value}"

ab3 = "#{collect_sorted([a31, a32, a33])}-#{b3}"
puts "AB3 = #{ab3}"
@c[index] = c(ab_value)

c3 = c(ab3)
puts "C3 = #{c3}"
puts "C#{index} = #{@c[index]}"
end
end
end

c123 = collect_sorted([c1, c2, c3])
result = a(c123)
result = a(collect_sorted(@c.values))

puts "FINISHED in #{Time.now - start}s."
puts "RESULT = #{result}" # 0bbe9ecf251ef4131dd43e1600742cfb