123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- <?php
- use PHPUnit\Framework\TestCase;
- define('APP_ID', 'OrderErr');
- define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
- require_once(BASE_ROOT_PATH . '/global.php');
- require_once(BASE_CORE_PATH . '/lrlz.php');
- require_once(BASE_ROOT_PATH . '/fooder.php');
- class TestOrderErr extends TestCase
- {
- public static function setUpBeforeClass(): void
- {
- Base::run_util();
- }
- //docker-compose run -d phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestOrderErr::testRecoverPdlog)( .*)?$/" --test-suffix TestOrderErr.php /var/www/html/test
- public function testRecoverPdlog()
- {
- $names = ['4', '45', '55', '81', '116'];
- foreach ($names as $name) {
- $filename = BASE_DATA_PATH . "/log/recover/{$name}.log";
- $this->recover($filename);
- }
- }
- private function recover($filename)
- {
- $reader = function ($fp) {
- while (!feof($fp)) {
- $logs = [];
- for ($index = 0; $index < 100 && !feof($fp); $index++) {
- $log = fgets($fp);
- $logs[] = $log;
- }
- yield $logs;
- }
- };
- $filter = function ($lines) {
- $logs = [];
- foreach ($lines as $line) {
- $ret = preg_match('/lrlz_pd_log[^(]+[(]{1}(?P<key>[^)]+)[)]{1}[\s]+VALUES[^(][(]{1}(?P<value>[^)]+)[)]{1}/u', $line, $matches);
- if ($ret) {
- $skey = $matches['key'];
- $svalue = $matches['value'];
- $keys = explode(',', $skey);
- $values = explode(',', $svalue);
- $conveter = function ($value) {
- $val = trim($value, '\'');
- return $val;
- };
- $values = array_map($conveter, $values);
- $log = array_combine($keys, $values);
- $logs[] = $log;
- }
- }
- return $logs;
- };
- $mod_log = Model();
- $inserter = function ($logs) use ($mod_log) {
- // $mod_log->table('acclog')->insertAll($logs);
- foreach ($logs as $log) {
- $ret = $mod_log->table('acclog')->insert($log);
- if (!$ret) {
- Log::record("import error", Log::ERR);
- }
- }
- };
- $file = fopen($filename, 'r');
- $batches = $reader($file);
- foreach ($batches as $batch) {
- $logs = $filter($batch);
- $inserter($logs);
- }
- fclose($file);
- }
- public function testSQLMatch()
- {
- // $line =<<< line
- // [cordispatcher 38-123975 2021-12-28 15:22:10 0.261198] ERR: DbError eno=1062 msg=Duplicate entry '2147483647' for key 'lrlz_pd_log.PRIMARY' sql=INSERT INTO `lrlz_pd_log` (lg_member_id,lg_member_name,lg_add_time,lg_type,lg_order_sn,lg_av_amount,lg_desc,lg_available) VALUES ('66025','5d322c4ee98781023b16d9a3c21cbfb4',1640676130,'order_pay','6861760694020130235975',-48,'下单,支付预存款,订单号: 6861760694020130235975','954256.6700')
- // line;
- $line = <<< line
- [cordispatcher 29-126259 2021-12-28 15:22:10 0.836849] ERR: DbError eno=1062 msg=Duplicate entry '2147483647' for key 'lrlz_pd_log.PRIMARY' sql=INSERT INTO `lrlz_pd_log` (lg_member_id,lg_member_name,lg_add_time,lg_type,lg_order_sn,lg_av_amount,lg_freeze_amount,lg_desc,lg_available) VALUES ('66240','c27517c6553a6f783b8df974c3d2d219',1640676130,'order_cancel','9031520694020106890799',48,-48,'取消订单,解冻预存款,订单
- 号: 9031520694020106890799','42192.9500')
- line;
- $ret = preg_match('/lrlz_pd_log[^(]+[(]{1}(?P<key>[^)]+)[)]{1}[\s]+VALUES[^(][(]{1}(?P<value>[^)]+)[)]{1}/u', $line, $matches);
- if ($ret) {
- $skey = $matches['key'];
- $svalue = $matches['value'];
- $keys = explode(',', $skey);
- $values = explode(',', $svalue);
- $conveter = function ($value) {
- $val = trim($value, '\'');
- return $val;
- };
- $values = array_map($conveter, $values);
- $log = array_combine($keys, $values);
- }
- }
- public function testGetLatestSending()
- {
- $mod = Model();
- $item = $mod->table('refill_detail')->field('min(order_time) as mintime')->where(['order_time' => ['egt', time() - 5 * 86400], 'order_state' => 30])->find();
- }
- public function testLoadAcc()
- {
- $line = <<< line
- [crontab 1 2021-12-28 00:12:28] SQL: UPDATE `lrlz_member` SET available_predeposit='74496.9500',freeze_predeposit='0.0000' WHERE ( member_id = 66240 ) [ RunTime:0.000156s ]
- line;
- // $ret = preg_match('/lrlz_pd_log[^(]+[(]{1}(?P<key>[^)]+)[)]{1}[\s]+VALUES[^(][(]{1}(?P<value>[^)]+)[)]{1}/u', $line, $matches);
- $ret = preg_match('/2021-12-28[\s]+(?P<time>[\d:^\]]+)]{1}[\s\S]+available_predeposit=[\']{1}(?P<amount>[\d\.]+)[\']{1}[\s\S]+member_id[\s=]+(?P<member>[\d]+)/u', $line, $matches);
- if ($ret) {
- $time = $matches['time'];
- $amount = $matches['amount'];
- $member = $matches['member'];
- $add_time = "2021-12-28 {$time}";
- $add_time = strtotime($add_time);
- $log = ['member_id' => $member, 'available_predeposit' => $amount, 'add_time' => $add_time];
- } else {
- }
- }
- //docker-compose run -d phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestOrderErr::testRecoverAcclog)( .*)?$/" --test-suffix TestOrderErr.php /var/www/html/test
- public function testRecoverAcclog()
- {
- $filename = BASE_DATA_PATH . "/log/recover/update.log";
- $this->recover_acclog($filename);
- }
- private function recover_acclog($filename)
- {
- $reader = function ($fp) {
- while (!feof($fp)) {
- $logs = [];
- for ($index = 0; $index < 100 && !feof($fp); $index++) {
- $log = fgets($fp);
- $logs[] = $log;
- }
- yield $logs;
- }
- };
- $filter = function ($lines) {
- $logs = [];
- foreach ($lines as $line) {
- $ret = preg_match('/2021-12-28[\s]+(?P<time>[\d:^\]]+)]{1}[\s\S]+available_predeposit=[\']{1}(?P<amount>[-\d\.]+)[\']{1}[\s\S]+member_id[\s=]+(?P<member>[\d]+)/u', $line, $matches);
- if ($ret) {
- $time = $matches['time'];
- $amount = $matches['amount'];
- $member = $matches['member'];
- $add_time = "2021-12-28 {$time}";
- $add_time = strtotime($add_time);
- $log = ['member_id' => $member, 'available_predeposit' => $amount, 'add_time' => $add_time];
- $logs[] = $log;
- } else {
- Log::record("{$line}", Log::ERR);
- }
- }
- return $logs;
- };
- $mod_log = Model();
- $inserter = function ($logs) use ($mod_log) {
- foreach ($logs as $log) {
- $ret = $mod_log->table('macclog')->insert($log);
- if (!$ret) {
- Log::record("import error", Log::ERR);
- }
- }
- };
- $file = fopen($filename, 'r');
- $batches = $reader($file);
- foreach ($batches as $batch) {
- $logs = $filter($batch);
- $inserter($logs);
- }
- fclose($file);
- }
- //docker-compose run -d phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestOrderErr::testFilterErrorLog)( .*)?$/" --test-suffix TestOrderErr.php /var/www/html/test
- public function testFilterErrorLog()
- {
- $start_time = '2021-12-28 15:22:00';
- $end_time = '2021-12-28 16:00:00';
- $files = ['45' => 'xaj'];
- $prefix = BASE_DATA_PATH . "/log/pdlog";
- foreach ($files as $path => $name) {
- $input = "{$prefix}/{$path}/{$name}";
- $outpath = "{$prefix}/{$path}";
- $this->filter_time($input, $outpath, $start_time, $end_time);
- }
- }
- private function filter_time($input_file, $outpath, $start_time, $end_time)
- {
- $start = strtotime($start_time);
- $end = strtotime($end_time);
- $reader = function ($fp) use ($start, $end)
- {
- while (!feof($fp))
- {
- $line = fgets($fp);
- $ret = preg_match('/\[cordispatcher[\s]+(?P<pid>[\d]+)-[\d]+[\s]+(?P<time>[\d-]+[\s]{1}[\d:]+)[\s]{1}/u', $line, $matches);
- if ($ret)
- {
- $time = strtotime($matches['time']);
- if ($time < $start) {
- continue;
- } elseif ($time > $end) {
- break;
- } else {
- yield $line;
- }
- }
- }
- };
- $filter_pid = function ($line)
- {
- $ret = preg_match('/\[cordispatcher[\s]+(?P<pid>[\d]+)-[\d]+[\s]+(?P<time>[\d-]+[\s]{1}[\d:]+)[\s]{1}/u', $line, $matches);
- if ($ret) {
- $pid = intval($matches['pid']);
- return $pid;
- }
- return 0;
- };
- $beginer = function ($line)
- {
- $ret = preg_match('/[\s]+BeginGoFunction[\s]+/u', $line, $matches);
- if ($ret) {
- return true;
- } else {
- return false;
- }
- };
- $pid_fp = [];
- $spliter = function ($pid, $line) use ($outpath, &$pid_fp,$beginer)
- {
- $begin = $beginer($line);
- if (array_key_exists($pid, $pid_fp))
- {
- $fp = $pid_fp[$pid];
- if($begin) {
- fwrite($fp, "\r\n");
- }
- }
- else {
- $name = "{$outpath}/{$pid}.log";
- $fp = fopen($name, 'w');
- $pid_fp[$pid] = $fp;
- }
- fwrite($fp, $line);
- };
- $input = fopen($input_file, 'r');
- $lines = $reader($input);
- foreach ($lines as $line)
- {
- $pid = $filter_pid($line);
- if ($pid > 0) {
- $spliter($pid, $line);
- }
- }
- fclose($input);
- foreach ($pid_fp as $pid => $fp) {
- fclose($fp);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public function testFindErrAcc()
- {
- $files = ['45' => ['7.log']];
- $prefix = BASE_DATA_PATH . "/log/pdlog";
- foreach ($files as $path => $names)
- {
- foreach ($names as $name) {
- $input = "{$prefix}/{$path}/{$name}";
- $this->filter_error($input);
- }
- }
- }
- private function filter_error($file)
- {
- $task_reader = function ($fp)
- {
- $task = [];
- $time = '';
- $pid = 0;
- $cid = 0;
- while (!feof($fp))
- {
- $line = fgets($fp);
- $ret = preg_match('/\[cordispatcher[\s]+(?P<pid>[\d]+)-(?P<cid>[\d]+)[\s]+(?P<time>[\d-]+[\s]{1}[\d:]+)[\s]{1}[\s\S]+BeginGoFunction[\s]+/u', $line, $matches);
- if ($ret)
- {
- if(!empty($task)) {
- yield [$time,$pid,$cid,$task];
- $task = [];
- }
- $time = $matches['time'];
- $pid = intval($matches['pid']);
- $cid = intval($matches['cid']);
- $task[] = $line;
- }
- else {
- $task[] = $line;
- }
- }
- if(!empty($task)) {
- yield [$time,$pid,$cid,$task];
- }
- };
- $methoder = function ($task)
- {
- foreach ($task as $line)
- {
- $ret = preg_match('/[\s]+method[=]{1}(?P<method>[\S]+)/u', $line, $matches);
- if ($ret) {
- $method = $matches['method'];
- return $method;
- }
- }
- return false;
- };
- $trans_filter = function ($task)
- {
- $tran = [];
- foreach ($task as $line)
- {
- $ret = preg_match('/[\s]+transaction[\s]+(?P<type>[\S]+)/u', $line, $matches);
- if ($ret)
- {
- $type = $matches['type'];
- if($type == 'begin') {
- $tran = [];
- $tran[] = $line;
- }
- elseif($type == 'commit') {
- $tran[] = $line;
- yield [$type,$tran];
- $tran = [];
- }
- elseif($type == 'rollback') {
- $tran[] = $line;
- yield [$type,$tran];
- $tran = [];
- }
- else {
- $tran[] = $line;
- yield [$type,$tran];
- $tran = [];
- }
- }
- else {
- $tran[] = $line;
- }
- }
- };
- $input = fopen($file, 'r');
- $tasks = $task_reader($input);
- foreach ($tasks as $time_task)
- {
- [$time,$pid,$cid,$task] = $time_task;
- $method = $methoder($task);
- Log::record("{$pid}-{$cid} method={$method} time={$time}",Log::DEBUG);
- $trans = $trans_filter($task);
- foreach ($trans as $type_tran) {
- [$type,$tran] = $type_tran;
- Log::record("\ttrans type={$type}",Log::DEBUG);
- }
- Log::record("\r\n",Log::DEBUG);
- }
- fclose($input);
- }
- public function testBegin()
- {
- $beginer = function ($line)
- {
- $ret = preg_match('/[\s]+BeginGoFunction[\s]+/u', $line, $matches);
- if ($ret) {
- return true;
- } else {
- return false;
- }
- };
- $line = '[cordispatcher 7-114568 2021-12-28 15:22:02 0.861740] DEBUG: BeginGoFunction coroutin_num=2 memory=24006608';
- $begin = $beginer($line);
- }
- }
|