664415a5b8625c94b266a4af5f0e57f5f54f805a
[nit.git] / src / metrics / tables_metrics.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Metrics on table generation
18 module tables_metrics
19
20 import metrics_base
21
22 redef class ToolContext
23 var tables_metrics_phase: Phase = new TablesMetricsPhase(self, null)
24 end
25
26 private class TablesMetricsPhase
27 super Phase
28 redef fun process_mainmodule(mainmodule, given_mmodules)
29 do
30 if not toolcontext.opt_tables.value and not toolcontext.opt_all.value then return
31 compute_tables_metrics(mainmodule)
32 end
33 end
34
35 # Print class tables metrics for the classes of the program main
36 fun compute_tables_metrics(main: MModule)
37 do
38 var model = main.model
39
40 var nc = 0 # Number of runtime classes
41 var nl = 0 # Number of usages of class definitions (a class definition can be used more than once)
42 var nhp = 0 # Number of usages of properties (a property can be used more than once)
43 var npas = 0 # Number of usages of properties without lookup (easy easy case, easier that CHA)
44
45 # Collect the full class hierarchy
46 var hier = main.flatten_mclass_hierarchy
47 for c in hier do
48 # Skip classes without direct instances
49 if c.kind == interface_kind or c.kind == abstract_kind then continue
50
51 nc += 1
52
53 # Now, we need to collect all properties defined/inherited/imported
54 # So, visit all definitions of all super-classes
55 for sup in hier[c].greaters do
56 for cd in sup.mclassdefs do
57 nl += 1
58
59 # Now, search properties introduced
60 for p in cd.intro_mproperties do
61
62 nhp += 1
63 # Select property definition
64 if p.mpropdefs.length == 1 then
65 npas += 1
66 else
67 var sels = p.lookup_definitions(main, c.intro.bound_mtype)
68 if sels.length > 1 then
69 print "conflict for {p.full_name} in class {c.full_name}: {sels.join(", ")}"
70 else if sels.is_empty then
71 print "ERROR: no property for {p.full_name} in class {c.full_name}!"
72 end
73 end
74 end
75 end
76 end
77 end
78
79 print "--- Construction of tables ---"
80 print "Number of runtime classes: {nc} (excluding interfaces and abstract classes)"
81 print "Average number of composing class definition by runtime class: {div(nl,nc)}"
82 print "Total size of tables (classes and instances): {nhp} (not including stuff like info for subtyping or call-next-method)"
83 print "Average size of table by runtime class: {div(nhp,nc)}"
84 print "Values never redefined: {npas} ({div(npas*100,nhp)}%)"
85 end