1<?php
2
3include __DIR__ . '/../vendor/autoload.php';
4
5if ($argc < 2) {
6    echo "sabre/vobject ", Sabre\VObject\Version::VERSION, " freebusy benchmark\n";
7    echo "\n";
8    echo "This script can be used to measure the speed of generating a\n";
9    echo "free-busy report based on a calendar.\n";
10    echo "\n";
11    echo "The process will be repeated 100 times to get accurate stats\n";
12    echo "\n";
13    echo "Usage: " . $argv[0] . " inputfile.ics\n";
14    die();
15}
16
17list(, $inputFile) = $argv;
18
19$bench = new Hoa\Bench\Bench();
20$bench->parse->start();
21
22$vcal = Sabre\VObject\Reader::read(fopen($inputFile, 'r'));
23
24$bench->parse->stop();
25
26$repeat = 100;
27$start = new \DateTime('2000-01-01');
28$end = new \DateTime('2020-01-01');
29$timeZone = new \DateTimeZone('America/Toronto');
30
31$bench->fb->start();
32
33for ($i = 0; $i < $repeat; $i++) {
34
35    $fb = new Sabre\VObject\FreeBusyGenerator($start, $end, $vcal, $timeZone);
36    $results = $fb->getResult();
37
38}
39$bench->fb->stop();
40
41
42
43echo $bench,"\n";
44
45function formatMemory($input) {
46
47    if (strlen($input) > 6) {
48
49        return round($input / (1024 * 1024)) . 'M';
50
51    } elseif (strlen($input) > 3) {
52
53        return round($input / 1024) . 'K';
54
55    }
56
57}
58
59unset($input, $splitter);
60
61echo "peak memory usage: " . formatMemory(memory_get_peak_usage()), "\n";
62echo "current memory usage: " . formatMemory(memory_get_usage()), "\n";
63