File: Zend_Db_Profiler.xml

package info (click to toggle)
zendframework 1.12.9%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 133,584 kB
  • sloc: xml: 1,311,829; php: 570,173; sh: 170; makefile: 125; sql: 121
file content (424 lines) | stat: -rw-r--r-- 16,190 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.db.profiler" xmlns:xi="http://www.w3.org/2001/XInclude">
    <title>Zend_Db_Profiler</title>

    <sect2 id="zend.db.profiler.introduction">
        <title>Introduction</title>

        <para>
            <classname>Zend_Db_Profiler</classname> can be enabled to allow profiling of
            queries. Profiles include the queries processed by the adapter as
            well as elapsed time to run the queries, allowing inspection of the
            queries that have been performed without needing to add extra
            debugging code to classes. Advanced usage also allows the
            developer to filter which queries are profiled.
        </para>

        <para>
            Enable the profiler by either passing a directive to the adapter
            constructor, or by asking the adapter to enable it later.
        </para>

        <programlisting language="php"><![CDATA[
$params = array(
    'host'     => '127.0.0.1',
    'username' => 'webuser',
    'password' => 'xxxxxxxx',
    'dbname'   => 'test'
    'profiler' => true  // turn on profiler
                        // set to false to disable (disabled by default)
);

$db = Zend_Db::factory('PDO_MYSQL', $params);

// turn off profiler:
$db->getProfiler()->setEnabled(false);

// turn on profiler:
$db->getProfiler()->setEnabled(true);
]]></programlisting>

        <para>
            The value of the '<property>profiler</property>' option is flexible. It is interpreted
            differently depending on its type. Most often, you should use a simple boolean value,
            but other types enable you to customize the profiler behavior.
        </para>

        <para>
            A boolean argument sets the profiler to enabled if it is a <constant>TRUE</constant>
            value, or disabled if <constant>FALSE</constant>. The profiler class is the adapter's
            default profiler class, <classname>Zend_Db_Profiler</classname>.
        </para>

        <programlisting language="php"><![CDATA[
$params['profiler'] = true;
$db = Zend_Db::factory('PDO_MYSQL', $params);
]]></programlisting>

        <para>
            An instance of a profiler object makes the adapter use that object. The object type must
            be <classname>Zend_Db_Profiler</classname> or a subclass thereof. Enabling the profiler
            is done separately.
        </para>

        <programlisting language="php"><![CDATA[
$profiler = MyProject_Db_Profiler();
$profiler->setEnabled(true);
$params['profiler'] = $profiler;
$db = Zend_Db::factory('PDO_MYSQL', $params);
]]></programlisting>

        <para>
            The argument can be an associative array containing any or all of the keys
            '<property>enabled</property>', '<property>instance</property>', and
            '<property>class</property>'. The '<property>enabled</property>' and
            '<property>instance</property>' keys correspond to the boolean and instance types
            documented above. The '<property>class</property>' key is used to name a class to
            use for a custom profiler. The class must be <classname>Zend_Db_Profiler</classname> or
            a subclass. The class is instantiated with no constructor arguments. The
            '<property>class</property>' option is ignored when the '<property>instance</property>'
            option is supplied.
        </para>

        <programlisting language="php"><![CDATA[
$params['profiler'] = array(
    'enabled' => true,
    'class'   => 'MyProject_Db_Profiler'
);
$db = Zend_Db::factory('PDO_MYSQL', $params);
]]></programlisting>

        <para>
            Finally, the argument can be an object of type <classname>Zend_Config</classname>
            containing properties, which are treated as the array keys described above. For example,
            a file "<filename>config.ini</filename>" might contain the following data:
        </para>

        <programlisting language="ini"><![CDATA[
[main]
db.profiler.class   = "MyProject_Db_Profiler"
db.profiler.enabled = true
]]></programlisting>

        <para>
            This configuration can be applied by the following <acronym>PHP</acronym> code:
        </para>

        <programlisting language="php"><![CDATA[
$config = new Zend_Config_Ini('config.ini', 'main');
$params['profiler'] = $config->db->profiler;
$db = Zend_Db::factory('PDO_MYSQL', $params);
]]></programlisting>

        <para>
            The '<property>instance</property>' property may be used as in the following:
        </para>

        <programlisting language="php"><![CDATA[
$profiler = new MyProject_Db_Profiler();
$profiler->setEnabled(true);
$configData = array(
    'instance' => $profiler
    );
$config = new Zend_Config($configData);
$params['profiler'] = $config;
$db = Zend_Db::factory('PDO_MYSQL', $params);
]]></programlisting>
    </sect2>

    <sect2 id="zend.db.profiler.using">
        <title>Using the Profiler</title>

        <para>
            At any point, grab the profiler using the adapter's
            <methodname>getProfiler()</methodname> method:
        </para>

        <programlisting language="php"><![CDATA[
$profiler = $db->getProfiler();
]]></programlisting>

        <para>
            This returns a <classname>Zend_Db_Profiler</classname> object instance. With
            that instance, the developer can examine your queries using a
            variety of methods:
        </para>

        <itemizedlist>
            <listitem>
                <para>
                    <methodname>getTotalNumQueries()</methodname> returns the total number
                    of queries that have been profiled.
                </para>
            </listitem>

            <listitem>
                <para>
                    <methodname>getTotalElapsedSecs()</methodname> returns the total
                    number of seconds elapsed for all profiled queries.
                </para>
            </listitem>

            <listitem>
                <para>
                    <methodname>getQueryProfiles()</methodname> returns an array of all
                    query profiles.
                </para>
            </listitem>

            <listitem>
                <para>
                    <methodname>getLastQueryProfile()</methodname> returns the last (most
                    recent) query profile, regardless of whether or not the query
                    has finished (if it hasn't, the end time will be <constant>NULL</constant>)
                </para>
            </listitem>

            <listitem>
                <para>
                    <methodname>clear()</methodname> clears any past query profiles
                    from the stack.
                </para>
            </listitem>
        </itemizedlist>

        <para>
            The return value of <methodname>getLastQueryProfile()</methodname> and the
            individual elements of <methodname>getQueryProfiles()</methodname> are
            <classname>Zend_Db_Profiler_Query</classname> objects, which provide the
            ability to inspect the individual queries themselves:
        </para>

        <itemizedlist>
            <listitem>
                <para>
                    <methodname>getQuery()</methodname> returns the <acronym>SQL</acronym> text of
                    the query. The <acronym>SQL</acronym> text of a prepared statement with
                    parameters is the text at the time the query was prepared, so it contains
                    parameter placeholders, not the values used when the
                    statement is executed.
                </para>
            </listitem>

            <listitem>
                <para>
                    <methodname>getQueryParams()</methodname> returns an array of
                    parameter values used when executing a prepared query.
                    This includes both bound parameters and arguments to the
                    statement's <methodname>execute()</methodname> method. The keys of
                    the array are the positional (1-based) or named (string)
                    parameter indices.
                </para>
            </listitem>

            <listitem>
                <para>
                    <methodname>getElapsedSecs()</methodname> returns the number of
                    seconds the query ran.
                </para>
            </listitem>
        </itemizedlist>

        <para>
            The information <classname>Zend_Db_Profiler</classname> provides is useful for
            profiling bottlenecks in applications, and for debugging queries
            that have been run. For instance, to see the exact query that was
            last run:
        </para>

        <programlisting language="php"><![CDATA[
$query = $profiler->getLastQueryProfile();

echo $query->getQuery();
]]></programlisting>

        <para>
            Perhaps a page is generating slowly; use the profiler to determine
            first the total number of seconds of all queries, and then step
            through the queries to find the one that ran longest:
        </para>

        <programlisting language="php"><![CDATA[
$totalTime    = $profiler->getTotalElapsedSecs();
$queryCount   = $profiler->getTotalNumQueries();
$longestTime  = 0;
$longestQuery = null;

foreach ($profiler->getQueryProfiles() as $query) {
    if ($query->getElapsedSecs() > $longestTime) {
        $longestTime  = $query->getElapsedSecs();
        $longestQuery = $query->getQuery();
    }
}

echo 'Executed ' . $queryCount . ' queries in ' . $totalTime .
     ' seconds' . "\n";
echo 'Average query length: ' . $totalTime / $queryCount .
     ' seconds' . "\n";
echo 'Queries per second: ' . $queryCount / $totalTime . "\n";
echo 'Longest query length: ' . $longestTime . "\n";
echo "Longest query: \n" . $longestQuery . "\n";
]]></programlisting>
    </sect2>

    <sect2 id="zend.db.profiler.advanced">
        <title>Advanced Profiler Usage</title>

        <para>
            In addition to query inspection, the profiler also allows the
            developer to filter which queries get profiled. The following
            methods operate on a <classname>Zend_Db_Profiler</classname> instance:
        </para>

        <sect3 id="zend.db.profiler.advanced.filtertime">
            <title>Filter by query elapsed time</title>

            <para>
                <methodname>setFilterElapsedSecs()</methodname> allows the developer to set
                a minimum query time before a query is profiled. To remove the
                filter, pass the method a <constant>NULL</constant> value.
            </para>

            <programlisting language="php"><![CDATA[
// Only profile queries that take at least 5 seconds:
$profiler->setFilterElapsedSecs(5);

// Profile all queries regardless of length:
$profiler->setFilterElapsedSecs(null);
]]></programlisting>
        </sect3>

        <sect3 id="zend.db.profiler.advanced.filtertype">
            <title>Filter by query type</title>

            <para>
                <methodname>setFilterQueryType()</methodname> allows the developer to set
                which types of queries should be profiled; to profile multiple
                types, logical OR them. Query types are defined as the following
                <classname>Zend_Db_Profiler</classname> constants:
            </para>

            <itemizedlist>
                <listitem>
                    <para>
                        <constant>Zend_Db_Profiler::CONNECT</constant>: connection
                        operations, or selecting a database.
                    </para>
                </listitem>

                <listitem>
                    <para>
                        <constant>Zend_Db_Profiler::QUERY</constant>: general database
                        queries that do not match other types.
                    </para>
                </listitem>

                <listitem>
                    <para>
                        <constant>Zend_Db_Profiler::INSERT</constant>: any query that
                        adds new data to the database, generally <acronym>SQL</acronym>
                        <acronym>INSERT</acronym>.
                    </para>
                </listitem>

                <listitem>
                    <para>
                        <constant>Zend_Db_Profiler::UPDATE</constant>: any query that
                        updates existing data, usually <acronym>SQL</acronym>
                        <acronym>UPDATE</acronym>.
                    </para>
                </listitem>

                <listitem>
                    <para>
                        <constant>Zend_Db_Profiler::DELETE</constant>: any query that
                        deletes existing data, usually <acronym>SQL</acronym>
                        <constant>DELETE</constant>.
                    </para>
                </listitem>

                <listitem>
                    <para>
                        <constant>Zend_Db_Profiler::SELECT</constant>: any query that
                        retrieves existing data, usually <acronym>SQL</acronym>
                        <acronym>SELECT</acronym>.
                    </para>
                </listitem>

                <listitem>
                    <para>
                        <constant>Zend_Db_Profiler::TRANSACTION</constant>: any
                        transactional operation, such as start transaction, commit,
                        or rollback.
                    </para>
                </listitem>
            </itemizedlist>

            <para>
                As with <methodname>setFilterElapsedSecs()</methodname>, you can remove any
                existing filters by passing <constant>NULL</constant> as the sole
                argument.
            </para>

            <programlisting language="php"><![CDATA[
// profile only SELECT queries
$profiler->setFilterQueryType(Zend_Db_Profiler::SELECT);

// profile SELECT, INSERT, and UPDATE queries
$profiler->setFilterQueryType(Zend_Db_Profiler::SELECT |
                              Zend_Db_Profiler::INSERT |
                              Zend_Db_Profiler::UPDATE);

// profile DELETE queries
$profiler->setFilterQueryType(Zend_Db_Profiler::DELETE);

// Remove all filters
$profiler->setFilterQueryType(null);
]]></programlisting>
        </sect3>

        <sect3 id="zend.db.profiler.advanced.getbytype">
            <title>Retrieve profiles by query type</title>

            <para>
                Using <methodname>setFilterQueryType()</methodname> can cut down on the
                profiles generated. However, sometimes it can be more useful to
                keep all profiles, but view only those you need at a given
                moment. Another feature of <methodname>getQueryProfiles()</methodname> is
                that it can do this filtering on-the-fly, by passing a query
                type (or logical combination of query types) as its first
                argument; see <link linkend="zend.db.profiler.advanced.filtertype">this
                    section</link> for a list of the query type constants.
            </para>

            <programlisting language="php"><![CDATA[
// Retrieve only SELECT query profiles
$profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::SELECT);

// Retrieve only SELECT, INSERT, and UPDATE query profiles
$profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::SELECT |
                                        Zend_Db_Profiler::INSERT |
                                        Zend_Db_Profiler::UPDATE);

// Retrieve DELETE query profiles
$profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::DELETE);
]]></programlisting>
        </sect3>
    </sect2>

    <sect2 id="zend.db.profiler.profilers">
        <title>Specialized Profilers</title>

        <para>
            A Specialized Profiler is an object that inherits from
            <classname>Zend_Db_Profiler</classname>. Specialized Profilers treat
            profiling information in specific ways.
        </para>

        <xi:include href="Zend_Db_Profiler-Firebug.xml" />
    </sect2>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->