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.
27 redef class ToolContext
30 var opt_all
= new OptionBool("Compute all metrics", "--all")
33 var opt_mmodules
= new OptionBool("Compute metrics about mmodules", "--mmodules")
35 var opt_mclasses
= new OptionBool("Compute metrics about mclasses", "--mclasses")
37 var opt_mendel
= new OptionBool("Compute mendel metrics", "--mendel")
39 var opt_inheritance
= new OptionBool("Compute metrics about inheritance usage", "--inheritance")
41 var opt_refinement
= new OptionBool("Compute metrics about refinement usage", "--refinement")
43 var opt_self
= new OptionBool("Compute metrics about the usage of explicit and implicit self", "--self")
45 var opt_ast
= new OptionBool("Compute metrics about the usage of nodes and identifiers in the AST", "--ast")
47 var opt_nullables
= new OptionBool("Compute metrics on nullables send", "--nullables")
49 var opt_static_types
= new OptionBool("Compute explicit static types metrics", "--static-types")
51 var opt_tables
= new OptionBool("Compute tables metrics", "--tables")
53 var opt_rta
= new OptionBool("Compute RTA metrics", "--rta")
55 var opt_csv
= new OptionBool("Export metrics in CSV format", "--csv")
57 var opt_generate_hyperdoc
= new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
59 var opt_poset
= new OptionBool("Complete metrics on posets", "--poset")
62 var opt_nocolors
= new OptionBool("Disable colors in console outputs", "--no-colors")
65 var opt_dir
= new OptionString("Directory where some statistics files are generated", "-d", "--dir")
66 var output_dir
: String = "."
71 self.option_context
.add_option
(opt_all
)
72 self.option_context
.add_option
(opt_mmodules
)
73 self.option_context
.add_option
(opt_mclasses
)
74 self.option_context
.add_option
(opt_mendel
)
75 self.option_context
.add_option
(opt_inheritance
)
76 self.option_context
.add_option
(opt_refinement
)
77 self.option_context
.add_option
(opt_self
)
78 self.option_context
.add_option
(opt_ast
)
79 self.option_context
.add_option
(opt_nullables
)
80 self.option_context
.add_option
(opt_static_types
)
81 self.option_context
.add_option
(opt_tables
)
82 self.option_context
.add_option
(opt_rta
)
83 self.option_context
.add_option
(opt_csv
)
84 self.option_context
.add_option
(opt_generate_hyperdoc
)
85 self.option_context
.add_option
(opt_poset
)
86 self.option_context
.add_option
(opt_dir
)
87 self.option_context
.add_option
(opt_nocolors
)
90 redef fun process_options
(args
)
93 var val
= self.opt_dir
.value
95 val
= val
.simplify_path
101 # colorize heading 1 for console output
102 fun format_h1
(str
: String): String do
103 if opt_nocolors
.value
then return str
104 return str
.yellow
.bold
107 fun format_h2
(str
: String): String do
108 if opt_nocolors
.value
then return str
112 fun format_h3
(str
: String): String do
113 if opt_nocolors
.value
then return str
117 fun format_h4
(str
: String): String do
118 if opt_nocolors
.value
then return str
122 fun format_p
(str
: String): String do
123 if opt_nocolors
.value
then return str
124 return str
.light_gray
130 # is the class imported from standard lib?
131 fun is_standard
: Bool do
132 return self.intro_mmodule
.mgroup
.mproject
.name
== "standard"
137 # is the module imported from standard lib?
138 fun is_standard
: Bool do
139 return self.mgroup
.mproject
.name
== "standard"
143 # A Metric is used to collect data about things
145 # The concept is reified here for a better organization and documentation
149 type RES: Map[ELM, VAL]
151 fun name
: String is abstract
152 fun desc
: String is abstract
154 # Clear all results for this metric
155 fun clear
is abstract
157 # Values for each element
158 fun values
: RES is abstract
160 # Collect metric values on elements
161 fun collect
(elements
: Set[ELM]) is abstract
163 # The value calculated for the element
164 fun [](element
: ELM): VAL do return values
[element
]
166 # Does the element have a value for this metric?
167 fun has_element
(element
: ELM): Bool do return values
.has_key
(element
)
170 fun avg
: Float is abstract
172 # Pretty print the metric results in console
173 fun to_console
(indent
: Int, colors
: Bool) do
174 if values
.is_empty
then
176 print
"{"\t" * indent}{name}: {desc} -- nothing".green
178 print
"{"\t" * indent}{name}: {desc} -- nothing"
186 print
"{"\t" * indent}{name}: {desc}".green
187 print
"{"\t" * indent} avg: {avg}".light_gray
188 print
"{"\t" * indent} max: {max} ({self[max]})".light_gray
189 print
"{"\t" * indent} min: {min} ({self[min]})".light_gray
190 print
"{"\t" * indent} std: {std_dev}".light_gray
192 print
"{"\t" * indent}{name}: {desc}"
193 print
"{"\t" * indent} avg: {avg}"
194 print
"{"\t" * indent} max: {max} ({self[max]})"
195 print
"{"\t" * indent} min: {min} ({self[min]})"
196 print
"{"\t" * indent} std: {std_dev}"
200 # The values standard derivation
201 fun std_dev
: Float is abstract
203 # The element with the highest value
204 fun max
: ELM is abstract
206 # The element with the lowest value
207 fun min
: ELM is abstract
209 # The value threshold above what elements are considered as 'interesting'
210 fun threshold
: Float do return avg
+ std_dev
212 # The set of element above the threshold
213 fun above_threshold
: Set[ELM] is abstract
216 # A Metric that collects integer data
218 # Used to count things
223 redef type RES: Counter[ELM]
225 protected var values_cache
= new Counter[ELM]
226 redef fun values
do return values_cache
228 redef fun clear
do values_cache
.clear
230 fun sum
: Int do return values_cache
.sum
233 assert not values_cache
.is_empty
234 return values_cache
.max
.as(not null)
238 assert not values_cache
.is_empty
239 return values_cache
.min
.as(not null)
243 redef fun avg
: Float do return values_cache
.avg
245 redef fun std_dev
: Float do return values_cache
.std_dev
247 redef fun above_threshold
do
248 var above
= new HashSet[ELM]
249 var threshold
= threshold
250 for element
, value
in values
do
251 if value
.to_f
> threshold
then above
.add
(element
)
256 redef fun to_console
(indent
, colors
) do
259 print
"{"\t" * indent} sum: {sum}".light_gray
261 print
"{"\t" * indent} sum: {sum}"
266 # A Metric that collects float datas
268 # Used sor summarization
272 redef type VAL: Float
274 protected var values_cache
= new HashMap[ELM, VAL]
275 redef fun values
do return values_cache
277 redef fun clear
do values_cache
.clear
281 for v
in values
.values
do sum
+= v
286 assert not values
.is_empty
287 var max
: nullable Float = null
288 var elem
: nullable ELM = null
289 for e
, v
in values
do
290 if max
== null or v
> max
then
295 return elem
.as(not null)
299 assert not values
.is_empty
300 var min
: nullable Float = null
301 var elem
: nullable ELM = null
302 for e
, v
in values
do
303 if min
== null or v
< min
then
308 return elem
.as(not null)
312 if values
.is_empty
then return 0.0
313 return sum
/ values
.length
.to_f
316 redef fun std_dev
: Float do
318 for value
in values
.values
do
319 sum
+= (value
- avg
).pow
(2.to_f
)
321 return (sum
/ values
.length
.to_f
).sqrt
324 redef fun above_threshold
do
325 var above
= new HashSet[ELM]
326 var threshold
= threshold
327 for element
, value
in values
do
328 if value
> threshold
then above
.add
(element
)
333 redef fun to_console
(indent
, colors
) do
336 print
"{"\t" * indent} sum: {sum}".light_gray
338 print
"{"\t" * indent} sum: {sum}"
344 # A MetricSet is a metric holder
346 # It purpose is to be extended with a metric collect service
351 var metrics
: Set[Metric] = new HashSet[Metric]
353 # Add a metric to the set
354 fun register
(metrics
: Metric...) do for metric
in metrics
do self.metrics
.add
(metric
)
356 # Clear all results for all metrics
357 fun clear
do for metric
in metrics
do metric
.clear
359 # Collect all metrics for this set of class
360 fun collect
(elements
: Set[ELM]) do
361 for metric
in metrics
do metric
.collect
(elements
)
364 # Pretty print the resuls in console
365 fun to_console
(indent
: Int, colors
: Bool) do
366 for metric
in metrics
do metric
.to_console
(indent
, colors
)
369 # Export the metric set in CSV format
370 fun to_csv
: CsvDocument do
371 var csv
= new CsvDocument
373 csv
.format
= new CsvFormat('"', ';', "\n")
376 csv
.header
.add
("entry")
377 for metric
in metrics
do csv
.header
.add
(metric
.name
)
379 # collect all entries to merge metric results
380 var entries
= new HashSet[ELM]
381 for metric
in metrics
do
382 for entry
in metric
.values
.keys
do entries
.add
(entry
)
386 for entry
in entries
do
387 var line
= [entry
.to_s
]
388 for metric
in metrics
do
389 if metric
.has_element
(entry
) then
390 line
.add
(metric
[entry
].to_s
)
395 csv
.records
.add
(line
)