Metrics from RTA

Introduced classes

class CNLC

nitc :: CNLC

Class Metric: Number of Live Cast
class CNLI

nitc :: CNLI

Class Metric: Number of Live Instances
class MNCT

nitc :: MNCT

MModule Metric: Number of Live Cast Types
class MNLC

nitc :: MNLC

MModule Metric: Number of Live Classes
class MNLDD

nitc :: MNLDD

MModule Metric: Number of Dead MethodDefs
class MNLI

nitc :: MNLI

MModule Metric: Number of Live Types
class MNLM

nitc :: MNLM

MModule Metric: Number of Live Methods
class MNLMD

nitc :: MNLMD

MModule Metric: Number of Live MethodDefs
class MNLT

nitc :: MNLT

MModule Metric: Number of Live Types
interface MTypeMetric

nitc :: MTypeMetric

A metric about MType
class RTAMetric

nitc :: RTAMetric

RTA related metric that needs a modelbuilder
private class RTAMetricsPhase

nitc :: RTAMetricsPhase

class TAGS

nitc :: TAGS

Type Metric: Arity of Generic Signature
class TDGS

nitc :: TDGS

Type Metric: Depth of Generic Signature
class TNLC

nitc :: TNLC

Type Metric: Number of Live Cast
class TNLI

nitc :: TNLI

Type Metric: Number of Live Instances

Redefined classes

redef abstract class MType

nitc :: rta_metrics $ MType

A global static type
redef class ToolContext

nitc :: rta_metrics $ ToolContext

Global context for tools

All class definitions

class CNLC

nitc $ CNLC

Class Metric: Number of Live Cast
class CNLI

nitc $ CNLI

Class Metric: Number of Live Instances
class MNCT

nitc $ MNCT

MModule Metric: Number of Live Cast Types
class MNLC

nitc $ MNLC

MModule Metric: Number of Live Classes
class MNLDD

nitc $ MNLDD

MModule Metric: Number of Dead MethodDefs
class MNLI

nitc $ MNLI

MModule Metric: Number of Live Types
class MNLM

nitc $ MNLM

MModule Metric: Number of Live Methods
class MNLMD

nitc $ MNLMD

MModule Metric: Number of Live MethodDefs
class MNLT

nitc $ MNLT

MModule Metric: Number of Live Types
redef abstract class MType

nitc :: rta_metrics $ MType

A global static type
interface MTypeMetric

nitc $ MTypeMetric

A metric about MType
class RTAMetric

nitc $ RTAMetric

RTA related metric that needs a modelbuilder
class TAGS

nitc $ TAGS

Type Metric: Arity of Generic Signature
class TDGS

nitc $ TDGS

Type Metric: Depth of Generic Signature
class TNLC

nitc $ TNLC

Type Metric: Number of Live Cast
class TNLI

nitc $ TNLI

Type Metric: Number of Live Instances
redef class ToolContext

nitc :: rta_metrics $ ToolContext

Global context for tools
package_diagram nitc::rta_metrics rta_metrics nitc::rapid_type_analysis rapid_type_analysis nitc::rta_metrics->nitc::rapid_type_analysis nitc::mmodules_metrics mmodules_metrics nitc::rta_metrics->nitc::mmodules_metrics nitc::mclasses_metrics mclasses_metrics nitc::rta_metrics->nitc::mclasses_metrics nitc\>semantize\> semantize nitc::rapid_type_analysis->nitc\>semantize\> nitc::explain_assert_api explain_assert_api nitc::rapid_type_analysis->nitc::explain_assert_api csv csv nitc::rapid_type_analysis->csv nitc::metrics_base metrics_base nitc::mmodules_metrics->nitc::metrics_base nitc::model_collect model_collect nitc::mmodules_metrics->nitc::model_collect nitc::mclasses_metrics->nitc::metrics_base nitc::mclasses_metrics->nitc::model_collect ...nitc\>semantize\> ... ...nitc\>semantize\>->nitc\>semantize\> ...nitc::explain_assert_api ... ...nitc::explain_assert_api->nitc::explain_assert_api ...csv ... ...csv->csv ...nitc::metrics_base ... ...nitc::metrics_base->nitc::metrics_base ...nitc::model_collect ... ...nitc::model_collect->nitc::model_collect nitc::metrics metrics nitc::metrics->nitc::rta_metrics nitc::nitmetrics nitmetrics nitc::nitmetrics->nitc::metrics nitc::api_metrics api_metrics nitc::api_metrics->nitc::metrics nitc::nitmetrics... ... nitc::nitmetrics...->nitc::nitmetrics nitc::api_metrics... ... nitc::api_metrics...->nitc::api_metrics

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module annotation

