@ledsun blog

無味の味は佳境に入らざればすなわち知れず

処理時間計測用のRubyスクリプト

ある関数をチューニングするために、つぎのような計測用のスクリプトを作成しました。

require 'benchmark'

annotations_collection = JSON.parse File.read(ARGV[0])
annotations_collection.each { _1.deep_symbolize_keys! }
project = Project.find_by(id: 1)

Benchmark.bmbm do |x|
  x.report("instantiate_and_save_annotations_collection") do
    10.times do
      project.instantiate_and_save_annotations_collection annotations_collection
    end
  end
end

ファイルから読み込んだJSONファイルを処理する時間を計測します。 チューニング対象はRailsアプリケーション内のクラスなのでrails runnerで実行します。

参考

module Benchmark (Ruby 3.1 リファレンスマニュアル)