BACKGROUND: I just migrated my project to a different server (a shared DigitalOcean one, the cheapest offer) with CentOS, and noticed that PHP's imap functions take way more on the new server, than on the old one(like 10x more).
The old server is a dedicated server, and a mail server is also hosted on the old server, the one which I am trying to perform imap actions on.
IMPORTANT: I don't think that this slow down has anything to do with not trying to connect from the same physical server to the mail server anymore, because of the severe increase of the time it requires to run any imap function throughout my project, but anyone can prove me wrong, because I don't know anything about networking unfortunatly:(
I've made some simple script runtime test on both servers, by creating two php files, and testing the execution time of both, one using only php's imap functions (imap_open), and another one, where I am using PEAR Net_IMAP package. Here are the two scripts:
SCRIPT 1:
$start=microtime(true);
$temp=$start;
$mbox = imap_open ("{mymailserver:143/novalidate-cert}INBOX", "email@address.com", "password");
telltime($start, "Connect and Login", $temp);
$mbox = imap_reopen ($mbox,"{mymailserver:143/novalidate-cert}INBOX.Sent");
telltime($start, "Changefolder", $temp);
telltime($start, "End script");
SCRIPT 2:
require_once 'Net/IMAP.php';
$imapServer = new Net_IMAP('mymailserver', 143);
telltime($start, "Connect", $temp);
$loggedIn = $imapServer->login('email@address.com' , 'password');
telltime($start, "Login", $temp);
$imapServer->selectMailbox("INBOX.Sent");
telltime($start, "Change folder", $temp);
telltime($start, "End script");
I've ran these scripts the following way, with the following results:
SCRIPT 1 AS IS - old server
Connect and Login: 0.124350070953
Changefolder: 0.00585293769836
Whole time: 0.130313158035
SCRIPT 1 AS IS - new server
Connect and Login: 0.63277888298035
Changefolder: 0.15838479995728
Whole time: 0.79174709320068
SCRIPT 1 /novalidate-cert changed to /notls - old server
Connect and Login: 0.112071990967
Changefolder: 0.00407910346985
Whole time: 0.116246938705
SCRIPT 1 /novalidate-cert changed to /notls - new server
Connect and Login: 0.50686407089233
Changefolder: 0.17428183555603
Whole time: 0.68127012252808
SCRIPT 2 AS IS - new server
Connect: 0.42295503616333
Login: 0.4013729095459
Change folder: 0.057337045669556
End script: 0.88185501098633
The project also has a console based debugging system, from which I've managed to gather the following information:
- an average search to the mailbox takes around 0.01-0.02 seconds on the old server, while on the new server, the same search takes around 7-8x times more
- the fetching of a single e-mail from the mail server on the old server takes between 0.05s-0.1s while on the new server, there are e-mails(mainly those which have Text/HTML with attached image files) which take 4 seconds to fetch
Based on these results, I am presuming that I am facing a networking problem, but it is just a wild assumption, as till now I've never debugged networking errors, but I already took apart my php scripts, and I can't find any error in them.
I tried traceroute-ing and ping-ing the mail server from the new project environment, and I've got the following results:
traceroute to mymailserver (xxx.xxx.xxx.xx), 30 hops max, 60 byte packets
1 xxx.xxx.x.xxx 0.658 ms xxx.xxx.x.xxx 0.510 ms xxx.xxx.x.xxx 0.471 ms
2 xxx.xxx.xxx.xxx 0.434 ms xxx.xxx.xxx.xxx 0.333 ms xxx.xxx.xxx.xxx 0.247 ms
3 xxx.xxx.xxx.xx 0.984 ms 0.986 ms xxx.xxx.xxx.xxx 0.270 ms
4 xxx.xxx.xxx.xx 0.964 ms xxx.xxx.xx.xxx 1.414 ms 1.449 ms
5 xxx.xxx.xx.xxx 1.253 ms 1.211 ms xxx.xxx.xx.xxx 22.078 ms
6 xxx.xxx.xx.xxx 43.920 ms 41.971 ms 44.860 ms
7 xx.xx.xx.xxx 45.835 ms xxx.xxx.xx.xxx 42.055 ms 41.254 ms
8 * xxx.xxx.xxx.xxx 42.999 ms *
9 xxx.xxx.xxx.xx 41.989 ms 42.235 ms 44.925 ms
Yes, sometimes traceroute reports about lost packages, but not always, but the rest of this information unfortunatly is only giberish to me, cause I don't understand for what I have to look for, and I didn't find any usable tutorial for traceroute on the internet.
** OTHER INFORMATION BASED ON THE HELP FROM OTHER STACKOVERFLOW USERS: **
I've also downloaded xDebug, and tried function tracing the two above mentioned scripts. While I didn't get any usefull information by function tracing imap_open function, I've noticed that when I function trace the PEAR package, the fgets() that are performed somewhere in the class, take considerably more time, than any other function that is being run(around 0.05 seconds)
My question(s):
- Am I right in assuming from these information that this has to be a networking problem?
- If yes, how could I solve it, or what are the giveaways that this is indeed a networking problem?
- If no, how could I isolate the problem better, and obtain a solution to it?
I am offering a:
- a thank you, to someone who helps me isolate if this is a networking problem, or something else
- a bounty, if someone manages to help me solve this issue, and speed up the processing of imap functions