Upgrade to Pro — share decks privately, control downloads, hide ads and more …

フレームグラフこわくない - Singedで始めるパフォーマンス改善

naohisa
November 09, 2023
4.1k

フレームグラフこわくない - Singedで始めるパフォーマンス改善

After Kaigi on Rails LT Night 〜SmartBank x メドピア x マイベスト〜での登壇資料です。
https://smartbank.connpass.com/event/299631/

naohisa

November 09, 2023
Tweet

Transcript

  1. stackprof コード挿入が必要ですが、プロファイリング箇所や対象のコントロールが容易 StackProf.run(mode: :cpu, out: 'tmp/stackprof-cpu-myapp.dump', raw: true) do #

    code you want to profile here end フレームグラフ取得のためには追加でコマンドが必要 $ stackprof --d3-flamegraph tmp/stackprof-cpu-myapp.dump > flamegraph.html 26
  2. 多様なプロファイリング方法 ブロックで囲う flamegraph { # your code here } コントローラーで特定のアクションを指定

    class EmployeesController < ApplicationController flamegraph :show def show # your code here end end 30
  3. 多様なプロファイリング方法 RSpecで特定のテストを指定 require 'singed/rspec' RSpec.describe YourClass do it "is slow

    :(", flamegraph: true do # your code here end end リクエストヘッダーで指定 $ curl -H 'X-Singed: true' https://localhost:3000 CLIでの実行時に指定 $ bundle exec singed -- bin/rails runner 'Model.all.to_a' 31
  4. flamegraph出力のための設定 ※ /users/:id というエンドポイントへのリクエストをプロファイルします。 ※ わざと遅い処理を挟んでフレームグラフ上でわかりやすくしています。 class UsersController < ApplicationController

    + flamegraph :show def show + User.all.find_each { |user| user.some_slow_method } @user = User.find(params[:id]) @microposts = @user.microposts.paginate(page: params[:page]) end class User < ApplicationRecord + def some_slow_method + sleep 0.0005 + end 42