File: profiling.rb

package info (click to toggle)
ruby-batch-loader 2.0.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 284 kB
  • sloc: ruby: 783; makefile: 6; sh: 4
file content (26 lines) | stat: -rw-r--r-- 655 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# frozen_string_literal: true

# Usage: ruby spec/benchmarks/profiling.rb && open tmp/stack.html

require 'ruby-prof'

require_relative "../../lib/batch_loader"
require_relative "../fixtures/models"

User.save(id: 1)
iterations = Array.new(5_000)

def batch_loader
  BatchLoader.for(1).batch do |ids, loader|
    User.where(id: ids).each { |user| loader.call(user.id, user) }
  end
end

RubyProf.measure_mode = RubyProf::WALL_TIME
RubyProf.start

iterations.each { batch_loader.id } # 2.46, 2.87, 2.56 sec

result = RubyProf.stop
stack_printer = RubyProf::CallStackPrinter.new(result)
File.open("tmp/stack.html", 'w') { |file| stack_printer.print(file) }