From a07cfb70e9ab7e24d2888ece27187b37f44c6065 Mon Sep 17 00:00:00 2001 From: potashin Date: Sat, 1 Jun 2024 13:25:39 +0300 Subject: [PATCH 1/4] bump: ruby version --- .ruby-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ruby-version b/.ruby-version index bea438e..4772543 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -3.3.1 +3.3.2 From af9e8cde82fa72fd0a6375238014d344d46e5652 Mon Sep 17 00:00:00 2001 From: potashin Date: Sun, 2 Jun 2024 01:05:01 +0300 Subject: [PATCH 2/4] chore: using scheduler --- Gemfile | 1 + Gemfile.lock | 6 ++++ client.rb | 83 +++++++++++++++++++++++++++++++++------------------- 3 files changed, 60 insertions(+), 30 deletions(-) diff --git a/Gemfile b/Gemfile index 0b8e97a..f51d2e0 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,4 @@ gem "async" gem "falcon", "0.44.0" # use different version on your own risk; 0.44.0 worked gem "sinatra" gem "faraday" +gem "pry" diff --git a/Gemfile.lock b/Gemfile.lock index 4090b46..b3cddb7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -26,6 +26,7 @@ GEM async async-container (~> 0.16) base64 (0.2.0) + coderay (1.1.3) console (1.25.2) fiber-annotation fiber-local (~> 1.1) @@ -54,6 +55,7 @@ GEM json (2.7.2) localhost (1.3.1) mapping (1.1.1) + method_source (1.1.0) mustermann (3.0.0) ruby2_keywords (~> 0.0.1) net-http (0.4.1) @@ -73,6 +75,9 @@ GEM protocol-rack (0.5.1) protocol-http (~> 0.23) rack (>= 1.0) + pry (0.14.2) + coderay (~> 1.1) + method_source (~> 1.0) rack (3.0.11) rack-protection (4.0.0) base64 (>= 0.1.0) @@ -102,6 +107,7 @@ DEPENDENCIES async falcon (= 0.44.0) faraday + pry sinatra BUNDLED WITH diff --git a/client.rb b/client.rb index e001ba3..54f8b17 100644 --- a/client.rb +++ b/client.rb @@ -1,5 +1,6 @@ require 'openssl' require 'faraday' +require 'async' OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE @@ -36,43 +37,65 @@ def collect_sorted(arr) arr.sort.join('-') end -start = Time.now - -a11 = a(11) -a12 = a(12) -a13 = a(13) -b1 = b(1) - -ab1 = "#{collect_sorted([a11, a12, a13])}-#{b1}" -puts "AB1 = #{ab1}" +def async_method_calls(items = []) + Async do + items.each do |(method_name, buf, *values)| + values.each do |value| + Fiber.schedule do + buf[value] = send(method_name, value) + end + end + end + end.wait +end -c1 = c(ab1) -puts "C1 = #{c1}" +def c_via_ab(value) + ab_value = "#{collect_sorted(@a[value].values)}-#{@b[value]}" -a21 = a(21) -a22 = a(22) -a23 = a(23) -b2 = b(2) + puts "AB#{value} = #{ab_value}" -ab2 = "#{collect_sorted([a21, a22, a23])}-#{b2}" -puts "AB2 = #{ab2}" + c_value = c(ab_value) -c2 = c(ab2) -puts "C2 = #{c2}" + puts "C#{value} = #{c_value}" -a31 = a(31) -a32 = a(32) -a33 = a(33) -b3 = b(3) + c_value +end -ab3 = "#{collect_sorted([a31, a32, a33])}-#{b3}" -puts "AB3 = #{ab3}" +def result(value = nil) + a(collect_sorted(@c.values_at(1, 2, 3))) +end -c3 = c(ab3) -puts "C3 = #{c3}" +start = Time.now -c123 = collect_sorted([c1, c2, c3]) -result = a(c123) +@a = Hash.new { |h, k| h[k] = {} } +@b = {} +@c = {} + +instructions = [ + [ + [:a, @a[1], 11, 12, 13], + [:b, @b, 1, 2], + ], + [ + [:a, @a[2], 21, 22, 23], + [:b, @b, 3], + [:c_via_ab, @c, 1], + ], + [ + [:c_via_ab, @c, 2], + [:a, @a[3], 31, 32, 33] + ], + [ + [:c_via_ab, @c, 3], + ], + [ + [:result, @a, :result] + ] +] + +instructions.each do |instruction| + async_method_calls(instruction) +end puts "FINISHED in #{Time.now - start}s." -puts "RESULT = #{result}" # 0bbe9ecf251ef4131dd43e1600742cfb +puts "RESULT = #{@a[:result]}" # 0bbe9ecf251ef4131dd43e1600742cfb From cd8b134748f176f5d696b70028ec217491de080d Mon Sep 17 00:00:00 2001 From: potashin Date: Sun, 2 Jun 2024 01:38:51 +0300 Subject: [PATCH 3/4] chore: using barriers & semaphores --- client.log | 24 ++++++++++++++++ client.rb | 82 +++++++++++++++++++++--------------------------------- 2 files changed, 56 insertions(+), 50 deletions(-) create mode 100644 client.log diff --git a/client.log b/client.log new file mode 100644 index 0000000..ab5aa56 --- /dev/null +++ b/client.log @@ -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 \ No newline at end of file diff --git a/client.rb b/client.rb index 54f8b17..38d87e5 100644 --- a/client.rb +++ b/client.rb @@ -1,6 +1,8 @@ require 'openssl' require 'faraday' require 'async' +require 'async/semaphore' +require 'async/barrier' OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE @@ -37,65 +39,45 @@ def collect_sorted(arr) arr.sort.join('-') end -def async_method_calls(items = []) - Async do - items.each do |(method_name, buf, *values)| - values.each do |value| - Fiber.schedule do - buf[value] = send(method_name, value) - end - end - end - end.wait -end +start = Time.now -def c_via_ab(value) - ab_value = "#{collect_sorted(@a[value].values)}-#{@b[value]}" +@a = Hash.new { |h, k| h[k] = [] } +@b = {} +@c = {} - puts "AB#{value} = #{ab_value}" +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 } } - c_value = c(ab_value) + {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 - puts "C#{value} = #{c_value}" + batch.each do |value| + semaphores[:a].async(parent: barriers[:a][index]) do + @a[index] << a(value) + end + end + end - c_value -end + [1, 2, 3].each do |index| + semaphores[:c].async do + barriers[:a][index].wait + barriers[:b][index].wait -def result(value = nil) - a(collect_sorted(@c.values_at(1, 2, 3))) -end + ab_value = "#{collect_sorted(@a[index])}-#{@b[index]}" -start = Time.now + puts "AB#{index} = #{ab_value}" -@a = Hash.new { |h, k| h[k] = {} } -@b = {} -@c = {} + @c[index] = c(ab_value) -instructions = [ - [ - [:a, @a[1], 11, 12, 13], - [:b, @b, 1, 2], - ], - [ - [:a, @a[2], 21, 22, 23], - [:b, @b, 3], - [:c_via_ab, @c, 1], - ], - [ - [:c_via_ab, @c, 2], - [:a, @a[3], 31, 32, 33] - ], - [ - [:c_via_ab, @c, 3], - ], - [ - [:result, @a, :result] - ] -] - -instructions.each do |instruction| - async_method_calls(instruction) + puts "C#{index} = #{@c[index]}" + end + end end +result = a(collect_sorted(@c.values)) + puts "FINISHED in #{Time.now - start}s." -puts "RESULT = #{@a[:result]}" # 0bbe9ecf251ef4131dd43e1600742cfb +puts "RESULT = #{result}" # 0bbe9ecf251ef4131dd43e1600742cfb From 2b9b5ce462d207a1a401a70a7ce71f3b9f35cf5a Mon Sep 17 00:00:00 2001 From: potashin Date: Sun, 2 Jun 2024 01:40:07 +0300 Subject: [PATCH 4/4] chore: remove pry --- Gemfile | 1 - Gemfile.lock | 6 ------ 2 files changed, 7 deletions(-) diff --git a/Gemfile b/Gemfile index f51d2e0..0b8e97a 100644 --- a/Gemfile +++ b/Gemfile @@ -4,4 +4,3 @@ gem "async" gem "falcon", "0.44.0" # use different version on your own risk; 0.44.0 worked gem "sinatra" gem "faraday" -gem "pry" diff --git a/Gemfile.lock b/Gemfile.lock index b3cddb7..4090b46 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -26,7 +26,6 @@ GEM async async-container (~> 0.16) base64 (0.2.0) - coderay (1.1.3) console (1.25.2) fiber-annotation fiber-local (~> 1.1) @@ -55,7 +54,6 @@ GEM json (2.7.2) localhost (1.3.1) mapping (1.1.1) - method_source (1.1.0) mustermann (3.0.0) ruby2_keywords (~> 0.0.1) net-http (0.4.1) @@ -75,9 +73,6 @@ GEM protocol-rack (0.5.1) protocol-http (~> 0.23) rack (>= 1.0) - pry (0.14.2) - coderay (~> 1.1) - method_source (~> 1.0) rack (3.0.11) rack-protection (4.0.0) base64 (>= 0.1.0) @@ -107,7 +102,6 @@ DEPENDENCIES async falcon (= 0.44.0) faraday - pry sinatra BUNDLED WITH