Skip to content

Commit 40af3f3

Browse files
committed
Use posargs
1 parent 17927ff commit 40af3f3

1 file changed

Lines changed: 104 additions & 109 deletions

File tree

bench/various_appraches_bench.rb

Lines changed: 104 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -77,29 +77,25 @@
7777
:owner_id,
7878
:created_at,
7979
:updated_at,
80-
:original_type,
81-
keyword_init: true,
80+
:original_type
8281
)
8382

8483
SellingPlanPriceAdjustment = Struct.new(
8584
:order_count,
8685
:position,
8786
:value_type,
88-
:value,
89-
keyword_init: true,
87+
:value
9088
)
9189

9290
SellingPlanOption = Struct.new(
9391
:name,
9492
:position,
95-
:value,
96-
keyword_init: true,
93+
:value
9794
)
9895

9996
SellingPlanCheckoutCharge = Struct.new(
10097
:value_type,
101-
:value,
102-
keyword_init: true,
98+
:value
10399
)
104100

105101
# SellingPlan is SHARED - the same selling plan instance is referenced by
@@ -111,16 +107,14 @@
111107
:recurring_deliveries,
112108
:options,
113109
:price_adjustments,
114-
:checkout_charge,
115-
keyword_init: true,
110+
:checkout_charge
116111
)
117112

118113
SellingPlanGroup = Struct.new(
119114
:id,
120115
:name,
121116
:options,
122-
:selling_plans,
123-
keyword_init: true,
117+
:selling_plans
124118
)
125119

126120
# ProductOptionValue is SHARED - the same option value instance appears in
@@ -129,16 +123,14 @@
129123
:id,
130124
:name,
131125
:position,
132-
:swatch_color,
133-
keyword_init: true,
126+
:swatch_color
134127
)
135128

136129
ProductOption = Struct.new(
137130
:id,
138131
:name,
139132
:position,
140-
:values,
141-
keyword_init: true,
133+
:values
142134
)
143135

144136
# ProductVariant - matches real ProductLoader::Messages::ProductVariant (37 fields)
@@ -172,8 +164,7 @@
172164
:requires_shipping,
173165
:selling_plans, # References SHARED SellingPlan objects
174166
:metafields,
175-
:variant_unit_price_measurement,
176-
keyword_init: true,
167+
:variant_unit_price_measurement
177168
)
178169

179170
# Product - matches real ProductLoader::Messages::Product (28 fields)
@@ -196,8 +187,7 @@
196187
:variants,
197188
:options, # Contains SHARED ProductOptionValue objects
198189
:selling_plan_groups, # Contains SHARED SellingPlan objects
199-
:metafields,
200-
keyword_init: true,
190+
:metafields
201191
)
202192

