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("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")
61 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")
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 # colorize heading 1 for console output
101 fun format_h1
(str
: String): String do
102 if opt_nocolors
.value
then return str
103 return str
.yellow
.bold
106 fun format_h2
(str
: String): String do
107 if opt_nocolors
.value
then return str
111 fun format_h3
(str
: String): String do
112 if opt_nocolors
.value
then return str
116 fun format_h4
(str
: String): String do
117 if opt_nocolors
.value
then return str
121 fun format_p
(str
: String): String do
122 if opt_nocolors
.value
then return str
123 return str
.light_gray
129 # is the class imported from standard lib?
130 fun is_standard
: Bool do
131 return self.intro_mmodule
.mgroup
.mproject
.name
== "standard"
136 # is the module imported from standard lib?
137 fun is_standard
: Bool do
138 return self.mgroup
.mproject
.name
== "standard"
142 # A Metric is used to collect data about things
144 # The concept is reified here for a better organization and documentation
148 type RES: Map[ELM, VAL]
150 fun name
: String is abstract
151 fun desc
: String is abstract
153 # Clear all results for this metric
154 fun clear
is abstract
156 # Values for each element
157 fun values
: RES is abstract
159 # Collect metric values on elements
160 fun collect
(elements
: Set[ELM]) is abstract
162 # The value calculated for the element
163 fun [](element
: ELM): VAL do return values
[element
]
165 # Does the element have a value for this metric?
166 fun has_element
(element
: ELM): Bool do return values
.has_key
(element
)
169 fun avg
: Float is abstract
171 # Pretty print the metric results in console
172 fun to_console
(indent
: Int, colors
: Bool) do
173 if values
.is_empty
then
175 print
"{"\t" * indent}{name}: {desc} -- nothing".green
177 print
"{"\t" * indent}{name}: {desc} -- nothing"
185 print
"{"\t" * indent}{name}: {desc}".green
186 print
"{"\t" * indent} avg: {avg}".light_gray
187 print
"{"\t" * indent} max: {max} ({self[max]})".light_gray
188 print
"{"\t" * indent} min: {min} ({self[min]})".light_gray
189 print
"{"\t" * indent} std: {std_dev}".light_gray
191 print
"{"\t" * indent}{name}: {desc}"
192 print
"{"\t" * indent} avg: {avg}"
193 print
"{"\t" * indent} max: {max} ({self[max]})"
194 print
"{"\t" * indent} min: {min} ({self[min]})"
195 print
"{"\t" * indent} std: {std_dev}"
199 # The values standard derivation
200 fun std_dev
: Float is abstract
202 # The element with the highest value
203 fun max
: ELM is abstract
205 # The element with the lowest value
206 fun min
: ELM is abstract
208 # The value threshold above what elements are considered as 'interesting'
209 fun threshold
: Float do return avg
+ std_dev
211 # The set of element above the threshold
212 fun above_threshold
: Set[ELM] is abstract
215 # A Metric that collects integer data
217 # Used to count things
222 redef type RES: Counter[ELM]
224 protected var values_cache
= new Counter[ELM]
225 redef fun values
do return values_cache
227 redef fun clear
do values_cache
.clear
229 fun sum
: Int do return values_cache
.sum
232 assert not values_cache
.is_empty
233 return values_cache
.max
.as(not null)
237 assert not values_cache
.is_empty
238 return values_cache
.min
.as(not null)
242 redef fun avg
: Float do return values_cache
.avg
244 redef fun std_dev
: Float do return values_cache
.std_dev
246 redef fun above_threshold
do
247 var above
= new HashSet[ELM]
248 var threshold
= threshold
249 for element
, value
in values
do
250 if value
.to_f
> threshold
then above
.add
(element
)
255 redef fun to_console
(indent
, colors
) do
258 print
"{"\t" * indent} sum: {sum}".light_gray
260 print
"{"\t" * indent} sum: {sum}"
265 # A Metric that collects float datas
267 # Used sor summarization
271 redef type VAL: Float
273 protected var values_cache
= new HashMap[ELM, VAL]
274 redef fun values
do return values_cache
276 redef fun clear
do values_cache
.clear
280 for v
in values
.values
do sum
+= v
285 assert not values
.is_empty
286 var max
: nullable Float = null
287 var elem
: nullable ELM = null
288 for e
, v
in values
do
289 if max
== null or v
> max
then
294 return elem
.as(not null)
298 assert not values
.is_empty
299 var min
: nullable Float = null
300 var elem
: nullable ELM = null
301 for e
, v
in values
do
302 if min
== null or v
< min
then
307 return elem
.as(not null)
311 if values
.is_empty
then return 0.0
312 return sum
/ values
.length
.to_f
315 redef fun std_dev
: Float do
317 for value
in values
.values
do
318 sum
+= (value
- avg
).pow
(2.to_f
)
320 return (sum
/ values
.length
.to_f
).sqrt
323 redef fun above_threshold
do
324 var above
= new HashSet[ELM]
325 var threshold
= threshold
326 for element
, value
in values
do
327 if value
> threshold
then above
.add
(element
)
332 redef fun to_console
(indent
, colors
) do
335 print
"{"\t" * indent} sum: {sum}".light_gray
337 print
"{"\t" * indent} sum: {sum}"
343 # A MetricSet is a metric holder
345 # It purpose is to be extended with a metric collect service
350 var metrics
: Set[Metric] = new HashSet[Metric]
352 # Add a metric to the set
353 fun register
(metrics
: Metric...) do for metric
in metrics
do self.metrics
.add
(metric
)
355 # Clear all results for all metrics
356 fun clear
do for metric
in metrics
do metric
.clear
358 # Collect all metrics for this set of class
359 fun collect
(elements
: Set[ELM]) do
360 for metric
in metrics
do metric
.collect
(elements
)
363 # Pretty print the resuls in console
364 fun to_console
(indent
: Int, colors
: Bool) do
365 for metric
in metrics
do metric
.to_console
(indent
, colors
)
368 # Export the metric set in CSV format
369 fun to_csv
: CSVDocument do
370 var csv
= new CSVDocument
373 csv
.header
.add
("entry")
374 for metric
in metrics
do csv
.header
.add
(metric
.name
)
376 # collect all entries to merge metric results
377 var entries
= new HashSet[ELM]
378 for metric
in metrics
do
379 for entry
in metric
.values
.keys
do entries
.add
(entry
)
383 for entry
in entries
do
384 var line
= [entry
.to_s
]
385 for metric
in metrics
do
386 if metric
.has_element
(entry
) then
387 line
.add
(metric
[entry
].to_s
)