PHP multi-process simulation multi-client concurrent access to remote MySQL database for network stress testing

Source: Internet
Author: User
Tags mysql host

Because a server (server a) is powered off by an IDC without reason and the root directory of the file cannot be written, you need to switch the data and services running on this server to another server (server B) in order to stop the business on a for repair. Before data migration, You need to perform a stress test on the database on B to see if the existing and additional database throughput can be taken into account simultaneously.

I have read the article "php multi-process concurrency control test cases" by Dr. Zhang for a lot of help. Here I will record it.

The general idea is: specify the maximum number of processes $ limit to prevent the local computer from exploding tables due to the large number of processes pushed to the background; set a variable to record the remaining idle sub-processes $ p_number (number of idle sub-processes = maximum number of processes-number of processes occupied by the current script, the number of idle sub-processes at the beginning is 0, or you can set the initial value to equal to the maximum number of processes $ limit ). When the remaining number of sub-processes is sufficient ($ p_number> 0), open a sub-process to dosql. PHP script. In this script, connect to MySQL remotely and perform crud operations and put the operation records to Ptest. in the log file, the number of idle sub-processes is reduced by one; otherwise ($ p_number
<= 0) Let the script wait for one second to read the number of existing processes in the current system $ line, the remaining number of idle sub-processes is $ p_number = $ limit-$ line (so it doesn't matter if $ p_number = 0 at the beginning. $ line = 0 at the beginning, $ p_number = $ limit). If $ p_number> 0, you can start a new sub-process. Otherwise, wait a second.

Code:


Ptest. php:


<? PHP/** objective: to use popen () to open multiple processes in the background and connect to a remote MySQL database to simulate concurrent Queries/operations on multiple clients for stress throughput testing * @ $ p_number: number of idle processes * @ $ limit: Maximum number of processes * // ** function: Under the number of sub-processes <$ limit, enable multiple sub-processes to execute dosql. PHP; when the idle sub-process is not enough ($ p_number <= 0), wait 1 second to continue executing */function run () {Global $ p_number; if ($ p_number <= 0) {$ p_number = work_process ($ p_number);} $ p_number = $ p_number-1; $ out = popen ("/usr/bin/PHP/opt/dosql. PHP & "," R "); pclose ($ out);}/** function: set the maximum number of processes $ limit to view the currently opened sub-process script (dosql . PHP) number of processes occupied. If idle sub-processes are not enough, wait 1 second. Otherwise, the remaining number of idle sub-processes is returned to run () */function work_process ($ p_number) {$ Limit = 3000; while ($ p_number <= 0) {// view dosql in the background. PHP script processes $ cmd = popen ("PS-Ef | grep \"/opt/dosql. PHP \ "| grep-V grep | WC-L", "R"); $ line = fread ($ cmd, 512); pclose ($ cmd ); $ p_number = $ limit-$ line; if ($ p_number <= 0) {sleep (1) ;}} return $ p_number ;} /** start $ count MySQL operation concurrency to $ limit sub-process execution */$ COUNT = 5000; For ($ I = 0; $ I < $ Count; $ I ++) {run (); echo "Idle process number:". $ p_number. "\ n" ;}?>


Dosql. php:


<? PHP/** note: the script executed by the sub-process connects to the remote MySQL database and performs a series of addition, deletion, query, and modification operations to record the required time in the log file. the MySQL statement of each sub-process is no query cache. */$ stime = Time (); $ dbhost = "XX. XX. XX. xx "; $ dbuser =" root "; $ dbpwd =" XXXX "; $ dbname =" test "; $ dbcharset =" GBK "; $ conn = mysql_connect ($ dbhost, $ dbuser, $ dbpwd) or die ("can not connect MySQL host ". $ dbhost); if ($ dbcharset) {mysql_query ("set names ". $ dbcharset);} if ($ dbname) {mysql_select_db ($ dbname, $ conn) or die ("Can not select DB". $ dbname);}/** here we can perform some complex crud operations. The example is very simple. $ Table1 = "pre_common_member"; $ SQL = "select SQL _no_cache * from '$ Table1' Where uid = 78"; $ result = mysql_query ($ SQL, $ conn ); $ DATA = @ mysql_fetch_array ($ result); if ($ data) dolog ("success", $ stime); elsedolog ("fail", $ stime ); mysql_close ($ conn); * // ** function: records logs, including the start time, end time, and time (SEC) of each SQL statement ), and whether the query is successful */function dolog ($ input, $ stime) {$ etime = Time (); $ duration = $ etime-$ stime; file_put_contents ("/opt/Ptest. l OG "," Start Time :". date ("Y-m-d h: I: s", $ stime ). "End Time :". date ("Y-m-d h: I: s", $ etime ). "". $ input. "Duration :". $ duration. php_eol, file_append | lock_ex) ;}?>

Run the script:

/Usr/bin/PHP/opt/Ptest. php


How to Use the PHP program as a Linux daemon:

Nohup/usr/bin/PHP/opt/Ptest. php 2> & 1>/dev/null &

(The nohup command can still execute the program after the user exits the terminal. "2> & 1>/dev/null" indicates that the standard output and error output are not displayed, the last & indicates pushing to the background for execution .)


PS. in dosql. PHP performs some complex query operations to test the pressure limit of MySQL. Of course, Ptest. the maximum number of processes in PHP is also related to the number of concurrent accesses to the website, which can only be simulated as much as possible. For dolog (), you can also store the records in the local database, so that you can analyze and view the statement execution in more detail, for example, which statements have an average execution time greater than 2 seconds.

I think of another problem. If the number of concurrent processes is too large, I don't know how to test the network latency.

PS. MySQL 5.1.x comes with a mysqlslap. You can use a few commands to easily simulate concurrent MySQL access operations. However, it seems that it is only a local test and cannot be used for remote testing?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.