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_csv
= new OptionBool("Also export metrics in CSV format", "--csv")
56 var opt_generate_hyperdoc
= new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
58 var opt_poset
= new OptionBool("Complete metrics on posets", "--poset")
60 var opt_nocolors
= new OptionBool("Disable colors in console outputs", "--no-colors")
62 var opt_dir
= new OptionString("Directory where some statistics files are generated", "-d", "--dir")
64 # Output directory for metrics files.
65 var output_dir
: String = "."
70 self.option_context
.add_option
(opt_all
)
71 self.option_context
.add_option
(opt_mmodules
)
72 self.option_context
.add_option
(opt_mclasses
)
73 self.option_context
.add_option
(opt_mendel
)
74 self.option_context
.add_option
(opt_inheritance
)
75 self.option_context
.add_option
(opt_refinement
)
76 self.option_context
.add_option
(opt_self
)
77 self.option_context
.add_option
(opt_ast
)
78 self.option_context
.add_option
(opt_nullables
)
79 self.option_context
.add_option
(opt_static_types
)
80 self.option_context
.add_option
(opt_tables
)
81 self.option_context
.add_option
(opt_rta
)
82 self.option_context
.add_option
(opt_csv
)
83 self.option_context
.add_option
(opt_generate_hyperdoc
)
84 self.option_context
.add_option
(opt_poset
)
85 self.option_context
.add_option
(opt_dir
)
86 self.option_context
.add_option
(opt_nocolors
)
89 redef fun process_options
(args
)
92 var val
= self.opt_dir
.value
94 val
= val
.simplify_path
100 # Format and colorize a string heading of level 1 for console output.
102 # Default style is yellow and bold.
103 fun format_h1
(str
: String): String do
104 if opt_nocolors
.value
then return str
105 return str
.yellow
.bold
108 # Format and colorize a string heading of level 2 for console output.
110 # Default style is white and bold.
111 fun format_h2
(str
: String): String do
112 if opt_nocolors
.value
then return str
116 # Format and colorize a string heading of level 3 for console output.
118 # Default style is white and nobold.
119 fun format_h3
(str
: String): String do
120 if opt_nocolors
.value
then return str
124 # Format and colorize a string heading of level 4 for console output.
126 # Default style is green.
127 fun format_h4
(str
: String): String do
128 if opt_nocolors
.value
then return str
132 # Format and colorize a string heading of level 5 for console output.
134 # Default style is light gray.
135 fun format_p
(str
: String): String do
136 if opt_nocolors
.value
then return str
137 return str
.light_gray
142 # A Metric is used to collect data about things
144 # The concept is reified here for a better organization and documentation
147 # Type of elements measured by this metric.
150 # Type of values used to measure elements.
153 # Type of data representation used to associate elements and values.
154 type RES: Map[ELM, VAL]
156 # The name of this metric (generally an acronym about the metric).
157 fun name
: String is abstract
159 # A long and understandable description about what is measured by this metric.
160 fun desc
: String is abstract
162 # Clear all results for this metric
163 fun clear
is abstract
165 # Values for each element
166 fun values
: RES is abstract
168 # Collect metric values on elements
169 fun collect
(elements
: Collection[ELM]) is abstract
171 # The value calculated for the element
172 fun [](element
: ELM): VAL do return values
[element
]
174 # Does the element have a value for this metric?
175 fun has_element
(element
: ELM): Bool do return values
.has_key
(element
)
178 fun avg
: Float is abstract
180 # Pretty print the metric results in console
181 fun to_console
(indent
: Int, colors
: Bool) do
182 if values
.is_empty
then
184 print
"{"\t" * indent}{name}: {desc} -- nothing".green
186 print
"{"\t" * indent}{name}: {desc} -- nothing"
194 print
"{"\t" * indent}{name}: {desc}".green
195 print
"{"\t" * indent} avg: {avg}".light_gray
196 print
"{"\t" * indent} max: {max} ({self[max]})".light_gray
197 print
"{"\t" * indent} min: {min} ({self[min]})".light_gray
198 print
"{"\t" * indent} std: {std_dev}".light_gray
200 print
"{"\t" * indent}{name}: {desc}"
201 print
"{"\t" * indent} avg: {avg}"
202 print
"{"\t" * indent} max: {max} ({self[max]})"
203 print
"{"\t" * indent} min: {min} ({self[min]})"
204 print
"{"\t" * indent} std: {std_dev}"
208 # The sum of all the values.
209 fun sum
: VAL is abstract
211 # The values standard derivation
212 fun std_dev
: Float is abstract
214 # The element with the highest value
215 fun max
: ELM is abstract
217 # The element with the lowest value
218 fun min
: ELM is abstract
220 # The value threshold above what elements are considered as 'interesting'
221 fun threshold
: Float do return avg
+ std_dev
223 # The set of element above the threshold
224 fun above_threshold
: Set[ELM] is abstract
226 # Sort the metric keys by values
227 fun sort
: Array[ELM] do
228 return values
.keys_sorted_by_values
(default_reverse_comparator
)
232 # A Metric that collects integer data
234 # Used to count things
239 redef type RES: Counter[ELM]
241 # `IntMetric` uses a Counter to store values in intern.
242 protected var values_cache
= new Counter[ELM]
244 redef fun values
do return values_cache
246 redef fun clear
do values_cache
.clear
248 redef fun sum
do return values_cache
.sum
251 assert not values_cache
.is_empty
252 return values_cache
.max
.as(not null)
256 assert not values_cache
.is_empty
257 return values_cache
.min
.as(not null)
261 redef fun avg
do return values_cache
.avg
263 redef fun std_dev
do return values_cache
.std_dev
265 redef fun above_threshold
do
266 var above
= new HashSet[ELM]
267 var threshold
= threshold
268 for element
, value
in values
do
269 if value
.to_f
> threshold
then above
.add
(element
)
274 redef fun to_console
(indent
, colors
) do
277 print
"{"\t" * indent} sum: {sum}".light_gray
279 print
"{"\t" * indent} sum: {sum}"
284 # A Metric that collects float datas
286 # Used sor summarization
290 redef type VAL: Float
292 # `FloatMetric` uses a Map to store values in intern.
293 protected var values_cache
= new HashMap[ELM, VAL]
295 redef fun values
do return values_cache
297 redef fun clear
do values_cache
.clear
302 for v
in values
.values
do
303 if v
.is_nan
then continue
310 assert not values
.is_empty
311 var max
: nullable Float = null
312 var elem
: nullable ELM = null
313 for e
, v
in values
do
314 if max
== null or v
> max
then
319 return elem
.as(not null)
323 assert not values
.is_empty
324 var min
: nullable Float = null
325 var elem
: nullable ELM = null
326 for e
, v
in values
do
327 if min
== null or v
< min
then
332 return elem
.as(not null)
336 if values
.is_empty
then return 0.0
337 return sum
/ values
.length
.to_f
342 for value
in values
.values
do
343 if value
.is_nan
then continue
344 sum
+= (value
- avg
).pow
(2.to_f
)
346 return (sum
/ values
.length
.to_f
).sqrt
349 redef fun above_threshold
do
350 var above
= new HashSet[ELM]
351 var threshold
= threshold
352 for element
, value
in values
do
353 if value
> threshold
then above
.add
(element
)
358 redef fun to_console
(indent
, colors
) do
361 print
"{"\t" * indent} sum: {sum}".light_gray
363 print
"{"\t" * indent} sum: {sum}"
368 # A MetricSet is a metric holder
370 # It purpose is to be extended with a metric collect service
373 # Type of element measured by this `MetricSet`.
377 var metrics
: Set[Metric] = new HashSet[Metric]
379 # Add a metric to the set
380 fun register
(metrics
: Metric...) do for metric
in metrics
do self.metrics
.add
(metric
)
382 # Clear all results for all metrics
383 fun clear
do for metric
in metrics
do metric
.clear
385 # Collect all metrics for this set of class
386 fun collect
(elements
: Set[ELM]) do
387 for metric
in metrics
do metric
.collect
(elements
)
390 # Pretty print the resuls in console
391 fun to_console
(indent
: Int, colors
: Bool) do
392 for metric
in metrics
do metric
.to_console
(indent
, colors
)
395 # Export the metric set in CSV format
396 fun to_csv
: CsvDocument do
397 var csv
= new CsvDocument
401 csv
.header
.add
("entry")
402 for metric
in metrics
do csv
.header
.add
(metric
.name
)
404 # collect all entries to merge metric results
405 var entries
= new HashSet[ELM]
406 for metric
in metrics
do
407 for entry
in metric
.values
.keys
do entries
.add
(entry
)
411 for entry
in entries
do
412 var line
= [entry
.to_s
]
413 for metric
in metrics
do
414 if metric
.has_element
(entry
) then
415 line
.add
(metric
[entry
].to_s
)
420 csv
.records
.add
(line
)