Make WordPress Core

Ticket #17670: get_page_by_path-benchmark.php

File get_page_by_path-benchmark.php, 5.8 KB (added by chrisbliss18, 15 years ago)

Benchmarks the original code verses two different fixes

Line 
1<?php
2
3error_reporting( E_ALL );
4
5$trials = 1000000;
6
7$data = array(
8 array(
9 'result' => 172,
10 'inputs' => array(
11 'a:3:{i:174;O:8:"stdClass":3:{s:2:"ID";s:3:"174";s:9:"post_name";s:7:"level-1";s:11:"post_parent";s:1:"0";}i:173;O:8:"stdClass":3:{s:2:"ID";s:3:"173";s:9:"post_name";s:7:"level-2";s:11:"post_parent";s:3:"174";}i:172;O:8:"stdClass":3:{s:2:"ID";s:3:"172";s:9:"post_name";s:7:"level-3";s:11:"post_parent";s:3:"173";}}',
12 'a:3:{i:0;s:7:"level-1";i:1;s:7:"level-2";i:2;s:7:"level-3";}',
13 ),
14 ),
15 array(
16 'result' => 600,
17 'inputs' => array(
18 'a:1:{i:600;O:8:"stdClass":3:{s:2:"ID";s:3:"600";s:9:"post_name";s:4:"blog";s:11:"post_parent";s:1:"0";}}',
19 'a:1:{i:0;s:4:"blog";}',
20 ),
21 ),
22 array(
23 'result' => 568,
24 'inputs' => array(
25 'a:2:{i:12;O:8:"stdClass":3:{s:2:"ID";s:2:"12";s:9:"post_name";s:5:"about";s:11:"post_parent";s:1:"0";}i:568;O:8:"stdClass":3:{s:2:"ID";s:3:"568";s:9:"post_name";s:17:"about-da-children";s:11:"post_parent";s:2:"12";}}',
26 'a:2:{i:0;s:5:"about";i:1;s:17:"about-da-children";}',
27 ),
28 ),
29 array(
30 'result' => 145,
31 'inputs' => array(
32 'a:2:{i:145;O:8:"stdClass":3:{s:2:"ID";s:3:"145";s:9:"post_name";s:12:"child-page-2";s:11:"post_parent";s:3:"953";}i:953;O:8:"stdClass":3:{s:2:"ID";s:3:"953";s:9:"post_name";s:11:"parent-page";s:11:"post_parent";s:1:"0";}}',
33 'a:2:{i:0;s:11:"parent-page";i:1;s:12:"child-page-2";}',
34 ),
35 ),
36);
37
38$functions = array( 'get_page_by_path', 'get_page_by_path_simple_fix', 'get_page_by_path_rewritten' );
39
40
41
42foreach ( $data as $index => $set )
43 foreach ( $set['inputs'] as $input_index => $input )
44 $data[$index]['inputs'][$input_index] = unserialize( $input );
45
46
47foreach ( $functions as $function ) {
48 $errors = 0;
49
50 foreach ( $data as $index => $set ) {
51 $result = @call_user_func_array( $function, $set['inputs'] );
52
53 if ( $result != $set['result'] ) {
54 echo "$function failed test set $index\n";
55 echo "\tExpected result: {$set['result']}\n";
56 echo "\tActual result: ";
57 var_dump( $result );
58
59 $errors++;
60 }
61 }
62
63 if ( 0 == $errors )
64 echo "$function passed all tests.\n";
65
66 echo "\n";
67}
68
69
70
71$results = array();
72
73for ( $count = 0; $count < $trials; $count++ ) {
74 $function = $functions[ rand( 0, count( $functions ) - 1 ) ];
75
76 if ( ! isset( $results[$function] ) ) {
77 $results[$function] = array(
78 'trials' => 0,
79 'total_time' => 0,
80 'trial_time' => array(),
81 'total_count' => 0,
82 );
83 }
84
85 run_trial( $function );
86}
87
88foreach ( $functions as $function ) {
89 $result = $results[$function];
90
91 echo "$function\n";
92 echo "==========================\n";
93 echo "Trials: {$result['trials']}\n";
94 echo "Total Time: {$result['total_time']}\n";
95 echo "Average Time: " . ( $result['total_time'] / $result['trials'] ) . "\n";
96 echo "Runs per Sec: " . ( 1 / ( $result['total_time'] / $result['trials'] ) ) . "\n";
97
98 if ( isset( $results['no_cast']['total_time'] ) )
99 echo "Speed Difference: " . ( ( $result['total_time'] - $results['no_cast']['total_time'] ) / $results['no_cast']['total_time'] ) . "\n";
100
101 echo "\n";
102}
103
104
105
106function run_trial( $function, $count = 10 ) {
107 global $results, $data;
108
109
110 $time = 0;
111 $total_count = 0;
112
113 for ( $i = 0; $i < $count; $i++ ) {
114 $args = $data[ rand( 0, count( $data ) - 1 ) ]['inputs'];
115
116 $start = microtime( true );
117 @call_user_func_array( $function, $args );
118 $end = microtime( true );
119
120 $time += $end - $start;
121
122 $total_count++;
123 }
124
125
126 $results[$function]['trials']++;
127 $results[$function]['total_time'] += $time;
128 $results[$function]['total_count'] += $total_count;
129}
130
131
132function get_page_by_path( $pages, $parts ) {
133 $revparts = array_reverse( $parts );
134
135 $foundid = 0;
136 foreach ( $pages as $page ) {
137 if ( $page->post_name == $revparts[0] ) {
138 $count = 0;
139 if ( $page->post_parent != 0 ) {
140 if ( null === ( $parent_page = $pages[ $page->post_parent ] ) )
141 continue;
142
143 while ( $parent_page->ID != 0 ) {
144 $count++;
145 if ( $parent_page->post_name != $revparts[ $count ] )
146 break;
147 $parent_page = $pages[ $parent_page->post_parent ];
148 }
149
150 if ( $parent_page->ID == 0 && $count+1 == count($revparts) ) {
151 $foundid = $page->ID;
152 break;
153 }
154 } else if ( count($revparts) == 1 ) {
155 $foundid = $page->ID;
156 break;
157 }
158 }
159 }
160
161 if ( $foundid )
162 return $foundid;
163
164 return null;
165}
166
167function get_page_by_path_simple_fix( $pages, $parts ) {
168 $revparts = array_reverse( $parts );
169
170 $foundid = 0;
171 foreach ( $pages as $page ) {
172 if ( $page->post_name == $revparts[0] ) {
173 $count = 0;
174 if ( $page->post_parent != 0 ) {
175 if ( ! isset( $pages[ $page->post_parent ] ) )
176 continue;
177
178 $parent_page = $pages[ $page->post_parent ];
179
180 while ( ! is_null( $parent_page ) ) {
181 $count++;
182 if ( $parent_page->post_name != $revparts[ $count ] )
183 break;
184 if ( isset( $pages[ $parent_page->post_parent ] ) )
185 $parent_page = $pages[ $parent_page->post_parent ];
186 else
187 $parent_page = null;
188 }
189
190 if ( is_null( $parent_page ) && $count+1 == count($revparts) ) {
191 $foundid = $page->ID;
192 break;
193 }
194 } else if ( count($revparts) == 1 ) {
195 $foundid = $page->ID;
196 break;
197 }
198 }
199 }
200
201 if ( $foundid )
202 return $foundid;
203
204 return null;
205}
206
207function get_page_by_path_rewritten( $pages, $parts ) {
208 $revparts = array_reverse( $parts );
209
210 $foundid = 0;
211 foreach ( $pages as $page ) {
212 if ( $page->post_name == $revparts[0] ) {
213 if ( 1 == count( $revparts ) ) {
214 $foundid = $page->ID;
215 break;
216 }
217
218 $cur_page = $page;
219 $count = 0;
220 while ( ! empty( $cur_page->post_parent ) && isset( $pages[ $cur_page->post_parent ] ) ) {
221 if ( $cur_page->post_name != $revparts[ $count ] )
222 break;
223
224 $cur_page = $pages[ $cur_page->post_parent ];
225 $count++;
226 }
227
228 if ( empty( $cur_page->post_parent ) && $count+1 == count($revparts) ) {
229 $foundid = $page->ID;
230 break;
231 }
232 }
233 }
234
235 if ( $foundid )
236 return $foundid;
237
238 return null;
239}