%3CLINGO-SUB%20id%3D%22lingo-sub-1083187%22%20slang%3D%22en-US%22%3ELesson%20Learned%20%23117%3A%20Testing%20the%20connection%20time%20using%20Azure%20Database%20for%20MySQL%20and%20PHP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1083187%22%20slang%3D%22en-US%22%3E%3CP%3EToday%2C%20I%20worked%20on%20a%20very%20interesting%20service%20request%20that%20our%20customer%20noticed%20some%20milliseconds%20of%20difference%20connecting%20a%20MySQL%20local%20environment%20and%20Azure%20Database%20for%20MySQL%20using%20PHP.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20first%20point%20that%20we%20need%20to%20know%20that%20these%20milliseconds%20of%20difference%20is%20expected%20because%20when%20you%20are%20connecting%20to%20Azure%20Database%20for%20MySQL%20all%20connections%20will%20be%20made%20against%20a%20proxy%20server%20as%20you%20could%20see%20in%20this%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2FeN-US%2Fazure%2Fmysql%2Fconcepts-connectivity-architecture%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EURL%3C%2FA%3E.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20this%20reason%2C%20we%20have%20some%20best%20practices%20and%20recomendations%20that%20my%20colleague%20Bashar%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FAzure-Database-for-MySQL%2FAzure-Database-for-MySQL-Performance-Troubleshooting-Basics%2Fba-p%2F782815%22%20target%3D%22_self%22%3Ereported%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20article%20just%20wanted%20to%20share%20the%20two%20examples%20that%20how%20to%20show%20you%20measure%20the%20results%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1060399160%22%20id%3D%22toc-hId-1060399160%22%3E%3CSTRONG%3EIn%20Summary%3C%2FSTRONG%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EI%20did%20several%20tests%20using%20mysqlcli%20and%20PDO%20drivers%20using%20a%20sample%20PHP%20application.%26nbsp%3B%3C%2FLI%3E%0A%3CLI%3EAll%20test%20have%20been%20done%20in%20NorthEurope.%3C%2FLI%3E%0A%3CLI%3EI%20installed%20%3CSTRONG%3EPHP%20version%20-%20php-7.3.13-Win32-VC15-x64%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CUL%3E%0A%3CLI%3EI%20created%20a%20virtual%20machine%20and%20installed%20a%20MySQL%20server%20v.8.0.18%2C%20with%20a%20model%20that%20allows%20me%20to%20select%20Accelerated%20networking.%3C%2FLI%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EComputer%20%3C%2FSTRONG%3E%3CSTRONG%3ESize%3C%2FSTRONG%3E%3A%20Standard%20D4s%20v3%20(4%20vcpus%2C%2016%20GiB%20memory)%26nbsp%3Band%20%3CSTRONG%3Eaccelerated%20networking%20enabled%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CLI%3E%3CSTRONG%3EI%20created%20an%20Azure%20Database%20for%20MySQL%3C%2FSTRONG%3E%3A%3CUL%3E%0A%3CLI%3EMySQL%20%3CSTRONG%3EVersion%3C%2FSTRONG%3E%3A%205.6%2C%20Memory%20Optimized%2C%208%20vCore(s)%2C%201018%20GB%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3ESSL%20Enforce%20status%3A%3C%2FSTRONG%3E%20DISABLED%3C%2FLI%3E%0A%3CLI%3EInitial%20configuration%20without%20any%20parameter%20modification.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH3%20id%3D%22toc-hId--747055303%22%20id%3D%22toc-hId--747055303%22%3E%3CSTRONG%3EResults%20running%20on%20Azure%3C%2FSTRONG%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId--56493829%22%20id%3D%22toc-hId--56493829%22%3E%3CSTRONG%3ETest%20using%20PDO%3A%3C%2FSTRONG%3E%3C%2FH4%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EI%20developed%20this%20small%20application%2C%20using%20persistent%20connection%2C%20running%201000%20connections%20opening%2Fclosing%20and%20SELECT%201%20and%20the%20results%20of%20the%20connections%20are%20more%20faster.%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EI%20developed%20this%20small%20application%2C%20using%20non-persistent%20connection%2C%20running%201000%20connections%20opening%2Fclosing%20and%20SELECT%201.%20You%20could%20find%20a%20slightly%20delay%20in%20the%20results.%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId--1863948292%22%20id%3D%22toc-hId--1863948292%22%3E%3CSTRONG%3ETest%20using%20mySQLcli%20I%20got%20the%20same%20results%20using%20connection%20pooling%2Fpersistent%20connections%3A%3C%2FSTRONG%3E%3C%2FH4%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EI%20developed%20this%20small%20application%2C%20using%20persistent%20connection%2C%20running%201000%20connections%20opening%2Fclosing%20and%20SELECT%201%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EI%20developed%20this%20small%20application%2C%20without%20using%20persistent%20connection%2C%20running%201000%20connections%20opening%2Fclosing%20and%20SELECT%201%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1874451396%22%20id%3D%22toc-hId--1874451396%22%3E%3CSTRONG%3EResults%20running%20on%20Local%20Environment%3C%2FSTRONG%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId--1183889922%22%20id%3D%22toc-hId--1183889922%22%3E%3CSTRONG%3ETest%20using%20PDO%3A%3C%2FSTRONG%3E%3C%2FH4%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EI%20modified%20the%20application%20connecting%20to%20my%20local%20environment%20and%20I%20found%20that%20without%20using%20persistent%20connections%20the%20performance%20in%20terms%20of%20connection%20the%20time%20spent%20is%20the%20same%20using%20persistent%20connections%20in%20Azure.%26nbsp%3B%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EI%20modified%20the%20application%20connecting%20to%20my%20local%20environment%20and%20enabling%20the%20persistent%20connections%2C%20and%20the%20performance%20in%20terms%20of%20connection%2C%20is%20better%20than%20using%20persistent%20connections%20in%20Azure.%26nbsp%3B%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20need%20more%20information%20you%20could%20visit%20this%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FJMNetwalker%2F%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EURL%3C%2FA%3E%20and%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FJMNetwalker%2FPhpMySQLTimeSpent%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Escript%3C%2FA%3E%26nbsp%3Bto%20have%20more%20details%20about%20other%20tests%20done.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEnjoy!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1083187%22%20slang%3D%22en-US%22%3E%3CP%3EToday%2C%20I%20worked%20on%20a%20very%20interesting%20service%20request%20that%20our%20customer%20noticed%20some%20milliseconds%20of%20difference%20connecting%20a%20MySQL%20local%20environment%20and%20Azure%20Database%20for%20MySQL%20using%20PHP.%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FAzure-Database-for-MySQL%2FAzure-Database-for-MySQL-Performance-Troubleshooting-Basics%2Fba-p%2F782815%22%20target%3D%22_self%22%3Ehttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FAzure-Database-for-MySQL%2FAzure-Database-for-MySQL-Performance-Troubleshooting-Basics%2Fba-p%2F782815%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E

Today, I worked on a very interesting service request that our customer noticed some milliseconds of difference connecting a MySQL local environment and Azure Database for MySQL using PHP. 

 

The first point that we need to know that these milliseconds of difference is expected because when you are connecting to Azure Database for MySQL all connections will be made against a proxy server as you could see in this URL

 

For this reason, we have some best practices and recomendations that my colleague Bashar reported

 

In this article just wanted to share the two examples that how to show you measure the results: 

 

In Summary

 

  • I did several tests using mysqlcli and PDO drivers using a sample PHP application. 
  • All test have been done in NorthEurope.
  • I installed PHP version - php-7.3.13-Win32-VC15-x64
  • I created a virtual machine and installed a MySQL server v.8.0.18, with a model that allows me to select Accelerated networking.
    • Computer Size: Standard D4s v3 (4 vcpus, 16 GiB memory) and accelerated networking enabled
  • I created an Azure Database for MySQL:
    • MySQL Version: 5.6, Memory Optimized, 8 vCore(s), 1018 GB
    • SSL Enforce status: DISABLED
    • Initial configuration without any parameter modification.

Results running on Azure

 

Test using PDO:

 

  • I developed this small application, using persistent connection, running 1000 connections opening/closing and SELECT 1 and the results of the connections are more faster.

 

