annotate Lib/PerfCounter.pm @ 7:94d47b388442

Улучшены тесты Исправлены ошибки Улучшена документация Работа над схемой DOM
author Sergey
date Mon, 24 Aug 2009 01:05:34 +0400
parents 03e58a454b20
children 16ada169ca75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 package PerfCounter;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2 use strict;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4 use Exporter;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
5 our @ISA = qw(Exporter);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6 our @EXPORT = qw(&GetTimeCounter &StartTimeCounter &StopTimeCounter &SetDBIPerfCounter);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
7
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8 our %Counters;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10 sub Reset() {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11 $_->Reset foreach values %Counters;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
13
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14 sub GetTimeCounter {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
15 my $counter = $Counters{$_[0]};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
16 die new Exception("'$_[0]' already exists and isn't a time counter.") if ref $counter and ref $counter ne 'PerfInterval';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
17 if (not ref $counter) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
18 $counter = new PerfInterval;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
19 $Counters{$_[0]} = $counter;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
20 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 return $counter;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24 sub StartTimeCounter {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25 my $counter = GetTimeCounter($_[0]);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26 if (not $counter->IsOpened) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27 $counter->OpenInterval;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31 sub StopTimeCounter {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32 my $counter = GetTimeCounter($_[0]);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33 if ($counter->IsOpened) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34 $counter->CloseInterval;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38 sub SetDBIPerfCounter{
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39 my ($dbh,$name) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40 $name ||= 'DBI';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41 $Counters{$name} = DBIPerfomance->new(DBH => $dbh);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44 package PerfInterval;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46 use Time::HiRes qw(gettimeofday tv_interval);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48 sub new {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49 my $class = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50 my $self = bless { StartTime => scalar(gettimeofday()) }, $class;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51 return $self;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54 sub CloseInterval {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57 if (not $this->{'EndTime'}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
58 $this->{'EndTime'} = scalar(gettimeofday());
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59 $this->{'Value'} += $this->{'EndTime'} - $this->{'StartTime'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62 return $this->{'Value'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65 sub Value {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68 if (not $this->{'EndTime'}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69 return sprintf ( '%.3f+',scalar(gettimeofday()) - $this->{'StartTime'});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71 return sprintf ( '%.3f',$this->{'Value'});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75 sub Add {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76 my ($this,$interval) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78 if(ref $interval eq 'PerfInterval') {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 $this->{'Value'} += $interval->{'Value'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81 $this->{'Value'} += $interval;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
83
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84 return $this->{'Value'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
87 sub IsOpened {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
88 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89 return( not $this->{'EndTime'} );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
91
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92 sub OpenInterval {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
94
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
95 $this->{'StartTime'} = gettimeofday();
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
96 delete $this->{'EndTime'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
97
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
98 return 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
99 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
100
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
101 sub Reset {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
102 my ($this) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
103
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
104 $this->CloseInterval();
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
105 $this->{'Value'} = 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
106 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
107
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
108 package DBIPerfomance;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
109 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
110 our @ISA = qw(Object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
111
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
112 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
113 DeclareProperty DBH => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
114
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
115 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
116
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
117 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
118 my $this=shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
119 $this->SUPER::CTOR(@_);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
120
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
121
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
122 $this->DBH->{Profile} = 6;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
123 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
124
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
125 sub Reset {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
126 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
127 $this->DBH->{Profile} = 6;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
128 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
129
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
130 sub Value {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
131 my ($this,%opt) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
132
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
133 my $infoSelect = { count => 0, time => 0};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
134 my $infoUpdate = { count => 0, time => 0};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
135 my $infoTotal;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
136
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
137 foreach my $stmt (grep /^SELECT/i,keys %{$this->DBH->{Profile}->{Data} || {}}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
138 $infoSelect->{'count'} += $this->DBH->{Profile}{Data}{$stmt}{execute}[0] || 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
139 $infoSelect->{'time'} += $this->DBH->{Profile}{Data}{$stmt}{execute}[1] || 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
140 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
141
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
142 foreach my $stmt (grep /^UPDATE/i,keys %{$this->DBH->{Profile}->{Data} || {}}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
143 $infoUpdate->{'count'} += $this->DBH->{Profile}{Data}{$stmt}{execute}[0] || 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
144 $infoUpdate->{'time'} += $this->DBH->{Profile}{Data}{$stmt}{execute}[1] || 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
145 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
146
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
147 $infoTotal->{'count'} = $infoSelect->{'count'} + $infoUpdate->{'count'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
148 $infoTotal->{'time'} = $infoSelect->{'time'} + $infoUpdate->{'time'};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
149
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
150 if ($opt{'extended'}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
151 return ($infoSelect,$infoUpdate,$infoTotal);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
152 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
153 return sprintf( '%i (%.2f)', $infoTotal->{count},$infoTotal->{time} );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
154 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
155 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
156
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
157 sub Queries {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
158 my ($this) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
159 return [ map { "$this->{$DBH}{Profile}{Data}{$_}{execute}[0] x $_"} sort grep $_, keys %{$this->DBH->{Profile}->{Data}}];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
160 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
161 1;