railsprof 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dd676c3941cfebbd315805ed26916b228d3851e2
4
- data.tar.gz: 0e3a3adfd11b13be3370e992d4b3f97e6713527a
3
+ metadata.gz: 69f4db46ad80ecccc6865b388d9b0635e4fbfa1b
4
+ data.tar.gz: f9b8821aaf2a7d25283628678af859981ec0ad13
5
5
  SHA512:
6
- metadata.gz: 94968394aacd9891748bc669235f6fea34760e08aef2fe7a56b02e3ed2f2c02a5b3085d95b13342c809c5af81dfb96bd94cd69d8408c15b7f1f3acd9c8c402ad
7
- data.tar.gz: c6bb5b06f0b9928194699ef571c7dca8e8a22a8da3fe9b336465deee9e49bc40f3f5ab66749583fc73afcfd02ea75d049fec507022faaee0779871bb2e4b5d6f
6
+ metadata.gz: 05aadce244d50592ab86757ba849be1c442236c87abeda0e2aefc188de573989edba4862eb1e9520a190e5e92db61a38a2f7fa8ac472b40e64fbda784d743539
7
+ data.tar.gz: a2f0fd21ec88b697fad8182d9f4ee558f2514c3499643e8a30791c86b41a3dd000bfaf8b998b52a72c856f6ca9566f595ee6a575b96b8322c5cccc8662c43e69
@@ -1,4 +1,5 @@
1
1
  require 'erb'
2
+ require 'pp'
2
3
 
3
4
  class Railsprof::LineprofParser
4
5
  # rblineprof formatting
@@ -13,20 +14,28 @@ class Railsprof::LineprofParser
13
14
  # ...
14
15
  # ]
15
16
  # }
17
+ attr_reader :logger, :options
16
18
 
17
- def initialize(profile, paths, threshold_ms: 0.5)
19
+ def initialize(profile, paths, threshold_ms: 0.5, **options)
18
20
  @threshold = threshold_ms * 1000
19
21
  @paths = paths
20
22
  @profile = profile
21
23
  @friendly_paths = {}
22
24
  @profiled_source = {}
25
+ @logger = options[:logger]
26
+ @total_ms = options[:total_ms]
27
+ @options = options
23
28
 
24
29
  @profiled_files = profile
25
30
  .reduce([]) do |files, (file, lines)|
26
31
  total = lines[0][0]
27
32
  if total > @threshold # && !file[/benchmark|\.rake$/] # time in micros
28
33
  @friendly_paths[file] = @paths.relative_path_for(file)
29
- files << [file, *lines[0]]
34
+ # [total_time, child_time, excl_time, total_cpu, child_cpu, excl_cpu],
35
+ tt, _ct, _et, tc, _cc, _ec = lines[0]
36
+ # total, cpu, idle, exclusive %
37
+ file_timings = [tt, tc, tt - tc].map { |n| n / 1000.0 }
38
+ files << [file, *file_timings]
30
39
  end
31
40
  files
32
41
  end
@@ -34,11 +43,13 @@ class Railsprof::LineprofParser
34
43
  end
35
44
 
36
45
  def cli_report
37
- puts "\n-- Top files by execution time (total / child / excl / filename) --\n"
46
+ spacing = 12
47
+ logger.info ''
48
+ logger.info(%W(total cpu idle).map { |s| s.ljust(spacing) }.join << " file")
38
49
 
39
- @profiled_files.each do |file, total, child, excl|
40
- printf "%9.2fms %9.2fms %9.2fms %s\n" %
41
- [total / 1000.0, child / 1000.0, excl / 1000.0, @friendly_paths[file]]
50
+ @profiled_files.each do |file, *timings|
51
+ str = timings.map { |t| ('%.1fms' % t).ljust(spacing) }.join
52
+ logger.info "#{str} #{@friendly_paths[file]}"
42
53
  end
43
54
  end
44
55
 
@@ -57,6 +68,7 @@ class Railsprof::LineprofParser
57
68
  ERB.new(template, 0, "", "@html_output").result(b)