nitc :: annotation

Management and utilities on annotations
module array

core :: array

This module introduces the standard array structure.
module auto_super_init

nitc :: auto_super_init

Computing of super-constructors that must be implicitly called at the begin of constructors.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module caching

serialization :: caching

Services for caching serialization engines
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module console

console :: console

Defines some ANSI Terminal Control Escape Sequences.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module counter

counter :: counter

Simple numerical statistical analysis and presentation
module csv

csv :: csv

CSV document handling.
module digraph

graph :: digraph

Implementation of directed graphs, also called digraphs.
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module explain_assert_api

nitc :: explain_assert_api

Explain failed assert to the console (service declaration only)
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module flow

nitc :: flow

Intraprocedural static flow.
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module ini

ini :: ini

Read and write INI configuration files
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module lexer

nitc :: lexer

Lexer and its tokens.
module lexer_work

nitc :: lexer_work

Internal algorithm and data structures for the Nit lexer
module list

core :: list

This module handle double linked lists
module literal

nitc :: literal

Parsing of literal values in the abstract syntax tree.
module loader

nitc :: loader

Loading of Nit source files
module local_var_init

nitc :: local_var_init

Verify that local variables are initialized before their usage
module location

nitc :: location

Nit source-file and locations in source-file
module math

core :: math

Mathematical operations
module mdoc

nitc :: mdoc

Documentation of model entities
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module metrics_base

nitc :: metrics_base

Helpers for various statistics tools.
module mmodule

nitc :: mmodule

modules and module hierarchies in the metamodel
module mmodule_data

nitc :: mmodule_data

Define and retrieve data in modules
module model

nitc :: model

Classes, types and properties
module model_base

nitc :: model_base

The abstract concept of model and related common things
module model_collect

nitc :: model_collect

Collect things from the model.
module model_examples

nitc :: model_examples

Examples for Model entities
module modelbuilder_base

nitc :: modelbuilder_base

Load nit source files and build the associated model
module modelize

nitc :: modelize

Create a model from nit source files
module modelize_class

nitc :: modelize_class

Analysis and verification of class definitions to instantiate model element
module modelize_property

nitc :: modelize_property

Analysis and verification of property definitions to instantiate model element
module more_collections

more_collections :: more_collections

Highly specific, but useful, collections-related classes.
module mpackage

nitc :: mpackage

Modelisation of a Nit package
module native

core :: native

Native structures for text and bytes
module nitpm_shared

nitc :: nitpm_shared

Services related to the Nit package manager
module numeric

core :: numeric

Advanced services for Numeric types
module opts

opts :: opts

Management of options on the command line
module ordered_tree

ordered_tree :: ordered_tree

Manipulation and presentation of ordered trees.
module parse_annotations

nitc :: parse_annotations

Simple annotation parsing
module parser

nitc :: parser

Parser.
module parser_nodes

nitc :: parser_nodes

AST nodes of the Nit language
module parser_prod

nitc :: parser_prod

Production AST nodes full definition.
module parser_work

nitc :: parser_work

Internal algorithm and data structures for the Nit parser
module phase

nitc :: phase

Phases of the processing of nit programs
module poset

poset :: poset

Pre order sets and partial order set (ie hierarchies)
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module scope

nitc :: scope

Identification and scoping of local variables and labels.
module semantize

nitc :: semantize

Process bodies of methods in regard with the model.
module serialization

serialization :: serialization

General serialization services
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module tables

nitc :: tables

Module that interfaces the parsing tables.
module template

template :: template

Basic template system
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module toolcontext

nitc :: toolcontext

Common command-line tool infrastructure than handle options and error messages
module typing

