nitstats: renamed in nitmetrics
[nit.git] / src / metrics / model_stats.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 # General statistics on a model
18 module model_stats
19
20 import model
21 private import metrics_base
22
23 # Print general statistics on a model
24 fun compute_statistics(model: Model)
25 do
26 print "--- Statistics of the model ---"
27 var nbmod = model.mmodules.length
28 print "Number of modules: {nbmod}"
29
30 print ""
31
32 var nbcla = model.mclasses.length
33 var nbcladef = model.mclassdef_hierarchy.length
34 print "Number of classes: {nbcla}"
35
36 # determine the distribution of:
37 # * class kinds (interface, abstract class, etc.)
38 # * refinex classes (vs. unrefined ones)
39 var kinds = new Counter[MClassKind]
40 var refined = 0
41 for c in model.mclasses do
42 kinds.inc(c.kind)
43 if c.mclassdefs.length > 1 then
44 refined += 1
45 end
46 end
47 for k in kinds.sort do
48 var v = kinds[k]
49 print " Number of {k} kind: {v} ({div(v*100,nbcla)}%)"
50 end
51
52
53 print ""
54
55 print "Number of class definitions: {nbcladef}"
56 print "Number of refined classes: {refined} ({div(refined*100,nbcla)}%)"
57 print "Average number of class refinments by classes: {div(nbcladef-nbcla,nbcla)}"
58 print "Average number of class refinments by refined classes: {div(nbcladef-nbcla,refined)}"
59
60 print ""
61
62 var nbprop = model.mproperties.length
63 var nbpropdef = 0
64 var redefined = 0
65 print "Number of properties: {model.mproperties.length}"
66 var pkinds = new Counter[String]
67 for p in model.mproperties do
68 nbpropdef += p.mpropdefs.length
69 if p.mpropdefs.length > 1 then
70 redefined += 1
71 end
72 pkinds.inc(p.class_name)
73 end
74 for k in pkinds.sort do
75 var v = pkinds[k]
76 print " Number of {k}: {v} ({div(v*100,nbprop)}%)"
77 end
78
79 print ""
80
81 print "Number of property definitions: {nbpropdef}"
82 print "Number of redefined properties: {redefined} ({div(redefined*100,nbprop)}%)"
83 print "Average number of property redefinitions by property: {div(nbpropdef-nbprop,nbprop)}"
84 print "Average number of property redefinitions by redefined property: {div(nbpropdef-nbprop,redefined)}"
85 end
86
87 # Print class tables statistics for the classes of the program main
88 fun compute_tables(main: MModule)
89 do
90 var model = main.model
91
92 var nc = 0 # Number of runtime classes
93 var nl = 0 # Number of usages of class definitions (a class definition can be used more than once)
94 var nhp = 0 # Number of usages of properties (a property can be used more than once)
95 var npas = 0 # Number of usages of properties without lookup (easy easy case, easier that CHA)
96
97 # Collect the full class hierarchy
98 var hier = main.flatten_mclass_hierarchy
99 for c in hier do
100 # Skip classes without direct instances
101 if c.kind == interface_kind or c.kind == abstract_kind then continue
102
103 nc += 1
104
105 # Now, we need to collect all properties defined/inherited/imported
106 # So, visit all definitions of all super-classes
107 for sup in hier[c].greaters do
108 for cd in sup.mclassdefs do
109 nl += 1
110
111 # Now, search properties introduced
112 for p in cd.intro_mproperties do
113
114 nhp += 1
115 # Select property definition
116 if p.mpropdefs.length == 1 then
117 npas += 1
118 else
119 var sels = p.lookup_definitions(main, c.mclassdefs.first.bound_mtype)
120 if sels.length > 1 then
121 print "conflict for {p.full_name} in class {c.full_name}: {sels.join(", ")}"
122 else if sels.is_empty then
123 print "ERROR: no property for {p.full_name} in class {c.full_name}!"
124 end
125 end
126 end
127 end
128 end
129 end
130
131 print "--- Construction of tables ---"
132 print "Number of runtime classes: {nc} (excluding interfaces and abstract classes)"
133 print "Average number of composing class definition by runtime class: {div(nl,nc)}"
134 print "Total size of tables (classes and instances): {nhp} (not including stuff like info for subtyping or call-next-method)"
135 print "Average size of table by runtime class: {div(nhp,nc)}"
136 print "Values never redefined: {npas} ({div(npas*100,nhp)}%)"
137 end