58
69
  File.open(filename, 'w') { |f| f.write(@html_output) }
59
70
 
71
+ logger.info "Results in #{filename}"
60
72
  `open #{filename}`
61
73
  end
62
74
 
@@ -66,6 +78,7 @@ class Railsprof::LineprofParser
66
78
  @profiled_source[file] = "\n % 8s + % 8s (called)\n" % %w(cpu idle)
67
79
  File.readlines(file).each_with_index do |line, num|
68
80
  wall, cpu, calls, _allocations = @profile[file][num + 1]
81
+ # excl cpu, excl idle, total cpu, total idle
69
82
  @profiled_source[file] <<
70
83
  if calls && calls > 0
71
84
  '% 8.1fms + % 8.1fms % 8s | %s' %
@@ -12,6 +12,7 @@ class Railsprof::Profiler
12
12
 
13
13
  def initialize(args, opts = {})
14
14
  @options = Railsprof::DEFAULT_OPTIONS.merge(opts)
15
+ @history = []
15
16
 
16
17
  @logger = options[:logger] || Railsprof::Logger.new(STDOUT)
17
18
  @logger.level = options[:log_level] || Logger::INFO
@@ -24,7 +25,7 @@ class Railsprof::Profiler
24
25
  args.first.upcase!
25
26
  @method, @path = args
26
27
  else
27
- fail ArgumentError "excepted METHOD PATH, received: #{args.join(' ')}"
28
+ fail ArgumentError, "excepted METHOD PATH, received: #{args.join(' ')}"
28
29
  end
29
30
 
30
31
  logger.debug "Request: #@method #@path"
@@ -39,7 +40,7 @@ class Railsprof::Profiler
39
40
  if options[:runs] > 0
40
41
  logger.info 'Profiling....'
41
42
 
42
- say_with_time('All profiles') do
43
+ total = say_with_time('All profiles') do
43
44
  @profile = lineprof(@paths.regexp) do
44
45
  options[:runs].times do |i|
45
46
  say_with_time("Profile ##{i + 1}") { run }
@@ -50,7 +51,15 @@ class Railsprof::Profiler
50
51
 
51
52
  parser = Railsprof::LineprofParser.new(
52
53
  @profile, @paths,
53
- threshold_ms: options[:threshold]
54
+ threshold_ms: options[:threshold],
55
+ logger: logger,
56
+ profiler_options: @options.slice(*%i[
57
+ gems app_paths threshold cookies params session
58
+ ]),
59
+ history: @history,
60
+ method: @method,
61
+ path: @path,
62
+ total_ms: total
54
63
  )
55
64
 
56
65
  parser.cli_report
@@ -99,9 +108,9 @@ class Railsprof::Profiler
99
108
  env_file = Dir.pwd + '/config/environment.rb'
100
109
  if File.exists?(env_file)
101
110
  ms = Benchmark.ms { load env_file }
102
- logger.info 'App loaded in %.2f secs' % (ms / 1000.0)
111
+ logger.info 'Loaded in %.2f secs (%s mode)' % [ms / 1000.0, Rails.env]
103
112
  else
104
- puts 'Exiting... an application with config/environment.rb was expected'
113
+ logger.error 'Exiting... an application with config/environment.rb was expected'
105
114
  exit 1
106
115
  end
107
116
  @loaded = true
@@ -110,8 +119,10 @@ class Railsprof::Profiler
110
119
  def say_with_time(msg, level: 'info', &block)
111
120
  ret = nil
112
121
  ms = Benchmark.ms { ret = block.call }
113
- logger.send(level, '%s completed in %.2fms' % [msg, ms])
114
- ret
122
+ str = '%s completed in %.2fms' % [msg, ms]
123
+ logger.send(level, str)
124
+ @history << str
125
+ ms
115
126
  end
116
127
  end
117
128
 
@@ -1,3 +1,3 @@
1
1
  module Railsprof
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -5,6 +5,17 @@
5
5
  <link href="http://clifton.is/css/normalize.css" rel="stylesheet" type="text/css">
