rblineprof-report 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 43d21ba85e97afcddcc2fa4176fe948548e7c1ff
4
+ data.tar.gz: 348b67b3b1a92b1ab7caae17132ba7b3e8731def
5
+ SHA512:
6
+ metadata.gz: 67dae2a435234c504a2b9c3a127a1ef03efc84853f19de8a44cdcceadc2ff169fb9151cd3951136d2630c1cd35ad2e618744a7eb5b9b3fa98c8adc8aca368300
7
+ data.tar.gz: 1506ac88e3fd5933da6bdce273afe8664aa4c6c0bdc3df724c9b9c39c834f0cfbe73da797a243e164dc074028a44f5ad77754b198eb1ab48141e80f927af8332
@@ -0,0 +1,7 @@
1
+ require 'rblineprof/report'
2
+
3
+ module LineProf
4
+ def self.report
5
+ Report.new.report(profile, options)
6
+ end
7
+ end
@@ -0,0 +1,34 @@
1
+ require 'rblineprof'
2
+ require 'term/ansicolor'
3
+ require 'rblineprof/report/sample'
4
+ require 'rblineprof/report/source'
5
+
6
+ module LineProf
7
+
8
+ CONTEXT = 0
9
+ NOMINAL = 1
10
+ WARNING = 2
11
+ CRITICAL = 3
12
+
13
+ class Report
14
+ def self.report(profile, options = {})
15
+ self.new.report(profile, options)
16
+ end
17
+
18
+ # options
19
+ # :context
20
+ # :thresholds
21
+ def report(profile, options = {})
22
+ puts Term::ANSIColor.blue("\n[LineProf] #{'=' * 63}") + "\n\n" +
23
+ format_profile(profile, options) + "\n"
24
+ end
25
+
26
+ def format_profile(profile, options = {})
27
+ sources = profile.map do |filename, samples|
28
+ Source.new filename, samples, options
29
+ end
30
+
31
+ sources.map(&:format).compact.join "\n"
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ module LineProf
2
+ class Report
3
+ class Sample < Struct.new :ms, :calls, :line, :code, :level
4
+
5
+ def format colorize = true
6
+ formatted = if level == CONTEXT
7
+ sprintf " | % 3i %s", line, code
8
+ else
9
+ sprintf "% 6.1fms %5i | % 3i %s", ms, calls, line, code
10
+ end
11
+
12
+ return formatted unless colorize
13
+
14
+ case level
15
+ when CRITICAL
16
+ color.red formatted
17
+ when WARNING
18
+ color.yellow formatted
19
+ when NOMINAL
20
+ color.white formatted
21
+ else # CONTEXT
22
+ color.intense_black formatted
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def color
29
+ Term::ANSIColor
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,83 @@
1
+ module LineProf
2
+ class Report
3
+ class Source
4
+
5
+ attr_reader :file_name, :raw_samples, :options
6
+
7
+ def initialize file_name, raw_samples, options = {}
8
+ @file_name, @raw_samples, @options = file_name, raw_samples, options
9
+ end
10
+
11
+ def format colorize = true
12
+ return nil if samples.empty?
13
+
14
+ formatted = file_name.sub(Dir.pwd + '/', '') + "\n"
15
+
16
+ prev_line = samples.first.line - 1
17
+ samples.each do |sample|
18
+ if sample.line != prev_line + 1
19
+ formatted << color.intense_black(' ' * 14 + '.' * 7) + "\n"
20
+ end
21
+ prev_line = sample.line
22
+
23
+ formatted << sample.format
24
+ end
25
+
26
+ formatted
27
+ end
28
+
29
+ def samples
30
+ @samples ||= begin
31
+ parsed = []
32
+
33
+ raw_samples.each_with_index do |sample, line|
34
+ next if line == 0 # drop file info
35
+
36
+ ms = sample[0] / 1000.0
37
+ calls = sample[2]
38
+
39
+ abnormal = ms >= thresholds[NOMINAL]
40
+ near_abnormal = (line-context..line+context).any? do |near|
41
+ near = [1, near].max
42
+ next unless raw_samples[near]
43
+ (raw_samples[near][0] / 1000.0) >= thresholds[NOMINAL]
44
+ end
45
+
46
+ next unless abnormal or near_abnormal
47
+
48
+ threshold = thresholds.invert.detect { |th, _| ms > th }
49
+ level = threshold ? threshold.last : CONTEXT
50
+
51
+ next unless code = source_lines[line - 1]
52
+ parsed << Sample.new(ms, calls, line, code, level)
53
+ end
54
+
55
+ parsed
56
+ end
57
+ end
58
+
59
+ def source_lines
60
+ @source_lines ||= ::File.open(file_name, 'r').to_a
61
+ end
62
+
63
+ private
64
+
65
+ def color
66
+ Term::ANSIColor
67
+ end
68
+
69
+ def context
70
+ options.fetch :context, 2
71
+ end
72
+
73
+ def thresholds
74
+ @thresholds ||= {
75
+ CRITICAL => 50,
76
+ WARNING => 5,
77
+ NOMINAL => 0.2
78
+ }.merge(options.fetch :thresholds, {})
79
+ end
80
+
81
+ end
82
+ end
83
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rblineprof-report
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Naotoshi Seo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: term-ansicolor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Format and print the result of rblineprof (github.com/tmm1/rblineprof)
42
+ email:
43
+ - sonots@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/rblineprof-report.rb
49
+ - lib/rblineprof/report.rb
50
+ - lib/rblineprof/report/sample.rb
51
+ - lib/rblineprof/report/source.rb
52
+ homepage: https://github.com/sonots/rblineprof-report
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.2.2
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Format and print the result of rblineprof (github.com/tmm1/rblineprof)
76
+ test_files: []