Created
          November 5, 2020 16:38 
        
      - 
      
 - 
        
Save bitglue/92ff113a0d8b1e1effffa7a2aceb215a to your computer and use it in GitHub Desktop.  
    convert a ruby stackprof file to the format required by pprof
  
        
  
    
      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
    
  
  
    
  | # writes protobuf to stdout. Gzip that, and then https://github.com/google/pprof can read it. | |
| require 'stackprof' | |
| require 'pp' | |
| require './profile_pb' | |
| filename = 'profile-web-2020-11-04' | |
| data = Marshal.load(IO.binread(filename)) | |
| class StringMap | |
| def initialize() | |
| @strings = [] | |
| @string_hash = {} | |
| add("") # spec says string_table[0] must always be "". | |
| end | |
| attr_reader :strings | |
| attr_reader :string_hash | |
| def add(s) | |
| i = string_hash[s] | |
| return i unless i == nil | |
| i = strings.push(s).length - 1 | |
| string_hash[s] = i | |
| return i | |
| end | |
| end | |
| samples = [] | |
| locations = [] | |
| functions = [] | |
| string_map = StringMap.new | |
| data[:frames].each do |location_id, location| | |
| functions.push(Perftools::Profiles::Function.new( | |
| :id => location_id, | |
| :name => string_map.add(location[:name]), | |
| :filename => string_map.add(location[:file]), | |
| :start_line => location[:line], | |
| )) | |
| line = Perftools::Profiles::Line.new( | |
| :function_id => location_id, | |
| :line => location[:line], | |
| ) | |
| locations.push(Perftools::Profiles::Location.new( | |
| :id => location_id, | |
| :line => [line], | |
| )) | |
| end | |
| raw = data[:raw] | |
| while len = raw.shift | |
| frames = raw.slice!(0, len) | |
| frames.reverse! | |
| weight = raw.shift | |
| sample = Perftools::Profiles::Sample.new( | |
| :value => [(weight * data[:interval] * 1e-3).round], | |
| :location_id => frames, | |
| ) | |
| samples.push(sample) | |
| end | |
| sample_type = Perftools::Profiles::ValueType.new( | |
| :type => string_map.add(data[:mode]), | |
| :unit => string_map.add("ms"), | |
| ) | |
| message = Perftools::Profiles::Profile.new( | |
| :sample_type => [sample_type], | |
| :sample => samples, | |
| :string_table => string_map.strings, | |
| :location => locations, | |
| :function => functions, | |
| ) | |
| STDOUT.write(Perftools::Profiles::Profile.encode(message)) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment