-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssutil.php
148 lines (118 loc) · 4.62 KB
/
ssutil.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
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
<?php
require_once('sstiles.php');
class ssutil extends sstiles{
/**
* Determine the ideal zoom (least lossy) for a given image
*/
function idealZoom(){
$min = FALSe;
if(extension_loaded('gmagick')){
$image = new Gmagick($this->mapfile);
$min = min($image->getimagewidth,$image->getimageheight);
}else if(extension_loaded("magickwand")) {
$image = NewMagickWand();
MagickReadImage($image,$this->mapfile);
$min = min(MagickGetImageWidth($image),MagickGetImageHeight($image));
}else if(extension_loaded("imagick")) {
$image = new Imagick($this->mapfile);
$ident = $image->identifyImage();
$min = min($ident['geometry']['width'],$ident['geometry']['height']);
}else if(`which convert` != ''){
$identcmd = "convert " . escapeshellarg($this->mapfile) . " -format '%w,%h' info:";
$ident = explode(',',`$identcmd`);
$min = min($ident);
}else if(extension_loaded('gd')){
$min = min(getimagesize($this->mapfile));
}
// I have litterally never needed a log function until now.
return round(log($min/256,2));
}
/**
* benchmark each available method
*
* Make $this->benchmarkCount tiles with each available method.
*
* Might be better to run this on the command line to avoid PHP timeouts
*
* Returns a hash with the method as the key and the time it took to run in seconds.
*/
function benchmark($rounds = 1,$outputCSV = TRUE){
$benchmarkstart = microtime(TRUE);
// Try to remove the time limit
@set_time_limit(0);
// Figure out which methods we can test
$benchmarkMethods = Array(
'makeCacheMagickWand' => extension_loaded("magickwand"),
'makeCacheGD' => extension_loaded('gd'),
'makeCacheGM' => extension_loaded('gmagick'),
'makeCacheIM' => extension_loaded("imagick"),
'makeCacheIMExec' => (strlen(`which convert`) > 0),
);
// capture headers which have already been set
$headers = headers_list();
error_log("Starting tile benchmark with $rounds rounds for each enabled method");
foreach($benchmarkMethods as $method => $results){
if($results === FALSE){
$benchmarkMethods[$method] = "Not available to test";
continue;
}
$benchmarkMethods[$method] = $this->benchmarkMethod($method,$rounds,$outputCSV);
}
header_remove();
foreach($headers as $header){
header($header);
}
$benchmarkMethods['TOTAL TIME'] = (microtime(TRUE) - $benchmarkstart);
return $benchmarkMethods;
}
function benchmarkMethod($method,$rounds,$outputCSV = TRUE){
error_log("Testing $method");
$tilesAtZoom = pow(2,$this->zoom);
$tilesFound = 0;
$starttime = microtime(TRUE);
$maxtime = 0;
$mintime = 999999999999999;
while($tilesFound < $rounds){
for($x = 0;$x<$tilesAtZoom;$x++){
for($y = 0;$y<$tilesAtZoom;$y++){
$roundstart = microtime(TRUE);
if($tilesFound == $rounds){
break(3);
}
$this->x = $x;
$this->y = $y;
$this->prepCache();
try {
$this->$method();
}catch (Exception $e){
error_log("Benchmark error caught: " . $e->getMessage());
break(3);
}
// discard image printed to browser
$tilesFound++;
$roundstop = microtime(TRUE);
$roundtime = $roundstop - $roundstart;
if($outputCSV){
error_log("$method,$roundtime");
}
if($roundtime > $maxtime){
$maxtime = $roundtime;
}
if($roundtime < $mintime){
$mintime = $roundtime;
}
}
}
}
$stoptime = microtime(TRUE);
$runtime = $stoptime - $starttime;
return Array(
'iterations' => $tilesFound,
'totaltime' => $runtime,
'avgtime' => ($runtime / $tilesFound),
'mintime' => $mintime,
'maxtime' => $maxtime,
'variation' => ($maxtime - $mintime),
);
}
}