@@ -95,6 +95,10 @@ class evxCache {
95
95
96
96
protected $ prefixGetter ;
97
97
98
+ protected $ timings = [];
99
+
100
+ protected $ startTimings = [];
101
+
98
102
/**
99
103
* @var bool|\Domnikl\Statsd\Client
100
104
*/
@@ -129,14 +133,31 @@ public function __construct(array $aConfig, $driver = FALSE, $useLocks = FALSE){
129
133
$ this ->driver = $ driver ;
130
134
}
131
135
if (!empty ($ aConfig ['statsd ' ])) {
132
- $ connection = new \Domnikl \Statsd \Connection \UdpSocket (
133
- $ aConfig ['statsd ' ]['host ' ],
134
- $ aConfig ['statsd ' ]['port ' ],
135
- isset ($ aConfig ['statsd ' ]['timeout ' ]) ? $ aConfig ['statsd ' ]['timeout ' ] : 0.5 ,
136
- isset ($ aConfig ['statsd ' ]['persist ' ]) ? $ aConfig ['statsd ' ]['persist ' ] : true
137
- );
138
- $ this ->metric = new \Domnikl \Statsd \Client ($ connection , $ aConfig ['statsd ' ]['prefix ' ]);
139
-
136
+ $ this ->metric = true ;
137
+ $ self = $ this ;
138
+ register_shutdown_function (function () use ($ aConfig , $ self ) {
139
+ if (!empty ($ self ) && !(empty ($ self ->timings ))) {
140
+ $ timings = $ self ->timings ;
141
+ $ time = microtime (true );
142
+ $ connection = new \Domnikl \Statsd \Connection \UdpSocket (
143
+ $ aConfig ['statsd ' ]['host ' ],
144
+ $ aConfig ['statsd ' ]['port ' ],
145
+ isset ($ aConfig ['statsd ' ]['timeout ' ]) ? $ aConfig ['statsd ' ]['timeout ' ] : 0.5 ,
146
+ isset ($ aConfig ['statsd ' ]['persist ' ]) ? $ aConfig ['statsd ' ]['persist ' ] : true
147
+ );
148
+ $ statsd = new \Domnikl \Statsd \Client ($ connection , $ aConfig ['statsd ' ]['prefix ' ]);
149
+ $ connectionTime = microtime (true ) - $ time ;
150
+ $ statsd ->startBatch ();
151
+ foreach ($ timings as $ prefix => $ metricValues ) {
152
+ foreach ($ metricValues as $ value ) {
153
+ $ statsd ->timing ($ prefix , $ value );
154
+ }
155
+ }
156
+ $ statsd ->endBatch ();
157
+ $ statsd ->timing ('statsd.connection-time ' , $ connectionTime );
158
+ $ statsd ->timing ('statsd.metric-send ' , microtime (true ) - $ time );
159
+ }
160
+ });
140
161
}
141
162
142
163
$ this ->useLocks = $ useLocks ;
@@ -177,18 +198,27 @@ protected function getPrefixByKeys($method, $key) {
177
198
return $ method . '. ' . explode ('- ' , $ key )[0 ];
178
199
}
179
200
201
+ protected function setTiming ($ prefix , $ value ) {
202
+ if (empty ($ this ->timings [$ prefix ])) {
203
+ $ this ->timings [$ prefix ] = [];
204
+ }
205
+ $ this ->timings [$ prefix ][] = $ value ;
206
+ }
207
+
180
208
protected function startTiming ($ prefix ) {
181
209
if ($ this ->metric ) {
182
- $ this ->metric -> startTiming ( $ prefix );
210
+ $ this ->startTimings [ $ prefix] = microtime ( true );
183
211
}
184
212
}
185
213
186
214
protected function stopTiming ($ prefix , $ size = false ) {
187
215
if ($ this ->metric ) {
188
-
7F6A
$ this ->metric ->endTiming ($ prefix );
189
- if ($ size ) {
190
- // I know that is not a timing but statsd make for timing min max
191
- $ this ->metric ->timing ('size. ' . $ prefix , $ size );
216
+ if (!empty ($ this ->startTimings [$ prefix ])) {
217
+ $ this ->setTiming ($ prefix , microtime (true ) - $ this ->startTimings [$ prefix ]);
218
+ unset($ this ->startTimings [$ prefix ]);
219
+ if ($ size ) {
220
+ $ this ->setTiming (sprintf ('size.%s ' , $ prefix ), $ size );
221
+ }
192
222
}
193
223
}
194
224
}
0 commit comments