type_balancer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rubocop.yml +96 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +5 -0
- data/Dockerfile +38 -0
- data/Gemfile +19 -0
- data/Gemfile.lock +101 -0
- data/LICENSE.txt +21 -0
- data/README.md +86 -0
- data/Rakefile +101 -0
- data/benchmark/README.md +90 -0
- data/benchmark/end_to_end_benchmark.rb +65 -0
- data/benchmark/quick_benchmark.rb +70 -0
- data/benchmark_output.log +1581 -0
- data/benchmark_results/ruby3.2.8.txt +55 -0
- data/benchmark_results/ruby3.2.8_yjit.txt +55 -0
- data/benchmark_results/ruby3.3.7.txt +55 -0
- data/benchmark_results/ruby3.3.7_yjit.txt +55 -0
- data/benchmark_results/ruby3.4.2.txt +55 -0
- data/benchmark_results/ruby3.4.2_yjit.txt +55 -0
- data/docs/benchmarks/README.md +180 -0
- data/examples/quality.rb +178 -0
- data/lib/type_balancer/alternating_filler.rb +46 -0
- data/lib/type_balancer/balancer.rb +126 -0
- data/lib/type_balancer/calculator.rb +218 -0
- data/lib/type_balancer/distribution_calculator.rb +21 -0
- data/lib/type_balancer/distributor.rb +61 -0
- data/lib/type_balancer/ordered_collection_manager.rb +95 -0
- data/lib/type_balancer/sequential_filler.rb +32 -0
- data/lib/type_balancer/version.rb +5 -0
- data/lib/type_balancer.rb +44 -0
- data/sig/type_balancer.rbs +85 -0
- data/type_balancer.gemspec +33 -0
- metadata +77 -0
@@ -0,0 +1,70 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'benchmark'
|
6
|
+
require_relative '../lib/type_balancer'
|
7
|
+
|
8
|
+
# Test cases of increasing size with different ratios
|
9
|
+
TEST_CASES = [
|
10
|
+
{ name: 'Tiny', total: 100, ratio: 0.1 },
|
11
|
+
{ name: 'Small', total: 1_000, ratio: 0.2 },
|
12
|
+
{ name: 'Medium', total: 10_000, ratio: 0.3 },
|
13
|
+
{ name: 'Large', total: 100_000, ratio: 0.5 }
|
14
|
+
].freeze
|
15
|
+
|
16
|
+
# Print Ruby and YJIT information
|
17
|
+
puts "Ruby version: #{RUBY_VERSION}"
|
18
|
+
puts "YJIT enabled: #{RubyVM::YJIT.enabled? rescue false}"
|
19
|
+
puts
|
20
|
+
|
21
|
+
# Generate available items for each test case
|
22
|
+
TEST_CASES.each do |test|
|
23
|
+
# Create available items as 20% more than needed
|
24
|
+
target = (test[:total] * test[:ratio]).round
|
25
|
+
available = (target * 1.2).round
|
26
|
+
test[:available] = Array.new(available) { |i| { id: i, type: 'video', title: "Item #{i}" } }
|
27
|
+
end
|
28
|
+
|
29
|
+
def run_benchmark(test_case)
|
30
|
+
puts "\nRunning benchmark for #{test_case[:name]} dataset:"
|
31
|
+
puts "- Total count: #{test_case[:total]}"
|
32
|
+
puts "- Available items: #{test_case[:available].size}"
|
33
|
+
puts "- Ratio: #{test_case[:ratio]}"
|
34
|
+
puts
|
35
|
+
|
36
|
+
# Adjust iterations based on dataset size
|
37
|
+
iterations = case test_case[:name]
|
38
|
+
when 'Tiny' then 100_000
|
39
|
+
when 'Small' then 10_000
|
40
|
+
when 'Medium' then 1_000
|
41
|
+
when 'Large' then 100
|
42
|
+
end
|
43
|
+
|
44
|
+
# Warmup phase
|
45
|
+
puts "Warming up..."
|
46
|
+
warmup_iterations = iterations / 10
|
47
|
+
warmup_iterations.times do
|
48
|
+
TypeBalancer.balance(test_case[:available], type_field: :type)
|
49
|
+
end
|
50
|
+
puts "Warmup complete."
|
51
|
+
puts
|
52
|
+
|
53
|
+
# Run benchmark
|
54
|
+
result = Benchmark.bm(20) do |x|
|
55
|
+
x.report("Ruby:") do
|
56
|
+
iterations.times do
|
57
|
+
TypeBalancer.balance(test_case[:available], type_field: :type)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Calculate and display operations per second
|
63
|
+
ops_per_second = iterations / result[0].real
|
64
|
+
puts "\nOperations per second: #{ops_per_second.round(2)}"
|
65
|
+
end
|
66
|
+
|
67
|
+
# Run benchmarks for each test case
|
68
|
+
TEST_CASES.each do |test_case|
|
69
|
+
run_benchmark(test_case)
|
70
|
+
end
|