nitc :: typing

Intraprocedural resolution of static types and OO-services
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O
module version

nitc :: version

This file was generated by git-gen-version.sh

Parents

module mclasses_metrics

nitc :: mclasses_metrics

Collect common metrics about mclasses
module mmodules_metrics

nitc :: mmodules_metrics

Collect common metrics about modules
module rapid_type_analysis

nitc :: rapid_type_analysis

Rapid type analysis on the AST

Children

module metrics

nitc :: metrics

Various statistics about Nit models and programs

Descendants

module a_star-m

a_star-m

module api

nitc :: api

Components required to build a web server about the nit model.
module nitmetrics

nitc :: nitmetrics

A program that collects various metrics on nit programs and libraries
module nitweb

nitc :: nitweb

Runs a webserver based on nitcorn that render things from model.
# Metrics from RTA
module rta_metrics

private import rapid_type_analysis
import metrics_base
import mmodules_metrics
import mclasses_metrics

redef class ToolContext

	# RTA related metrics phase
	var rta_metrics_phase: Phase = new RTAMetricsPhase(self, null)
end

private class RTAMetricsPhase
	super Phase
	redef fun process_mainmodule(mainmodule, given_mmodules)
	do
		if not toolcontext.opt_rta.value and not toolcontext.opt_all.value then return
		var csv = toolcontext.opt_csv.value
		var out = "{toolcontext.opt_dir.value or else "metrics"}/rta"
		out.mkdir

		var model = toolcontext.modelbuilder.model
		var filter = new ModelFilter(min_visibility = protected_visibility)

		print toolcontext.format_h1("\n# RTA metrics")

		print toolcontext.format_h2("\n ## Live instances by mainmodules")
		var mmetrics = new MetricSet
		mmetrics.register(new MNLC(model, mainmodule, filter, toolcontext.modelbuilder))
		mmetrics.register(new MNLT(model, mainmodule, filter, toolcontext.modelbuilder))
		mmetrics.register(new MNCT(model, mainmodule, filter, toolcontext.modelbuilder))
		mmetrics.register(new MNLI(model, mainmodule, filter, toolcontext.modelbuilder))
		mmetrics.register(new MNLM(model, mainmodule, filter, toolcontext.modelbuilder))
		mmetrics.register(new MNLMD(model, mainmodule, filter, toolcontext.modelbuilder))
		mmetrics.register(new MNLDD(model, mainmodule, filter, toolcontext.modelbuilder))
		mmetrics.collect(new HashSet[MModule].from([mainmodule]))
		mmetrics.to_console(1, not toolcontext.opt_nocolors.value)
		if csv then mmetrics.to_csv.write_to_file("{out}/{mainmodule}.csv")

		var mtypes = new HashSet[MType]
		var analysis = new MetricsRapidTypeAnalysis(toolcontext.modelbuilder, mainmodule)
		analysis.run_analysis
		mtypes.add_all(analysis.live_types)
		mtypes.add_all(analysis.live_cast_types)

		print toolcontext.format_h2("\n ## Total live instances by mclasses")
		var cmetrics = new MetricSet
		cmetrics.register(analysis.cnli)
		cmetrics.register(analysis.cnlc)
		cmetrics.to_console(1, not toolcontext.opt_nocolors.value)
		if csv then cmetrics.to_csv.write_to_file("{out}/mclasses.csv")

		print toolcontext.format_h2("\n ## Total live instances by mtypes")
		var tmetrics = new MetricSet
		tmetrics.register(analysis.tnli)
		tmetrics.register(analysis.tnlc)
		tmetrics.to_console(1, not toolcontext.opt_nocolors.value)
		if csv then tmetrics.to_csv.write_to_file("{out}/mtypes.csv")

		print toolcontext.format_h2("\n ## MType complexity")
		var gmetrics = new MetricSet
		gmetrics.register(new TAGS)
		gmetrics.register(new TDGS)
		gmetrics.collect(mtypes)
		gmetrics.to_console(1, not toolcontext.opt_nocolors.value)
		if csv then gmetrics.to_csv.write_to_file("{out}/complexity.csv")

		callsite_info(analysis)

		# dump type and method infos
		if csv then
			analysis.live_types_to_csv.write_to_file("{out}/rta_types.csv")
			analysis.live_methods_to_tree.write_to_file("{out}/rta_methods.dat")
		end
	end

	fun callsite_info(rta: RapidTypeAnalysis)
	do
		print toolcontext.format_h2("\n ## Callsites")
		print "* {rta.live_callsites.length} live callsites"

		var csep = new Counter[MPropDef]
		var cglo = new Counter[MPropDef]
		var morphisme = new Counter[Int]
		for cs in rta.live_callsites do
			csep.inc(cs.mpropdef)
			var targets = rta.live_targets(cs)
			for d in targets do
				cglo.inc(d)
			end
			morphisme.inc(targets.length)
		end

		print toolcontext.format_h3("MMethodDef locally designated (by number of CallSites)")
		csep.print_summary
		csep.print_elements(5)

		print toolcontext.format_h3("MMethodDef possibly invoked at runtime (by number of CallSites)")
		cglo.print_summary
		cglo.print_elements(5)
	end
