tests: add deserialization test
[nit.git] / src / metrics / rta_metrics.nit
index 7ba1899..4752396 100644 (file)
@@ -20,36 +20,33 @@ module rta_metrics
 import modelbuilder
 private import rapid_type_analysis
 private import metrics_base
+import frontend
 
-redef class RapidTypeAnalysis
-       redef fun add_type(mtype)
-       do
-               mtype.nlvt += 1
-               mtype.mclass.nlvt += 1
-               mtype.mclass.live_types.add(mtype)
-               super(mtype)
-       end
+redef class ToolContext
+       var rta_metrics_phase = new RTAMetricsPhase(self, null)
+end
 
-       redef fun add_cast_type(mtype)
+private class RTAMetricsPhase
+       super Phase
+       redef fun process_mainmodule(mainmodule)
        do
-               mtype.nlct += 1
-               mtype.mclass.nlct += 1
-               mtype.mclass.cast_types.add(mtype)
-               super(mtype)
+               if not toolcontext.opt_rta.value and not toolcontext.opt_all.value then return
+               compute_rta_metrics(toolcontext.modelbuilder, mainmodule)
        end
 end
 
+
 redef class MType
-       var nlvt: Int = 0
-       var nlct: Int = 0
+       private var nlvt: Int = 0
+       private var nlct: Int = 0
 
-       fun is_user_defined: Bool do
+       private fun is_user_defined: Bool do
                var mtype = self
                if mtype isa MNullableType then mtype = mtype.mtype
                return self.as(MClassType).mclass.is_user_defined
        end
 
-       fun get_depth: Int do
+       private fun get_depth: Int do
                var mtype = self
                if mtype isa MNullableType then mtype = mtype.mtype
                if not mtype isa MGenericType then return 0
@@ -63,10 +60,10 @@ redef class MType
 end
 
 redef class MClass
-       var nlvt: Int = 0
-       var nlct: Int = 0
-       var live_types: Set[MType] = new HashSet[MType]
-       var cast_types: Set[MType] = new HashSet[MType]
+       private var nlvt: Int = 0
+       private var nlct: Int = 0
+       private var live_types: Set[MType] = new HashSet[MType]
+       private var cast_types: Set[MType] = new HashSet[MType]
 end
 
 # Run a runtime type analysis and print metrics
@@ -93,6 +90,8 @@ do
        for mtype in analysis.live_types do
                mtypes.add(mtype)
                nlvt += 1
+               mtype.mclass.nlvt += 1
+               mtype.mclass.live_types.add(mtype)
                if mtype isa MGenericType then nlvtg += 1
                if mtype.is_user_defined then
                        nlvtudud += 1
@@ -106,6 +105,8 @@ do
        for mtype in analysis.live_cast_types do
                mtypes.add(mtype)
                nlct += 1
+               mtype.mclass.nlct += 1
+               mtype.mclass.cast_types.add(mtype)
                if mtype isa MGenericType then nlctg += 1
                if mtype.is_user_defined then
                        nlctudud += 1
@@ -158,12 +159,23 @@ do
        end
 
        print "--- RTA metrics ---"
+       print "Number of live runtime classes: {analysis.live_classes.length}"
+       if analysis.live_classes.length < 8 then print "\t{analysis.live_classes.join(" ")}"
        print "Number of live runtime types (instantied resolved type): {analysis.live_types.length}"
        if analysis.live_types.length < 8 then print "\t{analysis.live_types.join(" ")}"
+       print "Number of live methods: {analysis.live_methods.length}"
+       if analysis.live_methods.length < 8 then print "\t{analysis.live_methods.join(" ")}"
        print "Number of live method definitions: {analysis.live_methoddefs.length}"
        if analysis.live_methoddefs.length < 8 then print "\t{analysis.live_methoddefs.join(" ")}"
-       print "Number of live customized method definitions: {analysis.live_customized_methoddefs.length}"
-       if analysis.live_customized_methoddefs.length < 8 then print "\t{analysis.live_customized_methoddefs.join(" ")}"
        print "Number of live runtime cast types (ie used in as and isa): {analysis.live_cast_types.length}"
        if analysis.live_cast_types.length < 8 then print "\t{analysis.live_cast_types.join(" ")}"
+
+       var x = 0
+       for p in analysis.live_methods do
+               for d in p.mpropdefs do
+                       if analysis.live_methoddefs.has(d) or d.is_abstract then continue
+                       x += 1
+               end
+       end
+       print "Number of dead method definitions of live methods: {x}"
 end