<?php

for ($i = 1; $i <= 1000; $i++)

 {

    $starttime = microtime(true);

    $conn = new pdo("mysql:host=servername.mysql.database.azure.com;dbname=databasename", "username@servername","password", array(

    PDO::ATTR_PERSISTENT => true));

    $tsql= "SELECT 1 as X";

    $getResults= $conn->query($tsql);

    echo ("Reading data from table" . PHP_EOL);

    if ($getResults == FALSE)

        echo (sqlsrv_errors());

    while ($row = $getResults->fetch(PDO::FETCH_ASSOC)) {

      echo ($row['X'] . PHP_EOL);

    }

    $getResults=null;

    $conn=null;

    $endtime = microtime(true);

    $timediff = $endtime - $starttime;

    echo 'Time'.$i.'---'.secondsToTime($timediff).'.--'.($timediff).PHP_EOL;

}

function secondsToTime($s)

{

    $h = floor($s / 3600);

    $s -= $h * 3600;

    $m = floor($s / 60);

    $s -= $m * 60;

    return $h.':'.sprintf('%02d', $m).':'.sprintf('%02d', $s);

}

?>

 

  • I developed this small application, using non-persistent connection, running 1000 connections opening/closing and SELECT 1. You could find a slightly delay in the results.

 

<?php

for ($i = 1; $i <= 1000; $i++)

 {

    $starttime = microtime(true);

    $conn = new pdo("mysql:host=servername.mysql.database.azure.com;dbname=databasename", "username@servername","password", array(

    PDO::ATTR_PERSISTENT => false));

    $tsql= "SELECT 1 as X";

    $getResults= $conn->query($tsql);

    echo ("Reading data from table" . PHP_EOL);

    if ($getResults == FALSE)

        echo (sqlsrv_errors());

    while ($row = $getResults->fetch(PDO::FETCH_ASSOC)) {

      echo ($row['X'] . PHP_EOL);

    }

    $getResults=null;

    $conn=null;

    $endtime = microtime(true);

    $timediff = $endtime - $starttime;

    echo 'Time'.$i.'---'.secondsToTime($timediff).'.--'.($timediff).PHP_EOL;

}

function secondsToTime($s)

{

    $h = floor($s / 3600);

    $s -= $h * 3600;

    $m = floor($s / 60);

    $s -= $m * 60;

    return $h.':'.sprintf('%02d', $m).':'.sprintf('%02d', $s);

}

?>

 

Test using mySQLcli I got the same results using connection pooling/persistent connections:

 

  • I developed this small application, using persistent connection, running 1000 connections opening/closing and SELECT 1

 

<?php

for ($i = 1; $i <= 1000; $i++)

 {

    $starttime = microtime(true);

    //Establishes the connection

    $conn = new mysqli("p:servername.mysql.database.azure.com", "username@servername","password","databasename");

    $tsql= "SELECT 1 as X";

    $getResults= $conn->query($tsql);

    echo ("Reading data from table" . PHP_EOL);

    if ($getResults == FALSE)

        echo (sqlsrv_errors());

    while ($row = $getResults->fetch_assoc()) {

      echo ($row['X'] . PHP_EOL);

    }

    $getResults->free();

    $conn->close();

    $endtime = microtime(true);

    $timediff = $endtime - $starttime;

    echo 'Time'.$i.'---'.secondsToTime($timediff).'.--'.($timediff).PHP_EOL;

}

function secondsToTime($s)

