annotate lib/IMPL/Profiler.pm @ 413:af8d359ee4cc ref20150831

working on di container
author cin
date Thu, 24 Sep 2015 12:19:30 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Profiler;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use warnings;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use Time::HiRes;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 require Scalar::Util;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 our $Enabled;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 our %TrappedModules;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 our %InvokeInfo;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 our $InvokeTime = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 our @TrapQueue;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 our $Filter ||= qr//;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 my $level;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 $level = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 if ($Enabled) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 warn "profiler enabled";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 unshift @INC, sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 my ($self,$filename) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 (my $module = $filename) =~ s/\//::/g;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 $module =~ s/\.\w+$//;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 return unless $module =~ $Filter;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 foreach my $dir (@INC) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 my $fullName = "$dir/$filename";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 if (-f $fullName) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 open my $hmod, $fullName or die "$fullName: $!" if $!;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 my @source;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 local $/ = "\n";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 while (my $line = <$hmod>) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 last if $line =~ /^\s*__END__/;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 push @source, $line;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 undef $hmod;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 push @source,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 "IMPL::Profiler::trap_all(__PACKAGE__);\n",
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 "1;\n";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 return (sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 if (@source) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 $_ = shift @source;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 return 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 }, undef );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 no warnings 'once';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 *CORE::GLOBAL::caller = sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 my $target = (shift || 0)+1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 my $realFrame = 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 for (my $i = 1; $i<$target; $i++) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 $realFrame ++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 my $caller = CORE::caller($realFrame-1) or return;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 $realFrame ++ if $caller eq 'IMPL::Profiler::Proxy'; #current frame is proxy
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 my @frame = CORE::caller($realFrame) or return;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 if ( $frame[0] eq 'IMPL::Profiler::Proxy' ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 my @next = CORE::caller($realFrame+1) or return;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 @frame[0..2] = @next[0..2];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 #warn " "x$level,"$frame[0] - $frame[3]";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 return wantarray ? @frame : $frame[0];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 sub trap_all {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 return if not $Enabled;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 no strict 'refs';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 foreach my $class (@_) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 next if $TrappedModules{$class};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 $TrappedModules{$class} = 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 eval "warn 'load $class'; require $class;" if not %{"${class}::"};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 die $@ if $@;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 no strict 'refs';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 my $table = \%{"${class}::"};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 trap($class,$_) foreach (grep eval { *{$table->{$_}}{CODE} }, keys %$table); # here can be a not a GLOB reference
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 sub trap {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 my ($class,$method) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 return if not $Enabled;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 return if $method eq 'import';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 no strict 'refs';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 my $prevCode = \&{"${class}::${method}"};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 my $proto = prototype $prevCode;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 if (defined $proto and not $proto) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 return;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 package IMPL::Profiler::Proxy;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 no warnings 'redefine';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 my $sub = sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 my $t0 = [Time::HiRes::gettimeofday];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 my @arr;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 my $scalar;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 my $entry = $prevCode;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 my ($timeOwn,$timeTotal);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 my $context = wantarray;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 local $InvokeTime = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 #warn " "x$level,"enter ${class}::$method";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 $level ++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 if ($context) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 @arr = &$entry(@_);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 if (defined $context) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 $scalar = &$entry(@_);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 &$entry(@_);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 $timeTotal = Time::HiRes::tv_interval($t0);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 $timeOwn = $timeTotal - $InvokeTime;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 $InvokeInfo{"${class}::${method}"}{Count} ++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142 $InvokeInfo{"${class}::${method}"}{Total} += $timeTotal;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 $InvokeInfo{"${class}::${method}"}{Own} += $timeOwn;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 $InvokeTime += $timeTotal;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 $level --;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 #warn " "x$level,"leave ${class}::$method";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 return $context ? @arr : $scalar;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 if ($proto) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 Scalar::Util::set_prototype($sub => $proto);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 *{"${class}::${method}"} = $sub;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157 sub PrintStatistics {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 my $hout = shift || *STDERR;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159 print $hout "-- modules --\n";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160 print $hout "$_\n" foreach sort keys %TrappedModules;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161 print $hout "\n-- stats --\n";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162 print $hout
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163 pad($_,50),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164 pad("$InvokeInfo{$_}{Count}",10),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165 pad(sprintf("%.3f",$InvokeInfo{$_}{Own}),10),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166 pad(sprintf("%.3f",$InvokeInfo{$_}{Total}),10),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167 "\n"
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
168 foreach sort { $InvokeInfo{$b}{Own} <=> $InvokeInfo{$a}{Own} } keys %InvokeInfo;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
169 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171 sub ResetStatistics {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172 $InvokeTime = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173 %InvokeInfo = ();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176 sub pad {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177 my ($str,$len) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178 if (length $str < $len) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179 return $str.(' 'x ($len- length $str));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181 return $str;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
183 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
184 1;