end

# Summary metrics

# RTA related metric that needs a `modelbuilder`
class RTAMetric
	super MModuleMetric

	# Modelbuilder used to access AST
	var modelbuilder: ModelBuilder
end

# MModule Metric: Number of Live Types
class MNLI
	super RTAMetric
	super IntMetric
	redef fun name do return "mnli"
	redef fun desc do return "number of live instances in a mmodule"


	redef fun collect(mainmodules) do
		for mainmodule in mainmodules do
			var analysis = new MetricsRapidTypeAnalysis(modelbuilder, mainmodule)
			analysis.run_analysis
			values[mainmodule] = analysis.tnli.sum
		end
	end
end

# MModule Metric: Number of Live Types
class MNLT
	super RTAMetric
	super IntMetric
	redef fun name do return "mnlt"
	redef fun desc do return "number of live mtypes in a mmodule"

	redef fun collect(mainmodules) do
		for mainmodule in mainmodules do
			var analysis = new MetricsRapidTypeAnalysis(modelbuilder, mainmodule)
			analysis.run_analysis
			values[mainmodule] = analysis.live_types.length
		end
	end
end

# MModule Metric: Number of Live Cast Types
class MNCT
	super RTAMetric
	super IntMetric
	redef fun name do return "mnct"
	redef fun desc do return "number of live cast mtypes in a mmodule"

	redef fun collect(mainmodules) do
		for mainmodule in mainmodules do
			var analysis = new MetricsRapidTypeAnalysis(modelbuilder, mainmodule)
			analysis.run_analysis
			values[mainmodule] = analysis.live_cast_types.length
		end
	end
end

# MModule Metric: Number of Live Classes
class MNLC
	super RTAMetric
	super IntMetric
	redef fun name do return "mnlc"
	redef fun desc do return "number of live mclasses in a mmodule"

	redef fun collect(mainmodules) do
		for mainmodule in mainmodules do
			var live = new HashSet[MClass]
			var analysis = new MetricsRapidTypeAnalysis(modelbuilder, mainmodule)
			analysis.run_analysis
			for mtype in analysis.live_types do
				live.add(mtype.mclass)
			end
			values[mainmodule] = live.length
		end
	end
end

# MModule Metric: Number of Live Methods
class MNLM
	super RTAMetric
	super IntMetric
	redef fun name do return "mnlm"
	redef fun desc do return "number of live methods in a mmodule"

	redef fun collect(mainmodules) do
		for mainmodule in mainmodules do
			var analysis = new MetricsRapidTypeAnalysis(modelbuilder, mainmodule)
			analysis.run_analysis
			values[mainmodule] = analysis.live_methods.length
		end
	end
end

# MModule Metric: Number of Live MethodDefs
class MNLMD
	super RTAMetric
	super IntMetric
	redef fun name do return "mnlmd"
	redef fun desc do return "number of live method definitions in a mmodule"

	redef fun collect(mainmodules) do
		for mainmodule in mainmodules do
			var analysis = new MetricsRapidTypeAnalysis(modelbuilder, mainmodule)
			analysis.run_analysis
			values[mainmodule] = analysis.live_methoddefs.length
		end
	end
end

# MModule Metric: Number of Dead MethodDefs
class MNLDD
	super RTAMetric
	super IntMetric
	redef fun name do return "mnldd"
	redef fun desc do return "number of dead method definitions in a mmodule"

	redef fun collect(mainmodules) do
		for mainmodule in mainmodules do
			var dead = 0
			var analysis = new MetricsRapidTypeAnalysis(modelbuilder, mainmodule)
			analysis.run_analysis
			for mmethod in analysis.live_methods do
				for mdef in mmethod.mpropdefs do
					if analysis.live_methoddefs.has(mdef) or mdef.is_abstract then continue
					dead += 1
				end
			end
			values[mainmodule] = dead
		end
	end
end

# MClass metrics

# Class Metric: Number of Live Instances
#
# count all the `new` made on each mclass
class CNLI
	super MClassMetric
	super IntMetric
	redef fun name do return "cnli"
	redef fun desc do return "number of live instances for a mclass"

	redef fun collect(mclasses) do end
end

# Class Metric: Number of Live Cast
#
# count all the cast made on each mclass type
class CNLC
	super MClassMetric
	super IntMetric
	redef fun name do return "cnlc"
	redef fun desc do return "number of live cast for a mclass type"

	redef fun collect(mclasses) do end
end

# MType metrics

# A metric about MType
interface MTypeMetric
	super Metric
	redef type ELM: MType
end

# Type Metric: Number of Live Instances
#
# count all the `new` made on each types
class TNLI
	super MTypeMetric
	super IntMetric
	redef fun name do return "tnli"
	redef fun desc do return "number of live instances for a mtype"

	redef fun collect(mtypes) do end
end

# Type Metric: Number of Live Cast
#
# count all the cast made to each types
class TNLC
	super MTypeMetric
	super IntMetric
	redef fun name do return "tnlc"
	redef fun desc do return "number of live casts to a mtype"

	redef fun collect(mtypes) do end
end

# Type Metric: Arity of Generic Signature
#
# tags(List[X]) = 1
# tags(Map[X, Y]) = 2
class TAGS
	super MTypeMetric
	super IntMetric
	redef fun name do return "tags"
	redef fun desc do return "arity of generic signature"

	redef fun collect(mtypes) do
		for mtype in mtypes do
			if mtype isa MGenericType then
				values[mtype] = mtype.arguments.length
			else
				values[mtype] = 0
			end
		end
	end
end

# Type Metric: Depth of Generic Signature
#
# tdgs(List[X]) = 1
# tdgs(Map[X, List[Y]]) = 2
class TDGS
	super MTypeMetric
	super IntMetric
	redef fun name do return "tdos"
	redef fun desc do return "depth of generic signature"

	redef fun collect(mtypes) do
		for mtype in mtypes do
			values[mtype] = mtype.signature_depth
		end
	end
end

# rta redef

# Custom RTA analyzer
class MetricsRapidTypeAnalysis
	super RapidTypeAnalysis

	# Class Live Instances
	var cnli: CNLI is lazy do return new CNLI(modelbuilder.model, mainmodule)

	# Class Live Casts
	var cnlc: CNLC is lazy do return new CNLC(modelbuilder.model, mainmodule)

	# Type Live Instances
	var tnli = new TNLI

	# Rtpe Live Casts
	var tnlc = new TNLC

	redef fun add_new(recv, mtype) do
		super
		tnli.values.inc(mtype)
		cnli.values.inc(mtype.mclass)
	end

	redef fun add_cast(mtype) do
		super
		tnlc.values.inc(mtype)

		mtype = mtype.undecorate
		if mtype isa MClassType then
			cnlc.values.inc(mtype.mclass)
		end
	end
end

# model redefs

redef class MType
	private fun signature_depth: Int do
		var mtype = self.undecorate
		if not mtype isa MGenericType then return 0

		var depth = 0
		for ft in mtype.arguments do
			var ftd = ft.signature_depth
			if ftd > depth then depth = ftd
		end
		return depth + 1
	end
end
src/metrics/rta_metrics.nit:17,1--401,3