Created
May 18, 2020 17:10
-
-
Save iarie/840f9427ab404d6fddd02b9535f5aa69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module AssignmentsBenchmark | |
@@zip = '02026' | |
@@orders = {} | |
@@ready = false | |
def self.prepare | |
generator = OrderGenerator.new(500) | |
@@orders = [1, 5, 10, 25, 50, 100, 150, 200, 500].map do |n| | |
[ n, generator.call(n, rand(8..12)) ] | |
end.to_h | |
@@ready = true | |
end | |
def self.flush! | |
@@orders.values.each(&:destroy!) | |
@@orders = {} | |
@@ready = false | |
end | |
def self.run | |
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC) | |
prepare unless @@ready | |
end_prepare = Process.clock_gettime(Process::CLOCK_MONOTONIC) | |
prepare_done = end_prepare - starting | |
report = Benchmark.ips do |x| | |
x.iterations = 3 | |
@@orders.each do |n, order| | |
# x.report("V1-#{n}") do | |
# Assignments::Delivery.new(order.reload, @@zip, mcu_cart: false).without_sku_keys | |
# end | |
# x.report("V2-#{n}") do | |
# Assignments::DeliveryV2.new(order.reload, @@zip, mcu_cart: false).call | |
# end | |
x.report("MCU-V1-#{n}") do | |
Assignments::Delivery.new(order.reload, @@zip, mcu_cart: true).without_sku_keys | |
end | |
x.report("MCU-V2-#{n}") do | |
Assignments::Delivery2.new(order.reload, @@zip, mcu_cart: true).call | |
end | |
end | |
end | |
end_experiment = Process.clock_gettime(Process::CLOCK_MONOTONIC) | |
experiment_done = end_experiment - starting | |
flush! | |
p "Preparation done: #{prepare_done}" | |
p "Expeirment done: #{experiment_done}" | |
report | |
end | |
# AssignmentsBenchmark::OrderGenerator | |
class OrderGenerator | |
def initialize(max_variants = 5) | |
@variants = [] | |
puts 'Searching variants...' | |
while @variants.size < max_variants | |
rest = max_variants - @variants.size | |
puts "rest: #{rest}" | |
@variants += sample_variants(rest) | |
puts "found: #{@variants.size}" | |
end | |
end | |
def call(n = 5, g = 1) | |
variants = random_variants(n) | |
user = Spree::User.first | |
address = Spree::Address.last | |
order = Spree::Order.create!( | |
user: user, | |
shipping_address: address, | |
store_id: 1 | |
) | |
order.contents.add_in_batch(variants) | |
order.reload.set_line_item_prices(true) | |
shipping_rules = ShippingRule.order('random()').take(g - 1).map(&:id).prepend(nil).cycle | |
order.line_items.each do |item| | |
item.update!(shipping_rule_id: shipping_rules.next) | |
end | |
order | |
end | |
private | |
def sample_variants(n) | |
Spree::Variant. | |
joins(:store_variants, :prices). | |
where(is_master: false). | |
where(store_variants: { sold: true, spree_store_id: 1 }). | |
order('random()').limit(n). | |
map do |x| | |
max = x.stock_items.maximum(:count_on_hand) | |
next if max.nil? || max.zero? | |
quantity = rand(1..max) | |
[x.id, quantity] | |
end.compact | |
end | |
def random_variants(n) | |
@variants.shuffle.take(n).map do |x| | |
[ | |
x[0], | |
{ 'quantity' => rand(1..(x[1])) } | |
] | |
end.to_h | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment