Services to gather information on the performance of events by categories

Provides PerfMap to manage all the categories and PerfEntry for per-category statistics.

for i in 100.times do
    var clock = new Clock

    # Do some "work" here
    nanosleep(0, 1000000)

    # Register the perf
    sys.perfs["sleep 1ms"].add clock.lapse

    # Do some other "work" here
    nanosleep(0, 5000000)

    # Register the perf
    sys.perfs["sleep 5ms"].add clock.lapse
end

assert sys.perfs["sleep 1ms"].count == 100
assert sys.perfs["sleep 1ms"].avg.is_approx(0.001, 0.001)
assert sys.perfs["sleep 5ms"].avg.is_approx(0.005, 0.005)

Introduced classes

class PerfEntry

performance_analysis :: PerfEntry

Statistics on wall clock execution time of a category of events by name
class PerfMap

performance_analysis :: PerfMap

Collection of statistics on many events

Redefined classes

redef class Sys

performance_analysis :: performance_analysis $ Sys

The main class of the program.

All class definitions

class PerfEntry

performance_analysis $ PerfEntry

Statistics on wall clock execution time of a category of events by name
class PerfMap

performance_analysis $ PerfMap

Collection of statistics on many events
redef class Sys

performance_analysis :: performance_analysis $ Sys

The main class of the program.
package_diagram performance_analysis::performance_analysis performance_analysis realtime realtime performance_analysis::performance_analysis->realtime core core realtime->core ...core ... ...core->core gamnit::dynamic_resolution dynamic_resolution gamnit::dynamic_resolution->performance_analysis::performance_analysis nitcorn::log log nitcorn::log->performance_analysis::performance_analysis gamnit::flat_core flat_core gamnit::flat_core->gamnit::dynamic_resolution gamnit::flat_core... ... gamnit::flat_core...->gamnit::flat_core a_star-m a_star-m a_star-m->nitcorn::log a_star-m... ... a_star-m...->a_star-m

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 array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
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 core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
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 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 gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
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 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 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 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

Parents

module realtime

realtime :: realtime

Services to keep time of the wall clock time

Children

module dynamic_resolution

gamnit :: dynamic_resolution

Virtual screen with a resolution independent from the real screen
module log

nitcorn :: log

Services inserting a timestamp in all prints and to log each requests

Descendants

module a_star-m

a_star-m

module bmfont

gamnit :: bmfont

Parse Angel Code BMFont format and draw text
module cardboard

gamnit :: cardboard

Update the orientation of world_camera at each frame using the head position given by android::cardboard
module depth

gamnit :: depth

Framework for 3D games in Nit
module depth_core

gamnit :: depth_core

Base entities of the depth 3D game framework
module flat

gamnit :: flat

Simple API for 2D games, built around Sprite and App::update
module flat_core

gamnit :: flat_core

Core services for the flat API for 2D games
module font

gamnit :: font

Abstract font drawing services, implemented by bmfont and tileset
module model_dimensions

gamnit :: model_dimensions

Dimensions related services for Model and Mesh
module more_lights

gamnit :: more_lights

More implementations of Light
module more_materials

gamnit :: more_materials

Various material implementations
module more_meshes

gamnit :: more_meshes

More simple geometric meshes
module more_models

gamnit :: more_models

Services to load models from the assets folder
module particles

gamnit :: particles

Particle effects
module selection

gamnit :: selection

Select Actor from a screen coordinate
module shadow

gamnit :: shadow

Shadow mapping using a depth texture
module stereoscopic_view

gamnit :: stereoscopic_view

Refine EulerCamera and App::frame_core_draw to get a stereoscopic view
module tileset

gamnit :: tileset

Support for TileSet, TileSetFont and drawing text with TextSprites
module virtual_gamepad

gamnit :: virtual_gamepad

Virtual gamepad mapped to keyboard keys for quick and dirty mobile support
module vr

gamnit :: vr

VR support for gamnit depth, for Android only
# Services to gather information on the performance of events by categories
#
# Provides `PerfMap` to manage all the categories and
# `PerfEntry` for per-category statistics.
#
# ~~~
# for i in 100.times do
#     var clock = new Clock
#
#     # Do some "work" here
#     nanosleep(0, 1000000)
#
#     # Register the perf
#     sys.perfs["sleep 1ms"].add clock.lapse
#
#     # Do some other "work" here
#     nanosleep(0, 5000000)
#
#     # Register the perf
#     sys.perfs["sleep 5ms"].add clock.lapse
# end
#
# assert sys.perfs["sleep 1ms"].count == 100
# assert sys.perfs["sleep 1ms"].avg.is_approx(0.001, 0.001)
# assert sys.perfs["sleep 5ms"].avg.is_approx(0.005, 0.005)
# ~~~
module performance_analysis

import realtime

redef class Sys
	# Main `PerfMap` available by default
	var perfs = new PerfMap
end

# Collection of statistics on many events
class PerfMap
	super HashMap[String, PerfEntry]

	redef fun provide_default_value(key)
	do
		if not key isa String then return super

		var ts = new PerfEntry(key)
		self[key] = ts
		return ts
	end

	# Number of digits to the right of the decimal points in reports created by `to_s`
	#
	# Defaults to 4.
	var precision = 4 is writable

	redef fun to_s
	do
		var prec = precision

		var table = new Map[String, Array[String]]
		for event, stats in self do
			table[event] = [event,
				stats.min.to_precision(prec),
				stats.max.to_precision(prec),
				stats.avg.to_precision(prec),
				stats.sum.to_precision(prec),
				stats.count.to_s]
		end

		var widths = [0] * 6
		for event, row in table do
			for i in row.length.times do
				widths[i] = widths[i].max(row[i].length)
			end
		end

		var s = "# {"Event".justify(widths[0], 0.0)} {"min".justify(widths[1], 0.5)} {"max".justify(widths[2], 0.5)} {"avg".justify(widths[3], 0.5)} {"sum".justify(widths[4], 0.5)} {"count".justify(widths[5], 0.5)}\n"

		var sorted_events = table.keys.to_a
		alpha_comparator.sort sorted_events
		for event in sorted_events do
			var row = table[event]
			s += "*"
			for c in row.length.times do
				var cell = row[c]
				s += " "
				if c == 0 then
					s += cell.justify(widths[c], 0.0, '.')
				else s += cell.justify(widths[c], 1.0)
			end
			s += "\n"
		end
		return s
	end
end

# Statistics on wall clock execution time of a category of events by `name`
class PerfEntry

	# Name of the category
	var name: String

	# Shortest execution time of registered events
	var min = 0.0

	# Longest execution time of registered events
	var max = 0.0

	# Average execution time of registered events
	var avg = 0.0

	# Number of registered events
	var count = 0

	# Total execution time of this event
	var sum = 0.0

	# Register a new event execution time in seconds
	fun add(time: Float)
	do
		if time.to_f < min.to_f or count == 0 then min = time
		if time.to_f > max.to_f then max = time

		sum += time
		count += 1
		avg = sum / count.to_f
	end

	redef fun to_s do return "min {min}, max {max}, avg {avg}, sum {sum}, count {count}"
end
lib/performance_analysis/performance_analysis.nit:15,1--142,3