1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 # Copyright 2014 Alexandre Terrasa <alexandre@moz-code.org>
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # Helpers for various statistics tools.
26 redef class ToolContext
29 var opt_all
= new OptionBool("Compute all metrics", "--all")
32 var opt_mmodules
= new OptionBool("Compute metrics about mmodules", "--mmodules")
34 var opt_mclasses
= new OptionBool("Compute metrics about mclasses", "--mclasses")
36 var opt_mendel
= new OptionBool("Compute mendel metrics", "--mendel")
38 var opt_inheritance
= new OptionBool("Compute metrics about inheritance usage", "--inheritance")
40 var opt_refinement
= new OptionBool("Compute metrics about refinement usage", "--refinement")
42 var opt_self
= new OptionBool("Compute metrics about the usage of explicit and implicit self", "--self")
44 var opt_ast
= new OptionBool("Compute metrics about the usage of nodes and identifiers in the AST", "--ast")
46 var opt_nullables
= new OptionBool("Compute metrics on nullables send", "--nullables")
48 var opt_static_types
= new OptionBool("Compute explicit static types metrics", "--static-types")
50 var opt_tables
= new OptionBool("Compute tables metrics", "--tables")
52 var opt_rta
= new OptionBool("Compute RTA metrics", "--rta")
54 var opt_readme
= new OptionBool("Compute ReadMe metrics", "--readme")
56 var opt_csv
= new OptionBool("Also export metrics in CSV format", "--csv")
58 var opt_generate_hyperdoc
= new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
60 var opt_poset
= new OptionBool("Complete metrics on posets", "--poset")
62 var opt_nocolors
= new OptionBool("Disable colors in console outputs", "--no-colors")
64 var opt_dir
= new OptionString("Directory where some statistics files are generated", "-d", "--dir")
66 # Output directory for metrics files.
67 var output_dir
: String = "."
72 self.option_context
.add_option
(opt_all
)
73 self.option_context
.add_option
(opt_mmodules
)
74 self.option_context
.add_option
(opt_mclasses
)
75 self.option_context
.add_option
(opt_mendel
)
76 self.option_context
.add_option
(opt_inheritance
)
77 self.option_context
.add_option
(opt_refinement
)
78 self.option_context
.add_option
(opt_self
)
79 self.option_context
.add_option
(opt_ast
)
80 self.option_context
.add_option
(opt_nullables
)
81 self.option_context
.add_option
(opt_static_types
)
82 self.option_context
.add_option
(opt_tables
)
83 self.option_context
.add_option
(opt_rta
)
84 self.option_context
.add_option
(opt_readme
)
85 self.option_context
.add_option
(opt_csv
)
86 self.option_context
.add_option
(opt_generate_hyperdoc
)
87 self.option_context
.add_option
(opt_poset
)
88 self.option_context
.add_option
(opt_dir
)
89 self.option_context
.add_option
(opt_nocolors
)
92 redef fun process_options
(args
)
95 var val
= self.opt_dir
.value
97 val
= val
.simplify_path
103 # Format and colorize a string heading of level 1 for console output.
105 # Default style is yellow and bold.
106 fun format_h1
(str
: String): String do
107 if opt_nocolors
.value
then return str
108 return str
.yellow
.bold
111 # Format and colorize a string heading of level 2 for console output.
113 # Default style is white and bold.
114 fun format_h2
(str
: String): String do
115 if opt_nocolors
.value
then return str
119 # Format and colorize a string heading of level 3 for console output.
121 # Default style is white and nobold.
122 fun format_h3
(str
: String): String do
123 if opt_nocolors
.value
then return str
127 # Format and colorize a string heading of level 4 for console output.
129 # Default style is green.
130 fun format_h4
(str
: String): String do
131 if opt_nocolors
.value
then return str
135 # Format and colorize a string heading of level 5 for console output.
137 # Default style is light gray.
138 fun format_p
(str
: String): String do
139 if opt_nocolors
.value
then return str
140 return str
.light_gray
145 # A Metric is used to collect data about things
147 # The concept is reified here for a better organization and documentation
150 # Type of elements measured by this metric.
153 # Type of values used to measure elements.
156 # Type of data representation used to associate elements and values.
157 type RES: Map[ELM, VAL]
159 # The name of this metric (generally an acronym about the metric).
160 fun name
: String is abstract
162 # A long and understandable description about what is measured by this metric.
163 fun desc
: String is abstract
165 # Clear all results for this metric
166 fun clear
is abstract
168 # Values for each element
169 fun values
: RES is abstract
171 # Collect metric values on elements
172 fun collect
(elements
: Collection[ELM]) is abstract
174 # The value calculated for the element
175 fun [](element
: ELM): VAL do return values
[element
]
177 # Does the element have a value for this metric?
178 fun has_element
(element
: ELM): Bool do return values
.has_key
(element
)
181 fun avg
: Float is abstract
183 # Pretty print the metric results in console
184 fun to_console
(indent
: Int, colors
: Bool) do
185 if values
.is_empty
then
187 print
"{"\t" * indent}{name}: {desc} -- nothing".green
189 print
"{"\t" * indent}{name}: {desc} -- nothing"
197 print
"{"\t" * indent}{name}: {desc}".green
198 print
"{"\t" * indent} avg: {avg}".light_gray
199 print
"{"\t" * indent} max: {max} ({self[max]})".light_gray
200 print
"{"\t" * indent} min: {min} ({self[min]})".light_gray
201 print
"{"\t" * indent} std: {std_dev}".light_gray
203 print
"{"\t" * indent}{name}: {desc}"
204 print
"{"\t" * indent} avg: {avg}"
205 print
"{"\t" * indent} max: {max} ({self[max]})"
206 print
"{"\t" * indent} min: {min} ({self[min]})"
207 print
"{"\t" * indent} std: {std_dev}"
211 # The sum of all the values.
212 fun sum
: VAL is abstract
214 # The values standard derivation
215 fun std_dev
: Float is abstract
217 # The element with the highest value
218 fun max
: ELM is abstract
220 # The element with the lowest value
221 fun min
: ELM is abstract
223 # The value threshold above what elements are considered as 'interesting'
224 fun threshold
: Float do return avg
+ std_dev
226 # The set of element above the threshold
227 fun above_threshold
: Set[ELM] is abstract
229 # Sort the metric keys by values
230 fun sort
: Array[ELM] do
231 return values
.keys_sorted_by_values
(default_reverse_comparator
)
235 # A Metric that collects integer data
237 # Used to count things
241 redef type VAL: Int is fixed
242 redef type RES: Counter[ELM]
244 # `IntMetric` uses a Counter to store values in intern.
245 protected var values_cache
= new Counter[ELM]
247 redef fun values
do return values_cache
249 redef fun clear
do values_cache
.clear
251 redef fun sum
do return values_cache
.sum
254 assert not values_cache
.is_empty
255 return values_cache
.max
.as(not null)
259 assert not values_cache
.is_empty
260 return values_cache
.min
.as(not null)
264 redef fun avg
do return values_cache
.avg
266 redef fun std_dev
do return values_cache
.std_dev
268 redef fun above_threshold
do
269 var above
= new HashSet[ELM]
270 var threshold
= threshold
271 for element
, value
in values
do
272 if value
.to_f
> threshold
then above
.add
(element
)
277 redef fun to_console
(indent
, colors
) do
280 print
"{"\t" * indent} sum: {sum}".light_gray
282 print
"{"\t" * indent} sum: {sum}"
287 # A Metric that collects float datas
289 # Used sor summarization
293 redef type VAL: Float
295 # `FloatMetric` uses a Map to store values in intern.
296 protected var values_cache
= new HashMap[ELM, VAL]
298 redef fun values
do return values_cache
300 redef fun clear
do values_cache
.clear
305 for v
in values
.values
do
306 if v
.is_nan
then continue
313 assert not values
.is_empty
314 var max
: nullable Float = null
315 var elem
: nullable ELM = null
316 for e
, v
in values
do
317 if max
== null or v
> max
then
322 return elem
.as(not null)
326 assert not values
.is_empty
327 var min
: nullable Float = null
328 var elem
: nullable ELM = null
329 for e
, v
in values
do
330 if min
== null or v
< min
then
335 return elem
.as(not null)
339 if values
.is_empty
then return 0.0
340 return sum
/ values
.length
.to_f
345 for value
in values
.values
do
346 if value
.is_nan
then continue
347 sum
+= (value
- avg
).pow
(2.to_f
)
349 return (sum
/ values
.length
.to_f
).sqrt
352 redef fun above_threshold
do
353 var above
= new HashSet[ELM]
354 var threshold
= threshold
355 for element
, value
in values
do
356 if value
> threshold
then above
.add
(element
)
361 redef fun to_console
(indent
, colors
) do
364 print
"{"\t" * indent} sum: {sum}".light_gray
366 print
"{"\t" * indent} sum: {sum}"
371 # A MetricSet is a metric holder
373 # It purpose is to be extended with a metric collect service
376 # Type of element measured by this `MetricSet`.
380 var metrics
: Set[Metric] = new HashSet[Metric]
382 # Add a metric to the set
383 fun register
(metrics
: Metric...) do for metric
in metrics
do self.metrics
.add
(metric
)
385 # Clear all results for all metrics
386 fun clear
do for metric
in metrics
do metric
.clear
388 # Collect all metrics for this set of class
389 fun collect
(elements
: Set[ELM]) do
390 for metric
in metrics
do metric
.collect
(elements
)
393 # Pretty print the resuls in console
394 fun to_console
(indent
: Int, colors
: Bool) do
395 for metric
in metrics
do metric
.to_console
(indent
, colors
)
398 # Export the metric set in CSV format
399 fun to_csv
: CsvDocument do
400 var csv
= new CsvDocument
404 csv
.header
.add
("entry")
405 for metric
in metrics
do csv
.header
.add
(metric
.name
)
407 # collect all entries to merge metric results
408 var entries
= new HashSet[ELM]
409 for metric
in metrics
do
410 for entry
in metric
.values
.keys
do entries
.add
(entry
)
414 for entry
in entries
do
415 var line
= [entry
.to_s
]
416 for metric
in metrics
do
417 if metric
.has_element
(entry
) then
418 line
.add
(metric
[entry
].to_s
)
423 csv
.records
.add
(line
)