# ---------------------------------------------------------------------------- # ::qw::profile # ---------------------------------------------------------------------------- /* { There are two ways to profile: (1) ::qw::profile::finally - uses qw::finally to terminate timer Useful for scopes. (2) ::qw::profile::begin/end Useful for loops. Only works if ::qw::control(qw_profile_is_enabled) is set. Stay away from recursion. Each way, when done puts out a line as below. ::qw::profile::array(profile_name) .milliseconds_start 1 .event_count .milliseconds_total .milliseconds_average */ } ::namespace eval ::qw::profile {}; ::proc ::qw::profile::finally {Name} { /* { ::qw::profile::finally $Name; Puts the number of milliseconds on proc/method exit. No other calls necessary. Only happens if caller has variable rwb1_debug set to non-zero. */ } ::upvar rwb1_debug Rwb1Debug; ::if {$Rwb1Debug eq ""} { ::return; } ::if {$Rwb1Debug==0} { ::return; } ::if {[::info exists ::qw::profile::milliseconds_array($Name)]} { ::qw::bug 314120250410162925 "[::qw::procname] - profile \"$Name\" already exists."; } ::set ::qw::profile::milliseconds_array($Name) [::clock clicks -milliseconds]; ::trace add variable Rwb1Debug unset [::list ::qw::profile::finally_callback $Name]; } ::proc ::qw::profile::finally_callback {Name args} { /* { This proc is the same as qw::profile::end except we need the extra args because this a callback from a trace. */ } ::if {![::info exists ::qw::profile::milliseconds_array($Name)]} { ::return 0; } ::set MilliSeconds [::expr {[::clock clicks -milliseconds]-$::qw::profile::milliseconds_array($Name)}]; ::unset ::qw::profile::milliseconds_array($Name); ::puts "::qw::profile::end,name==$Name,milliseconds==[::qw::number::format_whole_number .value $MilliSeconds]"; ::return $MilliSeconds; } ::proc ::qw::profile::begin {Name} { ::upvar rwb1_debug Rwb1Debug; ::if {$Rwb1Debug eq ""} { ::return; } ::if {$Rwb1Debug==0} { ::return; } ::if {[::info exists ::qw::profile::milliseconds_array($Name)]} { ::qw::bug 314120250409102052 "[::qw::procname] - profile \"$Name\" already exists."; } ::set ::qw::profile::milliseconds_array($Name) [::clock clicks -milliseconds]; } ::proc ::qw::profile::end {Name} { /* { Puts out the interval and returns the interval in milliseconds in case the caller wants to use it. This explicitly ends the profile whereas ::qw::profile::finally puts out on return from a proc/method. The array entry for the profile is unset. Use this when you need the interval immediately without using qw::finally, i.e. before exiting the proc/method block. */ } ::if {![::info exists ::qw::profile::milliseconds_array($Name)]} { ::return 0; } ::set MilliSeconds [::expr {[::clock clicks -milliseconds]-$::qw::profile::milliseconds_array($Name)}]; ::unset ::qw::profile::milliseconds_array($Name); ::puts "::qw::profile::end,name==$Name,milliseconds==[::qw::number::format_whole_number .value $MilliSeconds]"; ::return $MilliSeconds; }