| 1 | ############################################################################## | ||
| 2 | # | ||
| 3 | # Time::RTM runtime metrics | ||
| 4 | # 2024 (c) Vladi Belperchinov-Shabanski "Cade" <cade@noxrun.com> | ||
| 5 | # | ||
| 6 | # DISTRIBUTED UNDER GPLv2 | ||
| 7 | # | ||
| 8 | ############################################################################## | ||
| 9 | package Time::RTM; | ||
| 10 | |||
| 11 | use 5.006; | ||
| 12 | use strict; | ||
| 13 | use Carp; | ||
| 14 | use Time::RTM::Scope; | ||
| 15 | |||
| 16 | our $VERSION = '1.15'; | ||
| 17 | |||
| 18 | our %ATTRS = ( | ||
| 19 | log => 1, | ||
| 20 | logfile => 1, | ||
| 21 | ); | ||
| 22 | |||
| 23 | sub new | ||
| 24 | { | ||
| 25 | my $class = shift; | ||
| 26 | |||
| 27 | my $self = { RTM => [] }; | ||
| 28 | bless $self; | ||
| 29 | $self->attr( @_ ); | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub attr | ||
| 34 | { | ||
| 35 | my $self = shift; | ||
| 36 | |||
| 37 | croak "bad number of attribute/value pairs" unless @_ == 0 or @_ % 2 == 0; | ||
| 38 | my @ret; | ||
| 39 | my %h = @_; | ||
| 40 | for( keys %h ) | ||
| 41 | { | ||
| 42 | croak "invalid attribute name: $_" unless $ATTRS{ $_ }; | ||
| 43 | $self->{ $_ } = $h{ $_ } if defined $h{ $_ }; | ||
| 44 | push @ret, $self->{ $_ }; | ||
| 45 | } | ||
| 46 | return @ret; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub begin_scope | ||
| 50 | { | ||
| 51 | my $self = shift; | ||
| 52 | |||
| 53 | return new Time::RTM::Scope( $self, @_ ); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub __add_dt | ||
| 57 | { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | my $key = shift; | ||
| 61 | my $st = shift; | ||
| 62 | my $dt = shift; | ||
| 63 | my $et = shift; # should be used for debug only | ||
| 64 | |||
| 65 | push @{ $self->{ 'RTM' } }, "[RTM:K=$key;S=$st;D=$dt]"; # .= ";E=$et" if $DEBUG | ||
| 66 | } | ||
| 67 | |||
| 68 | sub save | ||
| 69 | { | ||
| 70 | my $self = shift; | ||
| 71 | |||
| 72 | open( my $of, $self->{ 'logfile' } ); | ||
| 73 | $of->print( $_, "\n" ) for @{ $self->{ 'RTM' } }; | ||
| 74 | close( $of ); | ||
| 75 | @{ $self->{ 'RTM' } }= (); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub get_data | ||
| 79 | { | ||
| 80 | my $self = shift; | ||
| 81 | |||
| 82 | return $self->{ 'RTM' }; | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; | ||
| 86 | |||
| 87 | =pod | ||
| 88 | |||
| 89 | =head1 NAME | ||
| 90 | |||
| 91 | Time::RTM - Run-time metrics stats | ||
| 92 | |||
| 93 | =head1 SYNOPSIS | ||
| 94 | |||
| 95 | use Time::RTM; | ||
| 96 | |||
| 97 | my $rtm = new Time::RTM logfile => '/tmp/rtm.log'; | ||
| 98 | |||
| 99 | { | ||
| 100 | my $_r = $rtm->begin_scope( 'DB/SELECT/CARS_MAP' ); | ||
| 101 | # do work, select tables :) | ||
| 102 | } | ||
| 103 | # end of scope: | ||
| 104 | # $_r stats will be recorded internally into $rtm and $_r discarded | ||
| 105 | |||
| 106 | # you can force recording of stats with deleting $_r before end of scope: | ||
| 107 | undef $_r; | ||
| 108 | # or: | ||
| 109 | $_r->stop(); | ||
| 110 | |||
| 111 | # $_r can be restarted with new stats: | ||
| 112 | $_r->restart(); | ||
| 113 | |||
| 114 | # if keys/name of the scope is not available upon begin_scope() it may be | ||
| 115 | # set later with: | ||
| 116 | $_r->set_keys( "PROCESSING/MESSAGE/$msg_id" ); | ||
| 117 | # because $msg_id may be not known at the beginning of the scope, but | ||
| 118 | # figured out of the incoming data later in the scope... | ||
| 119 | |||
| 120 | # to write down currently recorded stats to the logfile: | ||
| 121 | $rtm->save(); | ||
| 122 | |||
| 123 | # which will also flush current data and will record new one | ||
| 124 | $rtm->begin_scope( 'HANDLERS/h_select/TRIPS' ); | ||
| 125 | # etc. | ||
| 126 | |||
| 127 | # you can also access anytime the internal log and send it somewhere else: | ||
| 128 | my $rtm_array_ref = $rtm->get_data(); | ||
| 129 | # then save @$rtm_array_ref to a database or send tp a dot matrix printer | ||
| 130 | # you also can flush it after save and continue using $rtm with new data: | ||
| 131 | @$rtm_array_ref = (); | ||
| 132 | $rtm->begin_scope( 'FILE/IO' ); | ||
| 133 | |||
| 134 | |||
| 135 | # to view stats from the logfile: | ||
| 136 | # in a shell prompt: | ||
| 137 | perl -MTime::RTM::Report -e 'rtm_report_main(@ARGV)' -- /tmp/rtm.log | ||
| 138 | |||
| 139 | # to get help: | ||
| 140 | perl -MTime::RTM::Report -e 'rtm_report_main(@ARGV)' -- -h | ||
| 141 | |||
| 142 | # or create small perl, rtm.pl: | ||
| 143 | #!/usr/bin/perl | ||
| 144 | use strict; | ||
| 145 | use Time::RTM::Report; | ||
| 146 | rtm_report_main(@ARGV) | ||
| 147 | |||
| 148 | # then: | ||
| 149 | rtm.pl -l 3 -p DB/SELECT -s MS /tmp/rtm.log | ||
| 150 | # will printf report of all DB/SELECT branches, up to level 3 with | ||
| 151 | # sorting by median speed, see "rtm.pl -h" for more details | ||
| 152 | |||
| 153 | # sample report looks like: | ||
| 154 | +------------------------------------------------------------------------+ | ||
| 155 | | METRIC KEY PATH | COUNT | TIME | MED | MED/sec | | ||
| 156 | +------------------------------------------------------------------------+ | ||
| 157 | | NETWORK | 21441 | 272.743691 | 0.000491 | 2036.659878 | | ||
| 158 | | SOCK_REQUEST | 7249 | 92.968934 | 0.000478 | 2092.050209 | | ||
| 159 | | SOCK_RESPONSE | 6943 | 0.834674 | 0.000067 | 14925.373134 | | ||
| 160 | | MAIN_LOOP | 6943 | 79.945274 | 0.000195 | 5128.205128 | | ||
| 161 | | PROCESS_XT_MSG | 6943 | 79.945274 | 0.000195 | 5128.205128 | | ||
| 162 | | CONNECT | 306 | 75.680687 | 0.245800 | 4.068348 | | ||
| 163 | | SELECT | 317 | 0.924200 | 0.002907 | 343.997248 | | ||
| 164 | | LANG | 306 | 0.098311 | 0.000297 | 3367.003367 | | ||
| 165 | | NEXT | 4738 | 0.867704 | 0.000171 | 5847.953216 | | ||
| 166 | | HANDLERS | 6628 | 79.287188 | 0.000127 | 7874.015748 | | ||
| 167 | | h_connect | 306 | 75.634023 | 0.245563 | 4.072275 | | ||
| 168 | | h_update | 12 | 0.038996 | 0.003227 | 309.885342 | | ||
| 169 | | TASKS | 6 | 0.019931 | 0.003396 | 294.464075 | | ||
| 170 | | ZZ_SESSIONS | 6 | 0.019065 | 0.003227 | 309.885342 | | ||
| 171 | | h_select | 317 | 0.838763 | 0.002643 | 378.357927 | | ||
| 172 | | TASKS | 4 | 0.011384 | 0.003209 | 311.623559 | | ||
| 173 | | CERTS | 6 | 0.014033 | 0.002339 | 427.533134 | | ||
| 174 | | TRIPS | 3 | 0.002698 | 0.001012 | 988.142292 | | ||
| 175 | | h_finish | 317 | 0.009083 | 0.000027 | 37037.037037 | | ||
| 176 | | DB | 14635 | 9.172849 | 0.000086 | 11627.906977 | | ||
| 177 | | INSERT | 3 | 0.005676 | 0.001802 | 554.938957 | | ||
| 178 | | CARS_MAP | 3 | 0.005676 | 0.001802 | 554.938957 | | ||
| 179 | | SELECT | 6200 | 8.692144 | 0.001143 | 874.890639 | | ||
| 180 | | TASKS | 4 | 0.007903 | 0.001705 | 586.510264 | | ||
| 181 | | USER_DATA | 1224 | 1.386472 | 0.000938 | 1066.098081 | | ||
| 182 | | CARS_MAP | 1224 | 1.253929 | 0.000826 | 1210.653753 | | ||
| 183 | | UPDATE | 7 | 0.008143 | 0.001111 | 900.090009 | | ||
| 184 | | CARS_SIDS | 7 | 0.008143 | 0.001111 | 900.090009 | | ||
| 185 | | FREEID | 6 | 0.006308 | 0.000970 | 1030.927835 | | ||
| 186 | | TASKS | 3 | 0.004149 | 0.001324 | 755.287009 | | ||
| 187 | +------------------------------------------------------------------------+ | ||
| 188 | |||
| 189 | rtm.pl -p DB/SELECT -s MS /tmp/rtm.log | ||
| 190 | +------------------------------------------------------------------------+ | ||
| 191 | | METRIC KEY PATH | COUNT | TIME | MED | MED/sec | | ||
| 192 | +------------------------------------------------------------------------+ | ||
| 193 | | DB | 14635 | 9.172849 | 0.000086 | 11627.906977 | | ||
| 194 | | SELECT | 6200 | 8.692144 | 0.001143 | 874.890639 | | ||
| 195 | | TASKS | 4 | 0.007903 | 0.001705 | 586.510264 | | ||
| 196 | | USER_DATA | 1224 | 1.386472 | 0.000938 | 1066.098081 | | ||
| 197 | | CARS_MAP | 1224 | 1.253929 | 0.000826 | 1210.653753 | | ||
| 198 | +------------------------------------------------------------------------+ | ||
| 199 | |||
| 200 | rtm.pl -p DB -l 2 -s MS /tmp/rtm.log | ||
| 201 | +------------------------------------------------------------------------+ | ||
| 202 | | METRIC KEY PATH | COUNT | TIME | MED | MED/sec | | ||
| 203 | +------------------------------------------------------------------------+ | ||
| 204 | | DB | 14635 | 9.172849 | 0.000086 | 11627.906977 | | ||
| 205 | | INSERT | 3 | 0.005676 | 0.001802 | 554.938957 | | ||
| 206 | | SELECT | 6200 | 8.692144 | 0.001143 | 874.890639 | | ||
| 207 | | UPDATE | 7 | 0.008143 | 0.001111 | 900.090009 | | ||
| 208 | | FREEID | 6 | 0.006308 | 0.000970 | 1030.927835 | | ||
| 209 | +------------------------------------------------------------------------+ | ||
| 210 | |||
| 211 | # get dispersion graph for metric-path | ||
| 212 | rtm.pl -p DB/SELECT -r /tmp/rtm.log | ||
| 213 | status: option: dispersion graph requested | ||
| 214 | status: option: metric path set to [DB/SELECT] | ||
| 215 | +------------------------------------------------------------------------+ | ||
| 216 | | DTIME | OPS/sec | PERCENT | FREQUENCY | | ||
| 217 | +------------------------------------------------------------------------+ | ||
| 218 | | 0.000143 | 6993.006993 | 67.0 | *********************************** | | ||
| 219 | | 0.001585 | 630.914826 | 28.4 | ************** | | ||
| 220 | | 0.003028 | 330.250991 | 4.1 | ** | | ||
| 221 | | 0.004470 | 223.713647 | 0.2 | | | ||
| 222 | | 0.005913 | 169.118891 | 0.1 | | | ||
| 223 | +------------------------------------------------------------------------+ | ||
| 224 | |||
| 225 | |||
| 226 | =head1 DESCRIPTION | ||
| 227 | |||
| 228 | This module records time slices used by pieces of code. More docs to follow :) | ||
| 229 | |||
| 230 | =head1 GITHUB REPOSITORY | ||
| 231 | |||
| 232 | |||
| 233 | https://github.com/cade-vs/perl-time-rtm | ||
| 234 | |||
| 235 | git clone https://github.com/cade-vs/perl-time-rtm | ||
| 236 | |||
| 237 | |||
| 238 | =head1 AUTHOR | ||
| 239 | |||
| 240 | Vladi Belperchinov-Shabanski "Cade" | ||
| 241 | |||
| 242 | <cade@noxrun.com> <cade@cpan.org> | ||
| 243 | |||
| 244 | http://cade.noxrun.com | ||
| 245 | |||
| 246 | =head1 COPYRIGHT AND LICENSE | ||
| 247 | |||
| 248 | This software is (c) 2024 by Vladi Belperchinov-Shabanski E<lt>cade@noxrun.comE<gt> E<lt>cade@cpan.orgE<gt>. | ||
| 249 | |||
| 250 | This is free software; you can redistribute it and/or modify it under | ||
| 251 | the same terms as the Perl 5 programming language system itself. | ||
| 252 | |||
| 253 | =cut |