File: performance.php

package info (click to toggle)
php-sql-formatter 1.2.17%2Bdct1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 692 kB
  • sloc: php: 1,819; sql: 301; xml: 18; makefile: 17
file content (45 lines) | stat: -rw-r--r-- 1,212 bytes parent folder | download
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
<?php

declare(strict_types=1);

require __DIR__ . '/../vendor/autoload.php';

use Doctrine\SqlFormatter\SqlFormatter;

$formatter = new SqlFormatter();

//the sample query file is filled with install scripts for PrestaShop
//and some sample catalog data from Magento
$contents = file_get_contents(__DIR__ . '/sql.sql');

assert($contents !== false);
$queries = explode("\n---\n", $contents);

//track time and memory usage
$start  = microtime(true);
$ustart = memory_get_usage(true);

//track number of queries and size of queries
$num   = 0;
$chars = 0;

foreach ($queries as $query) {
    //do formatting and highlighting
    $formatter->format($query);

    $num++;
    $chars += strlen($query);
}

$uend = memory_get_usage(true);
$end  = microtime(true);
?>

    <p>Formatted <?= $num ?> queries</p>
    <p>Average query length of <?= number_format($chars / $num, 5) ?> characters</p>
    <p>
        Took <?= number_format($end - $start, 5) ?> seconds total,
        <?= number_format(($end - $start) / $num, 5) ?> seconds per query,
        <?= number_format(1000 * ($end - $start) / $chars, 5) ?> seconds per 1000 characters
    </p>
    <p>Used <?= number_format($uend - $ustart) ?> bytes of memory</p>