Skip to content

Instantly share code, notes, and snippets.

@ewandennis
Created November 15, 2016 19:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ewandennis/1dfe07b2dd51850ea5a62c22224afa37 to your computer and use it in GitHub Desktop.
Save ewandennis/1dfe07b2dd51850ea5a62c22224afa37 to your computer and use it in GitHub Desktop.
A cURL connection reuse speed test
<?php
function setoptions($session) {
curl_setopt($session, CURLOPT_TCP_NODELAY, TRUE);
curl_setopt($session, CURLOPT_ENCODING, 'gzip');
// curl_setopt($session, CURLOPT_TCP_FASTOPEN, TRUE);
}
$trans = "{
\"options\": {
\"open_tracking\": true,
\"click_tracking\": true
},
\"campaign_id\": \"php-speed-test\",
\"recipients\": [
{
\"address\": {
\"email\": \"php-speed-test@sink.sparkpostmail.com\",
\"name\": \"PHP Speed Test\"
},
}
],
\"content\": {
\"from\": {
\"name\": \"PHP Speed Test\",
\"email\": \"php-speed-test@cloudygoo.com\"
},
\"subject\": \"PHP Speed Test!\",
\"text\": \"Hi {{address.name}} \nSave big this Christmas in your area {{place}}! \nClick http://www.mysite.com and get huge discount\n Hurry, this offer is only to you\n Bub\",
\"html\": \"<p>Hi {{address.name}} \nSave big this Christmas in your area {{place}}! \nClick http://www.mysite.com and get huge discount\n</p><p>Hurry, this offer is only to you\n</p><p>Bub</p>\"
}
}";
function sendtrans($ch, $apikey, $trans) {
$headers = array(
"Content-Type: application/json",
"Authorization: " . $apikey
);
curl_setopt($ch, CURLOPT_URL, "https://api.sparkpost.com/api/v1/transmissions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $trans);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
error_log(
curl_getinfo($ch, CURLINFO_TOTAL_TIME) . ',' .
curl_getinfo($ch, CURLINFO_NAMELOOKUP_TIME) . ',' .
curl_getinfo($ch, CURLINFO_CONNECT_TIME) . ',' .
curl_getinfo($ch, CURLINFO_PRETRANSFER_TIME) . ',' .
curl_getinfo($ch, CURLINFO_STARTTRANSFER_TIME) . ',' .
curl_getinfo($ch, CURLINFO_SIZE_UPLOAD) . ',' .
curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD) . ',' .
curl_getinfo($ch, CURLINFO_SPEED_DOWNLOAD) . ',' .
curl_getinfo($ch, CURLINFO_SPEED_UPLOAD)
);
// var_dump($response);
}
function withoutReuse($apikey, $trans) {
for ($i = 0; $i < 5; $i++) {
$ch = curl_init();
setoptions($ch);
sendtrans($ch, $apikey, $trans);
curl_close($ch);
}
}
function withReuse($apikey, $trans) {
$ch = curl_init();
for ($i = 0; $i < 5; $i++) {
setoptions($ch);
sendtrans($ch, $apikey, $trans);
}
curl_close($ch);
}
$apikey = 'YOUR_API_KEY';
error_log('total,name lookup,connect,pre transfer,start transfer,size upload,size download,speed download,speed upload');
withoutReuse($apikey, $trans);
error_log('total,name lookup,connect,pre transfer,start transfer,size upload,size download,speed download,speed upload');
withReuse($apikey, $trans);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment