손만이의 개발노트

성능 최적화(ruby-prof) 본문

Ruby

성능 최적화(ruby-prof)

sonman 2019. 12. 5. 19:00

Ruby-prof

ruby의 대표적인 프로파일러 입니다.
뉴렐릭과 같은 rails와 호환되는 유명한 프로파일러가 있음에도 ruby-prof를 본 문서에서 기술하는 이유는
뉴렐릭은 모니터링형태로 웹사이트에서 제공되는 반면 ruby-prof의 경우

(1)코드 작성 단계에서 빠르게 원하는 부분만큼의 성능을 프로파일링 해볼 수 있으며
(2)다양한 포맷으로 결과를 확인 해 볼 수 있어

본 문서에는 ruby-prof에 대하여 기술하였습니다.

프로파일링 사용 예제

  1. ruby_prof_helper.rb
    1. 소스 : 

      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

      module RubyProfHelper

          require 'ruby-prof'

       

          def performance_start

              RubyProf.start

          end

       

          def performance_stop(file_name = 'common_performance_printer.txt',options = 'GraphPrinter')

       

              result = RubyProf.stop

              printer = RubyProf::GraphHtmlPrinter.new(result)

       

              File.open file_name, 'w+' do |file|

                  RubyProf::GraphPrinter.new(result).print(file) if options == "GraphPrinter"

                  RubyProf::GraphHtmlPrinter.new(result).print(file) if options == "GraphHtmlPrinter"

                  RubyProf::CallInfoPrinter.new(result).print(file) if options == "CallInfoPrinter"

                  RubyProf::CallStackPrinter.new(result).print(file) if options == "CallStackPrinter"

                  RubyProf::CallTreePrinter.new(result).print(file) if options == "CallTreePrinter"

                  RubyProf::DotPrinter.new(result).print(file) if options == "DotPrinter"

                  RubyProf::FlatPrinter.new(result).print(file) if options == "FlatPrinter"

                  RubyProf::FlatPrinterWithLineNumbers.new(result).print(file) if options == "FlatPrinterWithLineNumbers"

                   

              end

          end

       

      end

       - 2번 라인 : ruby-prof 라이브러리를 사용하기 위해 선언
       - 4~6번 라인 : 프로파일링을 시작하는 기능이 포함된 사용자정의 함수
       - 8~24번 라인 : 프로파일링 종료 및 프로젝트 내부 경로에 결과에 대한 파일을 생성
                                , file_name은 전달받는 파라미터가 없을 경우 default로 common_performance_printer.txt를 생성
                                , options은 프로파일링에 사용될 프린터를 선택하는 옵션으로 없을 경우 기본적으로 GraphPrinter 이용하여 결과를 출력한다.
                                , 문제점 : file출력시 Encoding::UndefinedConversionError: "\xED" from ASCII-8BIT to UTF-8 에러가 발생하는 printer클래스가 있어 현재는 기본적인 옵션 GraphPrinter만 사용할 것을 권한다.

  2. channel_intro_test.rb (본 문서에서 프로파일링에 사용되는 예제 코드)

    1. 소스 : 

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      require_relative "#{Rails.root}/test/controllers/api/v1/api_test_helper.rb"

      require_relative "#{Rails.root}/spec/support/ruby_prof_helper.rb"

       

      class ChannelIntroTest < ActionDispatch::IntegrationTest

        include ApplicationHelper

        include APITestHelper

        include RubyProfHelper

       

        def setup

          baseSetup

          set_channel_test

        end

         

        test '퀘스트 가입유저 리스트 조회' do

          performance_start

           

         

          get "/api/v1/channels/#{@channel.id}/users", headers: @headers

          assert_response :success

           

          performance_stop

           

        end

      end

       - 2번 라인 : ruby-prof를 사용하기 위해 require 선언
       - 7번 라인 : ruby-prof include하여 사용가능하게 선언.
       - 15번 라인 : 성능테스트 시작
       - 21번 라인 : 성능테스트 종료

  3. common_performance_printer.txt
    1. 샘플로 출력한 데이터로 http 성능 확인의 경우 라인수가 많음.

  4. 기타 
    1. encoding에러 수정하여 다양한 형식의 성능 확인을 하게 할 예정.
    2. 단순하게 라인 단위로 성능 테스트 권장.

  5. 계획
    1. 성능최적화를 위한 코드 가이드도 추가할 예정