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.
@@ -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