1
1
# frozen_string_literal: true
2
2
3
3
require_relative "filename_filter"
4
+ require "cgi/util"
4
5
5
6
module Vernier
6
7
module Output
@@ -41,8 +42,6 @@ def samples_by_file
41
42
h [ k ] = SamplesByLocation . new
42
43
end
43
44
44
- total = weights . sum
45
-
46
45
samples . zip ( weights ) . each do |stack_idx , weight |
47
46
# self time
48
47
top_frame_index = stack_table . stack_frame_idx ( stack_idx )
@@ -75,7 +74,7 @@ def samples_by_file
75
74
end
76
75
end
77
76
78
- def output
77
+ def output ( template : nil )
79
78
output = +""
80
79
81
80
relevant_files = samples_by_file . select do |k , v |
@@ -84,13 +83,18 @@ def output
84
83
next if k . start_with? ( "<" )
85
84
v . values . map ( &:total ) . sum > total * 0.01
86
85
end
87
- relevant_files . keys . sort . each do |filename |
86
+
87
+ if template == "html"
88
+ html_output ( output , relevant_files )
89
+ else
90
+ relevant_files . keys . sort . each do |filename |
91
+ output << "=" *80 << "\n "
92
+ output << filename << "\n "
93
+ output << "-" *80 << "\n "
94
+ format_file ( output , filename , samples_by_file , total : total )
95
+ end
88
96
output << "=" *80 << "\n "
89
- output << filename << "\n "
90
- output << "-" *80 << "\n "
91
- format_file ( output , filename , samples_by_file , total : total )
92
97
end
93
- output << "=" *80 << "\n "
94
98
end
95
99
96
100
def total
@@ -114,6 +118,35 @@ def format_file(output, filename, all_samples, total:)
114
118
end
115
119
end
116
120
end
121
+
122
+ def html_output ( output , relevant_files )
123
+ output << "<pre>"
124
+ output << " SELF FILE\n "
125
+ relevant_files . sort_by { |k , v | -v . values . map ( &:self ) . sum } . each do |filename , file_contents |
126
+ tmpl = "<details style=\" display:inline-block;vertical-align:top;\" ><summary>%s</summary>"
127
+ output << sprintf ( "% 5.1f%% #{ tmpl } \n " , file_contents . values . map ( &:self ) . sum * 100 / total . to_f , filename )
128
+ format_file_html ( output , filename , relevant_files )
129
+ output << "</details>\n "
130
+ end
131
+ output << "</pre>"
132
+ end
133
+
134
+ def format_file_html ( output , filename , relevant_files )
135
+ samples = relevant_files [ filename ]
136
+
137
+ # file_name, lines, file_wall, file_cpu, file_idle, file_sort
138
+ output << sprintf ( " TOTAL | SELF | LINE SOURCE\n " )
139
+ File . readlines ( filename ) . each_with_index do |line , i |
140
+ lineno = i + 1
141
+ calls = samples [ lineno ]
142
+
143
+ if calls && calls . total > 0
144
+ output << sprintf ( "%5.1f%% | %5.1f%% | % 4i %s" , 100 * calls . total / total . to_f , 100 * calls . self / total . to_f , lineno , CGI ::escapeHTML ( line ) )
145
+ else
146
+ output << sprintf ( " | | % 4i %s" , lineno , CGI ::escapeHTML ( line ) )
147
+ end
148
+ end
149
+ end
117
150
end
118
151
end
119
152
end
0 commit comments