{

    $h = floor($s / 3600);

    $s -= $h * 3600;

    $m = floor($s / 60);

    $s -= $m * 60;

    return $h.':'.sprintf('%02d', $m).':'.sprintf('%02d', $s);

}

?>

 

  • I developed this small application, without using persistent connection, running 1000 connections opening/closing and SELECT 1

 

<?php

for ($i = 1; $i <= 1000; $i++)

 {

    $starttime = microtime(true);

    //Establishes the connection

    $conn = new mysqli("servername.mysql.database.azure.com", "username@servername","password","databasename");

    $tsql= "SELECT 1 as X";

    $getResults= $conn->query($tsql);

    echo ("Reading data from table" . PHP_EOL);

    if ($getResults == FALSE)

        echo (sqlsrv_errors());

    while ($row = $getResults->fetch_assoc()) {

      echo ($row['X'] . PHP_EOL);

    }

    $getResults->free();

    $conn->close();

    $endtime = microtime(true);

    $timediff = $endtime - $starttime;

    echo 'Time'.$i.'---'.secondsToTime($timediff).'.--'.($timediff).PHP_EOL;

}

function secondsToTime($s)

{

    $h = floor($s / 3600);

    $s -= $h * 3600;

    $m = floor($s / 60);

    $s -= $m * 60;

    return $h.':'.sprintf('%02d', $m).':'.sprintf('%02d', $s);

}

?>

 

Results running on Local Environment

 

Test using PDO:

 

  • I modified the application connecting to my local environment and I found that without using persistent connections the performance in terms of connection the time spent is the same using persistent connections in Azure. 

 

<?php

for ($i = 1; $i <= 1000; $i++)

 {

    $starttime = microtime(true);

    $conn = new pdo("mysql:host=localhost;dbname=databasename", "username","password", array(

    PDO::ATTR_PERSISTENT => false));

    $tsql= "SELECT 1 as X";

    $getResults= $conn->query($tsql);

    echo ("Reading data from table" . PHP_EOL);

    if ($getResults == FALSE)

        echo (sqlsrv_errors());

    while ($row = $getResults->fetch(PDO::FETCH_ASSOC)) {

      echo ($row['X'] . PHP_EOL);

    }

    $getResults=null;

    $conn=null;

    $endtime = microtime(true);

    $timediff = $endtime - $starttime;

    echo 'Time'.$i.'---'.secondsToTime($timediff).'.--'.($timediff).PHP_EOL;

}

function secondsToTime($s)

{

    $h = floor($s / 3600);

    $s -= $h * 3600;

    $m = floor($s / 60);

    $s -= $m * 60;

    return $h.':'.sprintf('%02d', $m).':'.sprintf('%02d', $s);

}

?>

 

  • I modified the application connecting to my local environment and enabling the persistent connections, and the performance in terms of connection, is better than using persistent connections in Azure. 

 

<?php

for ($i = 1; $i <= 1000; $i++)

 {

    $starttime = microtime(true);

    $conn = new pdo("mysql:host=localhost;dbname=databasename", "username","password", array(

    PDO::ATTR_PERSISTENT => true));

    $tsql= "SELECT 1 as X";

    $getResults= $conn->query($tsql);

    echo ("Reading data from table" . PHP_EOL);

    if ($getResults == FALSE)

        echo (sqlsrv_errors());

    while ($row = $getResults->fetch(PDO::FETCH_ASSOC)) {

      echo ($row['X'] . PHP_EOL);

    }

    $getResults=null;

    $conn=null;

    $endtime = microtime(true);

    $timediff = $endtime - $starttime;

    echo 'Time'.$i.'---'.secondsToTime($timediff).'.--'.($timediff).PHP_EOL;

}

function secondsToTime($s)

{

    $h = floor($s / 3600);

    $s -= $h * 3600;

    $m = floor($s / 60);

    $s -= $m * 60;

    return $h.':'.sprintf('%02d', $m).':'.sprintf('%02d', $s);

}

?>

 

If you need more information you could visit this URL and script to have more details about other tests done.

 

Enjoy!