function run_tuning_pub($seq, $pid, $max_cnt, $one_msg) { // 初始化log配置 $log_conf = new BigpipeLogConf(); $log_conf->file = sprintf('publisher-%u-%u.log', $seq, $pid); if (BigpipeLog::init($log_conf)) { //echo '[Success] [open meta agent log]\n'; //print_r($log_conf); //echo '\n'; } else { echo "[Failure] [open meta agent log]\n"; print_r($log_conf); echo "\n"; return; } // subscriber的configure $conf = new BigpipeConf(); $conf_dir = './conf'; $conf_file = './for-tuning.conf'; if (false === bigpipe_load_file($conf_dir, $conf_file, $conf)) { echo "[failure][when load configure]\n"; return; } // 发布参数 $pipe_name = 'pipe2'; //$pipelet_id = 1; // 加1在哪里做好呢? $token = 'token'; $partitioner = new TestPubPartitioner($pid); // 生成一个1K左右的包 $max_msg_count = 12; // 500k $msg = null; $the_one = sprintf("[case:%u]\n", $seq); $uid = BigpipeUtilities::get_uid(); $msg_package = new BigpipeMessagePackage(); $msg_count = 0; while ($msg_count++ < $max_msg_count) { $msg = sprintf("[php-api-test][bigpipe_comlog_pvt_mm][uid:%s][package:%u][seq:%u]\n", $uid, 0, $msg_count); if (true === $one_msg) { $the_one .= $msg; } else { if (!$msg_package->push($msg)) { echo "[fail to pack message]{$msg}\n"; break; // 退出 } } } // end of add message to package if (true == $one_msg) { if (!$msg_package->push($the_one)) { echo "[fail to pack message]{$the_one}\n"; return; // 退出 } } // 定义测试中发包条数 $max_package_count = $max_cnt; echo "\n\n[Publish {$max_package_count} packages]\n\n"; $pub_file_name = sprintf('./pub-timer-%u-%u.csv', $seq, $pid); $pub_file = fopen($pub_file_name, 'w+'); $pub = new BigpipePublisher(); $stat = array(0 => 0, 1 => 0, 2 => 0); if ($pub->init($pipe_name, $token, $partitioner, $conf)) { echo "[Success][init publisher]\n\n"; $count = 0; $succeed = 0; $total_start = BigpipeUtilities::get_time_us(); $is_first = true; printf("[case:%u][start:%u]\n\n", $seq, $total_start); while ($count < $max_package_count) { $count++; //$max_msg_count = rand(1, $max_package_message_count); //echo "[Pack $max_msg_count messages to package <$count>]\n"; // if ($msg_count != $max_msg_count + 1) // { // echo "[expected:$max_msg_count][actual:$msg_count]\n"; // continue; // 打包失败,退出 // } $start_time = BigpipeUtilities::get_time_us(); $msg_package->push(sprintf('timestamp:%u', $start_time)); // 加入时间戳 $pub_result = $pub->send($msg_package); $end_time = BigpipeUtilities::get_time_us(); if (false === $pub_result) { //echo "[fail to publish message package][count:$count]\n"; //break; // 出错便停止 } else { // write result to file //$ret_str = sprintf("%d,%u,%u,%u\n", // $pub_result->error_no, // $pub_result->pipelet_id, // $pub_result->pipelet_msg_id, // $pub_result->session_msg_id); //fwrite($pub_file, $ret_str); if (true == $is_first) { printf('[%u][%u]\\n', $pub_result->pipelet_id, $pub_result->pipelet_msg_id); $is_first = false; } $succeed++; } $msg_package->pop(); // 弹出时间戳 $t = (double) ($end_time - $start_time) / 1000; $t_str = sprintf("%u\n", $t); fwrite($pub_file, $t_str); } $total_end = BigpipeUtilities::get_time_us(); echo "[Publisher][seq:{$seq}][count:{$count}][success:{$succeed}]====\n"; $avg = (double) ($total_end - $total_start) / (double) (1000 * $max_package_count); printf("\n[Publisher][case:%u][avg_time:%f(ms)]\n", $seq, $avg); } else { echo '[Failure][init publisher]\\n'; } $pub->uninit(); BigpipeLog::close(); }
echo "[fail to pack message]{$msg_count}\n"; break; // 跳出loop } } // end of add message to package if ($msg_count != $max_msg_count + 1) { echo "[expected:{$max_msg_count}][actual:{$msg_count}]\n"; continue; // 打包失败,退出 } // 当消息添加完后,使用publisher的send接口,发布数据 // send成功,返回的pub_result中包含了pipelet_id和pipelet_msg_id // pipelet_msg_id唯一标识了本个数据包在pipelet中的位置 // send失败,用户可以选择继续send,这时send接口内部会更新状态,尝试重新发布 $pub_result = $pub->send($msg_package); if (false === $pub_result) { echo "[fail to publish message package][count:{$count}]\n"; break; // 出错便停止 } else { // write result to file $session = TestUtilities::get_private_var($pub, '_session'); $ret_str = sprintf("%d,%u,%u,%u,%s\n", $pub_result->error_no, $pub_result->pipelet_id, $pub_result->pipelet_msg_id, $pub_result->session_msg_id, $session); fwrite($pub_file, $ret_str); $succeed++; } echo "[Publisher][count:{$count}][success:{$succeed}]====\n"; // 当发布结束后,使用uninit强制清空发布状态 $pub->uninit(); } else {
public function testSend() { $pkg = new BigpipeMessagePackage(); $subject = new BigpipePublisher(); $num_piplet = 10; // ²âÊÔ1 ʹÓÃδ³õʼ»¯µÄ·¢²¼Àà $this->assertFalse($subject->send($pkg)); // Ç¿ÖÆÉ趨Òѳõʼ»¯ $this->assertTrue(TestUtilities::set_private_var($subject, '_inited', true)); // ²âÊÔ2 get_pipelet_id´íÎó $stub_paritioner = $this->getMockBuilder('BigpipePubPartitioner')->disableOriginalConstructor()->getMock(); // configure the stubs $test_piplet = 2; $stub_paritioner->expects($this->any())->method('get_pipelet_id')->will($this->onConsecutiveCalls($num_piplet + 1, $test_piplet, $test_piplet, $test_piplet)); $this->assertTrue(TestUtilities::set_private_var($subject, '_partitioner', $stub_paritioner)); $this->assertTrue(TestUtilities::set_private_var($subject, '_num_piplet', $num_piplet)); $this->assertFalse($subject->send($pkg)); // ²âÊÔ3 pub task startʧ°Ü // create a stub array of BigpipePublishTask $stub_tasks = array(); $stub_tasks[$test_piplet] = $this->getMockBuilder('BigpipePublishTask')->disableOriginalConstructor()->getMock(); $stub_tasks[$test_piplet]->expects($this->any())->method('start')->will($this->onConsecutiveCalls(false, true, true)); $pub_ret = new BigpipePubResult(); $stub_tasks[$test_piplet]->expects($this->any())->method('send')->will($this->onConsecutiveCalls(false, $pub_ret)); $stub_tasks[$test_piplet]->expects($this->any())->method('stop')->will($this->returnValue(null)); $this->assertTrue(TestUtilities::set_private_var($subject, '_pub_list', $stub_tasks)); $this->assertFalse($subject->send($pkg)); // ²âÊÔ4 ·¢²¼Ê§°Ü $this->assertFalse($subject->send($pkg)); // ²âÊÔ5 ³É¹¦·¢²¼ print_r($pub_ret); $this->assertEquals($pub_ret, $subject->send($pkg)); }