203193
ALL_STRUCTS = [
@@ -254,7 +244,7 @@ def self.build_untracked_packer(struct)
254244
end
255245

256246
def self.build_tracked_unpacker(struct)
257-
args = struct.members.map { |m| "#{m}: unpacker.read" }.join(", ")
247+
args = struct.members.map { |_m| "unpacker.read" }.join(", ")
258248

259249
eval(<<~RUBY, binding, __FILE__, __LINE__ + 1)
260250
->(unpacker) {
@@ -273,7 +263,7 @@ def self.build_tracked_unpacker(struct)
273263
end
274264

275265
def self.build_untracked_unpacker(struct)
276-
args = struct.members.map { |m| "#{m}: unpacker.read" }.join(", ")
266+
args = struct.members.map { |_m| "unpacker.read" }.join(", ")
277267
eval(<<~RUBY, binding, __FILE__, __LINE__ + 1)
278268
->(unpacker) {
279269
#{struct}.new(#{args})
@@ -340,8 +330,8 @@ def self.build_factory
340330
ALL_STRUCTS.each do |struct|
341331
unpacker = eval(<<~RUBY, binding, __FILE__, __LINE__ + 1)
342332
-> (unpacker) {
343-
#{struct.members.join(", ")} = unpacker.read
344-
#{struct}.new(#{struct.members.map{ |m| "#{m}: #{m}" }.join(", ")})
333+
#{struct.members.join(",")} = unpacker.read
334+
#{struct}.new(#{struct.members.join(", ")})
345335
}
346336
RUBY
347337

@@ -510,26 +500,26 @@ def self.description
510500

511501
def create_selling_plan(id:)
512502
SellingPlan.new(
513-
id: id,
514-
name: "Subscribe & Save #{id}",
515-
description: "Save 10% with a subscription",
516-
recurring_deliveries: true,
517-
options: [
518-
SellingPlanOption.new(name: "Delivery Frequency", position: 1, value: "1 Month"),
503+
id,
504+
"Subscribe & Save #{id}",
505+
"Save 10% with a subscription",
506+
true,
507+
[
508+
SellingPlanOption.new("Delivery Frequency", 1, "1 Month")
519509
],
520-
price_adjustments: [
521-
SellingPlanPriceAdjustment.new(order_count: nil, position: 1, value_type: "percentage", value: 10),
510+
[
511+
SellingPlanPriceAdjustment.new(nil, 1, "percentage", 10)
522512
],
523-
checkout_charge: SellingPlanCheckoutCharge.new(value_type: "percentage", value: 100),
513+
SellingPlanCheckoutCharge.new("percentage", 100)
524514
)
525515
end
526516

527517
def create_selling_plan_group(id:, selling_plans:)
528518
SellingPlanGroup.new(
529-
id: id,
530-
name: "Subscription Group #{id}",
531-
options: [{ name: "Delivery Frequency", position: 1, values: ["1 Month", "2 Months"] }],
532-
selling_plans: selling_plans,
519+
id,
520+
"Subscription Group #{id}",
521+
[{ name: "Delivery Frequency", position: 1, values: ["1 Month", "2 Months"] }],
522+
selling_plans
533523
)
534524
end
535525

@@ -540,41 +530,42 @@ def create_metafields(owner_id:, count:, owner_type:)
540530
# - Relatively short values
541531
(1..count).map do |i|
542532
Metafield.new(
543-
id: owner_id * 1000 + i,
544-
namespace: "custom",
545-
key: "field_#{i}",
546-
value: "Value #{i}",
547-
type: "single_line_text_field", # this should be an enum
548-
value_type: "string",
549-
definition_id: nil,
550-
owner_type: owner_type,
551-
owner_id: owner_id,
552-
created_at: Time.now,
553-
updated_at: Time.now,
554-
original_type: nil,
533+
owner_id * 1000 + i,
534+
"custom",
535+
"field_#{i}",
536+
"Value #{i}",
537+
"single_line_text_field", # this should be an enum
538+
"string",
539+
nil,
540+
owner_type,
541+
owner_id,
542+
Time.now,
543+
Time.now,
544+
nil
555545
)
556546
end
557547
end
558548

559-
def create_product(id:, num_variants:, num_options:, selling_plan_groups:, num_product_metafields:, num_variant_metafields:)
549+
def create_product(id:, num_variants:, num_options:, selling_plan_groups:, num_product_metafields:,
550+
num_variant_metafields:)
560551
# Create shared option values
561552
option_values_by_option = {}
562553
options = (1..num_options).map do |opt_idx|
563554
values = (1..3).map do |val_idx|
564555
ProductOptionValue.new(
565-
id: id * 1000 + opt_idx * 100 + val_idx,
566-
name: "Option#{opt_idx} Value#{val_idx}",
567-
position: val_idx,
568-
swatch_color: nil, # Most products don't have swatch colors
556+
id * 1000 + opt_idx * 100 + val_idx,
557+
"Option#{opt_idx} Value#{val_idx}",
558+
val_idx,
559+
nil # Most products don"t have swatch colors
569560
)
570561
end
571562
option_values_by_option[opt_idx] = values
572563

573564
ProductOption.new(
574-
id: id * 100 + opt_idx,
575-
name: "Option #{opt_idx}",
576-
position: opt_idx,
577-
values: values,
565+
id * 100 + opt_idx,
566+
"Option #{opt_idx}",
567+
opt_idx,
568+
values
578569
)
579570
end
580571

@@ -588,59 +579,63 @@ def create_product(id:, num_variants:, num_options:, selling_plan_groups:, num_p
588579

589580
# Match real ProductVariant structure with some nil fields (sparse data)
590581
ProductVariant.new(
591-
id: id * 1000 + var_idx,
592-
product_id: id,
593-
title: "Variant #{var_idx}",
594-
uncontextualized_title: nil,
595-
price: 1999 + var_idx * 100,
596-
compare_at_price: nil, # Most variants don't have compare_at_price
597-
barcode: nil, # Most variants don't have barcodes
598-
options: variant_options,
599-
option1: variant_options[0]&.name,
600-
option2: variant_options[1]&.name,
601-
option1_id: variant_options[0]&.id,
602-
option2_id: variant_options[1]&.id,
603-
taxable: true,
604-
position: var_idx,
605-
created_at: Time.now,
606-
updated_at: Time.now,
607-
fulfillment_service: "manual",
608-
requires_components: false,
609-
inventory_management: "shopify",
610-
inventory_policy: "deny",
611-
weight_unit: "kg",
612-
weight_value: nil,
613-
sku: "SKU-#{id}-#{var_idx}",
614-
requires_shipping: true,
615-
selling_plans: variant_selling_plans,
616-
metafields: create_metafields(owner_id: id * 1000 + var_idx, count: num_variant_metafields, owner_type: "ProductVariant"),
617-
variant_unit_price_measurement: nil,
582+
id * 1000 + var_idx,
583+
id,
584+
"Variant #{var_idx}",
585+
nil,
586+
1999 + var_idx * 100,
587+
nil, # Most variants don"t have compare_at_price
588+
nil, # Most variants don"t have barcodes
589+
variant_options,
590+
variant_options[0]&.name,
591+
variant_options[1]&.name,
592+
variant_options[0]&.id,
593+
variant_options[1]&.id,
594+
nil,
595+
true,
596+
nil,
597+
var_idx,
598+
Time.now,
599+
Time.now,
600+
"manual",
601+
false,
602+
"shopify",
603+
"deny",
604+
"kg",
605+
nil,
606+
"SKU-#{id}-#{var_idx}",
607+
true,
608+
variant_selling_plans,
609+
create_metafields(owner_id: id * 1000 + var_idx, count: num_variant_metafields, owner_type: "ProductVariant"),
610+
nil
618611
)
619612
end
620613

621614
# Match real Product structure with some nil fields (sparse data)
622615
Product.new(
623-
id: id,
624-
title: "Product #{id}",
625-
handle: "product-#{id}",
626-
description: "Description for product #{id}",
627-
vendor: "Vendor",
628-
published_at: Time.now,
629-
created_at: Time.now,
630-
updated_at: Time.now,
631-
template_suffix: nil,
632-
gift_card: false,
633-
is_published: true,
634-
requires_selling_plan: selling_plan_groups.any?,
635-
published_scope: :published_scope_global,
636-
variants: variants,
637-
options: options,
638-
selling_plan_groups: selling_plan_groups,
639-
metafields: create_metafields(owner_id: id, count: num_product_metafields, owner_type: "Product"),
616+
id,
617+
"Product #{id}",
618+
"product-#{id}",
619+
"Description for product #{id}",
620+
nil,
621+
"Vendor",
622+
Time.now,
623+
Time.now,
624+
Time.now,
625+
nil,
626+
false,
627+
true,
628+
selling_plan_groups.any?,
629+
:published_scope_global,
630+
variants,
631+
options,
632+
selling_plan_groups,
633+
create_metafields(owner_id: id, count: num_product_metafields, owner_type: "Product")
640634
)
641635
end
642636

643-
def create_test_data(num_products:, num_variants:, num_selling_plan_groups:, num_selling_plans_per_group:, num_product_metafields: 0, num_variant_metafields: 0)
637+
def create_test_data(num_products:, num_variants:, num_selling_plan_groups:, num_selling_plans_per_group:,
638+
num_product_metafields: 0, num_variant_metafields: 0)
644639
# Create SHARED selling plans - same instances used across all products
645640
selling_plan_id = 1
646641
selling_plan_groups = (1..num_selling_plan_groups).map do |group_idx|
@@ -705,14 +700,14 @@ def run_benchmark(coders, scenario)
705700
num_selling_plan_groups: scenario[:spg],
706701
num_selling_plans_per_group: scenario[:sp],
707702
num_product_metafields: scenario[:product_metafields] || 0,
708-
num_variant_metafields: scenario[:variant_metafields] || 0,
703+
num_variant_metafields: scenario[:variant_metafields] || 0
709704
)
710705

711706
puts "\nBenchmarking scenario: P:#{scenario[:products]} V:#{scenario[:variants]} PM:#{scenario[:product_metafields]} VM:#{scenario[:variant_metafields]} SPG:#{scenario[:spg]} SP:#{scenario[:sp]}"
712707

713708
payloads = coders.map { |coder| coder.factory.dump(data) }
714709

715-
result = Benchmark.ips(quiet: true) do |x|
710+
result = Benchmark.ips(time: ENV.fetch("BENCH_TIME", 5).to_f, warmup: ENV.fetch("BENCH_WARMUP", 2).to_f, quiet: true) do |x|
716711
coders.each.with_index do |coder, index|
717712
x.report(coder.name.split("::").last) do
718713
coder.factory.load(payloads[index])
@@ -760,19 +755,19 @@ def print_results(coders, reports)
760755
marshal_size = report.bytesize_results[Coders::Marshal]
761756

762757
puts "Scenario: #{scenario_str}"
763-
puts "Winner: #{sorted_results.first[0].name.split("::").last} with #{'%.2f' % sorted_results.first[1]}x speedup"
758+
puts "Winner: #{sorted_results.first[0].name.split("::").last} with #{"%.2f" % sorted_results.first[1]}x speedup"
764759
linesize = 56
765760
puts "-" * linesize
766-
puts format("%-20s %12s %10s %10s", "Coder", "Size (bytes)", "Size (%)", "Speedup")
761+
puts format("%-20s %12s %10s %10s", "Coder", "Size (bytes)", "Size factor", "Speedup")
767762
puts "-" * linesize
768763

769764
sorted_results.each do |result|
770765
coder, speedup = result
771766
size = report.bytesize_results[coder]
772-
size_pct = (size.to_f / marshal_size) * 100
767+
size_pct = (size.to_f / marshal_size)
773768
coder_name = coder.name.split("::").last
774769

775-
line = format("%-20s %12d %9.1f%% %9.2fx", coder_name, size, size_pct, speedup)
770+
line = format("%-20s %12d %9.2fx %9.2fx", coder_name, size, size_pct, speedup)
776771
if coder == Coders::Marshal
777772
puts "#{BOLD}#{line}#{RESET}"
778773
else

0 commit comments

Comments
 (0)