Last active
January 1, 2016 00:19
-
-
Save clonemeagain/8066113 to your computer and use it in GitHub Desktop.
Test file to analyze the best algorithm for parsePriority.. ;-) Modified to include "Importance" header and string based values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define ( 'HIGH_PRIORITY', 1 ); | |
define ( 'NORMAL_PRIORITY', 2 ); | |
define ( 'LOW_PRIORITY', 3 ); | |
define ( 'NO_PRIORITY', 0 ); | |
echo "Time to beat: 0.027ms\n"; | |
test ( 'StrPos', 'strposversion' ); | |
test ( 'PregMatch', 'pregmatchversion' ); | |
function test($name, $func) { | |
$fastest = 1; | |
$total = 0; | |
for($i = 0; $i < 10000; $i ++) { | |
$starttime = microtime ( true ); | |
foreach ( array ( | |
'X-Priority: isNAN' => NO_PRIORITY, // input => output | |
'X-Priority: 1' => LOW_PRIORITY, | |
'X-Priority: 2' => LOW_PRIORITY, | |
'X-Priority: 3' => NORMAL_PRIORITY, | |
'X-Priority: 4' => NORMAL_PRIORITY, | |
'X-Priority: 5' => HIGH_PRIORITY, | |
'X-Priority: 6' => HIGH_PRIORITY, | |
'No priority set' => NO_PRIORITY, | |
'Priority: normal' => NORMAL_PRIORITY, | |
'xyz-priority: high' => HIGH_PRIORITY, | |
'Priority: high' => HIGH_PRIORITY, | |
'priority: low' => LOW_PRIORITY, | |
'x-priority: 1000' => LOW_PRIORITY, // only matches first 1, not the full 1000 | |
'priority: 3' => NORMAL_PRIORITY, | |
'IPM-Importance: low' => LOW_PRIORITY, | |
'My-Importance: URGENT' => HIGH_PRIORITY, | |
'Urgency: High' => NO_PRIORITY, //urgency doesn't match.. maybe it should? | |
'X-Importance: 5' => HIGH_PRIORITY, | |
'' => NO_PRIORITY | |
) as $priority => $response ) { | |
if (! (call_user_func ( $func, h ( $priority ) ) == $response)) | |
throw new Exception ( "Algorithm mistake: $priority should return $response!" ); | |
} | |
$time = (microtime ( true ) - $starttime); | |
if ($time < $fastest) | |
$fastest = $time; | |
$total += $time; | |
} | |
print $name . ': ' . number_format ( $fastest * 1000, 3 ) . "ms, total: " . number_format ( $total, 3 ) . "s\n"; | |
} | |
// runs in 0.350ms, on my laptop, seems to be pretty useful | |
function pregmatchversion($header = null) { | |
if (! $header) | |
return 0; | |
// Test for normal "X-Priority: INT" style header & stringy version. | |
// Allows for Importance possibility. | |
if (preg_match ( '/priority: (\d|\w)/i', $header, $matching_char ) | |
|| preg_match ( '/importance: (\d|\w)/i', $header, $matching_char )) { | |
switch ($matching_char[1]) { | |
case 'h' : | |
case 'H' :// high | |
case 'u': | |
case 'U': //Urgent | |
case 6 : | |
case 5 : | |
return 1; | |
case 'n' : // normal | |
case 'N' : | |
case 4 : | |
case 3 : | |
return 2; | |
case 'l' : // low | |
case 'L' : | |
case 2 : | |
case 1 : | |
return 3; | |
} | |
} | |
return 0; | |
} | |
// runs in 0.216ms, on my laptop, obviously VERY rigidly defined.. | |
function strposversion($header = null) { | |
if (! $header) | |
return 0; | |
// Get the number of characters before the start of the header we are after. | |
if ($p_header = stripos ( $header, 'priority:' )) { | |
// From start of the priority header, capture a single char to indicate priority | |
if (! $priority_char = substr ( $header, ($p_header + 10), 1 ))// "Priority: + space" is 10 characters | |
return 0; | |
} elseif ($p_header = stripos ( $header, 'importance:' )) { | |
if (! $priority_char = substr ( $header, ($p_header + 12), 1 ))// "Importance: + space" is 12 chars. | |
return 0; | |
}else{ | |
return 0; | |
} | |
switch ($priority_char) { | |
case 'u': | |
case 'U': //Urgent | |
case 'h' ://High | |
case 'H' : // was faster to include caps in switch than call strtolower on all. | |
case 6 : | |
case 5 : | |
return 1; | |
case 'n' : //Normal priority (or no) | |
case 'N' : | |
case 4 : | |
case 3 : | |
return 2; | |
case 'l' : //Low priority | |
case 'L' : | |
case 2 : | |
case 1 : | |
return 3; | |
} | |
return 0; | |
} | |
function h($setPriority = "") { | |
return <<<HEADER | |
Delivered-To: clonemeagain@gmail.com | |
Received: by 10.69.18.42 with SMTP id gj10csp88238pbd; | |
Fri, 20 Dec 2013 10:08:25 -0800 (PST) | |
X-Received: by 10.224.13.80 with SMTP id b16mr16256982qaa.73.1387562904239; | |
Fri, 20 Dec 2013 10:08:24 -0800 (PST) | |
Return-Path: <noreply@github.com> | |
Received: from github-smtp2a-ext-cp1-prd.iad.github.net (github-smtp2-ext5.iad.github.net. [192.30.252.196]) | |
by mx.google.com with ESMTPS id k3si6568083qao.74.2013.12.20.10.08.23 | |
for <clonemeagain@gmail.com> | |
(version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); | |
Fri, 20 Dec 2013 10:08:23 -0800 (PST) | |
Received-SPF: pass (google.com: domain of noreply@github.com designates 192.30.252.196 as permitted sender) client-ip=192.30.252.196; | |
Authentication-Results: mx.google.com; | |
spf=pass (google.com: domain of noreply@github.com designates 192.30.252.196 as permitted sender) smtp.mail=noreply@github.com | |
Date: Fri, 20 Dec 2013 10:08:23 -0800 | |
From: Jared Hancock <notifications@github.com> | |
Reply-To: "osTicket/osTicket-1.8" <reply+i-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH@reply.github.com> | |
To: "osTicket/osTicket-1.8" <osTicket-1.8@noreply.github.com> | |
Cc: clonemeagain <clonemeagain@gmail.com> | |
Message-ID: <osTicket/osTicket-1.8/pull/336/issue_event/82864993@github.com> | |
In-Reply-To: <osTicket/osTicket-1.8/pull/336@github.com> | |
References: <osTicket/osTicket-1.8/pull/336@github.com> | |
Subject: Re: [osTicket-1.8] Landing page inline image correction (#336) | |
Mime-Version: 1.0 | |
Content-Type: multipart/alternative; | |
boundary="--==_mimepart_52b4879729712_d621217cfc567e3"; | |
charset=UTF-8 | |
Content-Transfer-Encoding: 7bit | |
Precedence: list | |
X-GitHub-Recipient: clonemeagain | |
X-GitHub-Reason: author | |
List-ID: osTicket/osTicket-1.8 <osTicket-1.8.osTicket.github.com> | |
List-Archive: https://github.com/osTicket/osTicket-1.8 | |
List-Post: <mailto:reply+i-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH@reply.github.com> | |
List-Unsubscribe: <mailto:unsub+i-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH@reply.github.com>, | |
<https://github.com/notifications/unsubscribe/BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISH-BUNCHORANDOMGIBBERIBBERISHBUNCHORANDOMGIBBERIBBERISHBUNCHORANDOMGIBBERIBBERISH> | |
X-Auto-Response-Suppress: All | |
X-GitHub-Recipient-Address: clonemeagain@gmail.com | |
$setPriority | |
HEADER; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There it is.. added total time counter.. ;-)
My laptop is 5 years old.. I need to talk to my bosses about getting some proper servers.. FFFS.