T
T
tatarrr952020-06-09 21:16:59
PHP
tatarrr95, 2020-06-09 21:16:59

How to calculate the execution time of each operation in a script?

There is such a script where an array of people arrives in JSON, each of which has several numbers and certain parameters. For each person and number u must form a uuid and add it to the mysql table. The script runs for a very long time on big data. For example, the first 15,000 numbers fly into the table in a minute, and 75,000 fly in 20 minutes. Why does it take so long and the execution time exponentially increases with volume? Is it possible to calculate the execution time of each operation? How to do it?

$new_response_client = array();
            $queueBank = array();        

            $clients = $get_data['data'];
            foreach($clients as $client){
                array_push($queueBank, $client);
            }

            foreach($queueBank as $client){

                $flag = flagHandler($get_data['system'], $client['sex']);
                $io = $client['io'];
                $fio = $client['fio'];
                $row_id = $client['row_id'];
                $cnt_days = dpdHandler($client['dpd']);
                $sum = sumHandler($client['sum']);
                $reg_num = $client['reg_num'];
                $originate_params = "origination_caller_id_number=111";
                $smart_dialogs_uid = gen_uuid();

                $new_response_phones = array();

                foreach($client['phones'] as $phone){
                    $phone = phoneHandler($phone);
                    $call_uid = gen_uuid();
                    if(strlen($phone) == 10){
                        $STH = $DBH->prepare("INSERT INTO Queue (Phone, Flag, Io, Fio, Cnt_days, Sum, Reg_num, Originate_params, Client_id, Smart_dialogs_uid, Call_uid) values (:Phone, :Flag, :Io, :Fio, :Cnt_days, :Sum, :Reg_num, :Originate_params, :Client_id, :Smart_dialogs_uid, :Call_uid)");
                        $STH->bindParam(':Phone', $phone);
                        $STH->bindParam(':Flag', $flag);
                        $STH->bindParam(':Io', $io);
                        $STH->bindParam(':Fio', $fio);
                        $STH->bindParam(':Cnt_days', $cnt_days);
                        $STH->bindParam(':Sum', $sum);
                        $STH->bindParam(':Reg_num', $reg_num);
                        $STH->bindParam(':Originate_params', $originate_params);
                        $STH->bindParam(':Client_id', $client['client_id']);
                        $STH->bindParam(':Smart_dialogs_uid', $smart_dialogs_uid);
                        $STH->bindParam(':Call_uid', $call_uid);
                        $STH->execute();
                        $api_status = "Success";
                    }
                    else{
                        $api_status = "Error. Bad count in number.";
                    }
                    array_push($new_response_phones, $phone);
                }
                $new_response_client["row_id"] = $row_id;
                $new_response_client["client_id"] = $client['client_id'];
                $new_response_client["reg_num"] = $reg_num;
                $new_response_client["smart_dialogs_uid"] = $smart_dialogs_uid;
                $new_response_client["phone"] = $new_response_phones;

                $flag = null;
                $io = null;
                $fio = null;
                $cnt_days = null;
                $sum = null;
                $reg_num = null;
                $originate_params = null;
                $smart_dialogs_uid = null;
                array_push($new_response, $new_response_client);
            }
            print_r(json_encode($new_response));

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vilinyh, 2020-06-09
@tatarrr95

You can use microtime for this:

$_t = microtime(true);
    echo "Doing ... ";
    doSomething();
    echo microtime(true) - $_t . "s\n";

Solution with an asterisk - XDebug profiling - https://xdebug.org/docs/profiler
But this way the data is not loaded into the table.
If it's MySQL, use LOAD DATA INFILE or extended inserts. You can wrap the insert in a transaction, just keep the contention in mind.
It can also slow down due to triggers, constraints and indexes on the table. Disable them beforehand or load them through a temporary table.

M
mayton2019, 2020-06-09
@mayton2019

The author described a typical ETL process. (Extract-Transform-Load). Such processes
1) Do not write in PHP
2) Use special import utilities (the names are different and depend on the DBMS)
3) The DBMS itself and the table are prepared for this.
- triggers and constraints
are disabled - indexes are dropped.
- Transaction modes that do not load I/O as much as possible are used. That is, there should not be any mother evo auto-commit.
At the end, triggers and indexes return to their original status.
The speed of such an ETL is 80% dependent on these points that I described.
And once again I will repeat. PHP doesn't do this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question