6
6
  <link href="http://clifton.is/css/highlight.github.css" rel="stylesheet" type="text/css">
7
7
  <style>
8
+ * { box-sizing: border-box; }
9
+ .main { margin: 0 20px 50px; }
10
+ [double] {
11
+ margin: 0 0;
12
+ padding: 0 0;
13
+ font-size: 200%;
14
+ }
15
+ pre:first {
16
+ padding-top: 0;
17
+ margin 0 0;
18
+ }
8
19
  pre {
9
20
  overflow: auto;
10
21
  word-wrap: normal;
@@ -13,45 +24,53 @@
13
24
  </style>
14
25
  </head>
15
26
  <body>
16
- <table>
17
- <thead>
18
- <tr>
19
- <td width='120px'>Wall Total</td>
20
- <td width='110px'>Wall Child</td>
21
- <td width='110px'>Wall Excl</td>
22
- <td width='90px'>CPU Total</td>
23
- <td width='90px'>CPU Child</td>
24
- <td width='90px'>CPU Excl</td>
25
- <td>File</td>
26
- </tr>
27
- </thead>
28
- <tbody>
29
- <% @profiled_files.each do |file, *times| %>
27
+ <div class='main'>
28
+ <pre>
29
+ <code double><%= @options.values_at(:method, :path).join(' ') %></code>
30
+ <code class='ruby'>
31
+ <%= @options[:profiler_options].map { |k, v| ":#{k.to_s.ljust(12)} #{v.inspect}" }.join("\n") %></code>
32
+ <code>
33
+ <%= @options[:history].join("\n") %></code>
34
+ </pre>
35
+ <table>
36
+ <thead>
30
37
  <tr>
31
- <% times.each do |t| %>
32
- <td><%= t / 1000.0 %></td>
33
- <% end %>
34
- <td>
35
- <a href='#' class='js-toggle-code' data-file='<%= file %>'>
36
- <%= @friendly_paths[file] %>
37
- </a>
38
- </td>
38
+ <td width='90px'>Total</td>
39
+ <td width='90px'>CPU</td>
40
+ <td width='90px'>Idle</td>
41
+ <td>File</td>
39
42
  </tr>
40
- <tr style='display:none'>
41
- <td colspan='7' style='overflow: auto'>
42
- <br style='clear:both'>
43
- <pre>
44
- <code class="ruby"><%= @profiled_source[file] %></code>
45
- </pre>
46
- </td>
47
- </tr>
48
- <% end %>
49
- </tbody>
50
- </table>
43
+ </thead>
44
+ <tbody>
45
+ <% @profiled_files.each do |file, *timings| %>
46
+ <tr>
47
+ <% timings.each do |t| %>
48
+ <td><%= '%.1fms' % t %></td>
49
+ <% end %>
50
+ <td>
51
+ <a href='#' class='js-toggle-code' data-file='<%= file %>'>
52
+ <%= @friendly_paths[file] %>
53
+ </a>
54
+ </td>
55
+ </tr>
56
+ <tr style='display:none'>
57
+ <td colspan='7' style='overflow: auto'>
58
+ <br style='clear:both'>
59
+ <pre>
60
+ <code class="ruby"><%= @profiled_source[file] %></code>
61
+ </pre>
62
+ </td>
63
+ </tr>
64
+ <% end %>
65
+ </tbody>
66
+ </table>
67
+ </div>
51
68
  <script src='http://code.jquery.com/jquery-1.11.0.min.js'></script>
52
69
  <script src='http://clifton.is/js/highlight.js'></script>
53
70
  <script>
54
- hljs.initHighlightingOnLoad();
71
+ $(function () {
72
+ $('.ruby').each(function(i, e) { hljs.highlightBlock(e, 'ruby') });
73
+ });
55
74
  $('.js-toggle-code').on('click', function () {
56
75
  $(this).parents('tr').next().toggle();
57
76
  return false;
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railsprof
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clifton King
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-19 00:00:00.000000000 Z
11
+ date: 2014-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler