Skip to content

Commit

Permalink
Try to help PHPMailer#1824
Browse files Browse the repository at this point in the history
$ php benchmark_reduce_mime_encoding.phps
...
Size    Method1 Method2 Method3 Method4
1.8kb   0.0600  0.1133  0.0553  0.0974
5.7kb   0.0700  0.1526  0.0598  0.1359
50kb    0.0786  0.1687  0.0592  0.1386
100kb   0.0897  0.1782  0.0619  0.1439
200kb   0.1159  0.2045  0.0662  0.1470
500kb   0.1930  0.2837  0.0707  0.1546
  • Loading branch information
changyy committed Sep 6, 2019
1 parent 2d1ab8e commit 53b55a2
Show file tree
Hide file tree
Showing 3 changed files with 500 additions and 7 deletions.
230 changes: 230 additions & 0 deletions examples/benchmark_reduce_mime_encoding.phps
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<?php
/**
* This example shows sending mail per receiver and reduce MIME encode.
*/

//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

require '../vendor/autoload.php';

//Sample Images from https://sample-videos.com/download-sample-jpg-image.php
// $ curl https://sample-videos.com/img/Sample-jpg-image-1mb.jpg > /tmp/1mb.jpg
//
$image_files = array(
'1.8kb' => 'phpmailer_mini.png',
'5.7kb' => 'images/phpmailer.png',
'50kb' => '/tmp/50kb.jpg',
'100kb' => '/tmp/100kb.jpg',
'200kb' => '/tmp/200kb.jpg',
'500kb' => '/tmp/500kb.jpg',
);

$receiver_count = 200;

$receiver_list = [];
for ($i=0 ; $i<$receiver_count ; ++$i)
$receiver_list[] = 'whoto'.$i.'@example.com';

echo "Genrate $receiver_count receivers: " . count($receiver_list) . "\n";

$results = [];
$test_count_per_run = 10;

echo "Sending $receiver_count mails with creating a new PHPMailer instance:\n";
{
foreach ($image_files as $filesize => $image_file) {
echo "\tWith $filesize attachment, path = $image_file\n";
$total_time = 0;
for ($i=1; $i<=$test_count_per_run ; ++$i) {
$start = microtime(true);
foreach ($receiver_list as $receiver) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = 'mail.example.com';
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = 'yourname@example.com';
$mail->Password = 'yourpassword';
$mail->setFrom('from@example.com', 'First Last');
$mail->addReplyTo('replyto@example.com', 'First Last');
$mail->addAddress($receiver);
$mail->Subject = 'Hi '.$receiver.'!';
$mail->msgHTML(str_replace( '<h1>This is a test of PHPMailer.</h1>', '<h1>Hi '.$receiver.'</h1>', file_get_contents('contents.html')), __DIR__);
$mail->AltBody = 'This is a plain-text message body for '.$receiver;

//Attach an image file
$mail->addAttachment($image_file);

//Build the mail content
$mail->preSend();
unset($mail);
}
$cost = microtime(true) - $start;
$total_time += $cost;
}
$avg_time = $total_time / floatval($test_count_per_run);
echo sprintf("\t\ttest $test_count_per_run runs, avg. time: %.5f seconds\n", $avg_time );

if (!isset($results[$filesize]))
$results[$filesize] = [];
$results[$filesize]['method1'] = $avg_time;
}
}

echo "Sending $receiver_count mails with only one PHPMailer instance: \n";
{
foreach ($image_files as $filesize => $image_file) {
echo "\tWith $filesize attachment, path = $image_file\n";
$total_time = 0;
for ($i=1; $i<=$test_count_per_run ; ++$i) {
//echo "\t\tRun $i: ";
$start = microtime(true);

$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = 'mail.example.com';
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = 'yourname@example.com';
$mail->Password = 'yourpassword';
$mail->setFrom('from@example.com', 'First Last');
$mail->addReplyTo('replyto@example.com', 'First Last');

foreach ($receiver_list as $receiver) {
$mail->clearAddresses();
$mail->addAddress($receiver);
$mail->Subject = 'Hi '.$receiver.'!';
$mail->msgHTML(str_replace( '<h1>This is a test of PHPMailer.</h1>', '<h1>Hi '.$receiver.'</h1>', file_get_contents('contents.html')), __DIR__);
$mail->AltBody = 'This is a plain-text message body for '.$receiver;

//Attach an image file
$mail->addAttachment($image_file);

//Build the mail content
$mail->preSend();
}
$cost = microtime(true) - $start;
//echo "$cost sec\n";
$total_time += $cost;
}
$avg_time = $total_time / floatval($test_count_per_run);
echo sprintf("\t\ttest $test_count_per_run runs, avg. time: %.5f seconds\n", $avg_time );

if (!isset($results[$filesize]))
$results[$filesize] = [];
$results[$filesize]['method2'] = $avg_time;
}
}

echo "Sending $receiver_count mails with creating a new PHPMailer instance and ReduceMIMEEncodeCacheStore: \n";
{
foreach ($image_files as $filesize => $image_file) {
echo "\tWith $filesize attachment, path = $image_file\n";
$total_time = 0;
for ($i=1; $i<=$test_count_per_run ; ++$i) {
//echo "\t\tRun $i: ";
$start = microtime(true);
$cacheLookupTable = [];
foreach ($receiver_list as $receiver) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = 'mail.example.com';
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = 'yourname@example.com';
$mail->Password = 'yourpassword';
$mail->setFrom('from@example.com', 'First Last');
$mail->addReplyTo('replyto@example.com', 'First Last');
$mail->addAddress($receiver);
$mail->Subject = 'Hi '.$receiver.'!';
$mail->msgHTML(str_replace( '<h1>This is a test of PHPMailer.</h1>', '<h1>Hi '.$receiver.'</h1>', file_get_contents('contents.html')), __DIR__);
$mail->AltBody = 'This is a plain-text message body for '.$receiver;

//Attach an image file
$mail->addAttachment($image_file);

$mail->ReduceMIMEEncodeCacheStore = &$cacheLookupTable;

//Build the mail content
$mail->preSend();
unset($mail);
}
$cost = microtime(true) - $start;
//echo "$cost sec\n";
$total_time += $cost;
}
$avg_time = $total_time / floatval($test_count_per_run);
echo sprintf("\t\ttest $test_count_per_run runs, avg. time: %.5f seconds\n", $avg_time );

if (!isset($results[$filesize]))
$results[$filesize] = [];
$results[$filesize]['method3'] = $avg_time;
}

}
echo "Sending $receiver_count mails with only one PHPMailer instance and ReduceMIMEEncodeCacheStore: \n";
{
foreach ($image_files as $filesize => $image_file) {
echo "\tWith $filesize attachment, path = $image_file\n";
$total_time = 0;
for ($i=1; $i<=$test_count_per_run ; ++$i) {
//echo "\t\tRun $i: ";
$start = microtime(true);
$cacheLookupTable = [];

$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Host = 'mail.example.com';
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = 'yourname@example.com';
$mail->Password = 'yourpassword';
$mail->setFrom('from@example.com', 'First Last');
$mail->addReplyTo('replyto@example.com', 'First Last');

foreach ($receiver_list as $receiver) {
$mail->clearAddresses();
$mail->addAddress($receiver);
$mail->Subject = 'Hi '.$receiver.'!';
$mail->msgHTML(str_replace( '<h1>This is a test of PHPMailer.</h1>', '<h1>Hi '.$receiver.'</h1>', file_get_contents('contents.html')), __DIR__);
$mail->AltBody = 'This is a plain-text message body for '.$receiver;

//Attach an image file
$mail->addAttachment($image_file);

$mail->ReduceMIMEEncodeCacheStore = &$cacheLookupTable;

//Build the mail content
$mail->preSend();
}
$cost = microtime(true) - $start;
//echo "$cost sec\n";
$total_time += $cost;
}
$avg_time = $total_time / floatval($test_count_per_run);
echo sprintf("\t\ttest $test_count_per_run runs, avg. time: %.5f seconds\n", $avg_time );

if (!isset($results[$filesize]))
$results[$filesize] = [];
$results[$filesize]['method4'] = $avg_time;
}
}
//
// Print results
echo "\n";
echo "Size\tMethod1\tMethod2\tMethod3\tMethod4\n";
foreach($results as $filesize => $test_method_result) {
echo "$filesize";
foreach($test_method_result as $value)
echo sprintf("\t%3.4f", $value);
echo "\n";
}

0 comments on commit 53b55a2

Please sign in to comment.