forked from halilim/xml-iterator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.php
56 lines (48 loc) · 1.61 KB
/
benchmark.php
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
<?php
/**
* @author Halil Özgür | halil · ozgur |o| gmail.com
*
* Usage: php benchmark.php <FILE_URI/PATH> <DELIMITER_TAG> <METHOD>, e.g.:
* php benchmark.php example.xml product 1
* php benchmark.php http://www.example.com/example.xml product 2
*
* Use with very large files (50 MB+) and watch the memory usage.
*/
$url = $argv[1];
$elemTagName = $argv[2];
$startTime = microtime(true);
$ct = 0;
$method = "";
switch ($argv[3]) {
case 1:
// V1
$method = "XmlIterator\\XmlIterator";
require_once "XmlIterator/XmlIterator.php";
$it = new XmlIterator\XmlIterator($url, $elemTagName);
foreach ($it as $k => $v) {
$ct++;
// echo $k . " => " . var_export($v, true) . "\n\n";
}
break;
case 2:
// V2 the memory aggressor
$method = "SPL SimpleXMLIterator";
require_once "XmlIterator/Utf8Filter.php";
stream_filter_register('xmlutf8', "XmlIterator\\Utf8Filter");
$it = new SimpleXMLIterator("php://filter/read=xmlutf8/resource=" . $url, LIBXML_NOENT | LIBXML_NOCDATA, true);
foreach ($it->{$elemTagName} as $v) {
$ct++;
// echo var_export($v, true) . "\n\n";
// echo (string)$v->element_1 . "\n\n";
}
break;
default:
break;
}
$time = microtime(true) - $startTime;
echo "\n";
echo "method : $method\n";
echo "elem count : $ct\n";
echo "time passed : $time s\n";
// Useless since the memory used by libxml libraries is not reported
//echo "peak memory : " . (memory_get_peak_usage(true) / pow(2, 20)) . " MiB\n";