2017-06-01 24 views
0

我有一個Web服務,它需要大約15秒返回front-end.My代碼JSON響應如下所示:爲什麼我的CakePHP Web服務查詢需要大量時間來返回數據?

控制器代碼:

public function getDetailReport($data){ 
$user_id = $data->user_id; 
$test_id = $data->test_id; 
    $result_obj = new TestSetDetailResultTable(); 
    $data_array = $result_obj->getDetailReportByUser($user_id, $test_id); 
    if($data_array['status'] == 1) { 

     echo $this->successMessage('successfully Executed',$data_array['record']); 
     } else { 
     echo $this->failMessage($data_array['error'],$data_array['message']); 
     } 
exit; 
} 

型號代碼:

public function getDetailReportByUser($user_id,$test_id) { 
$data_array = Array(); 
$subject_array = Array(); 
$answer_array = Array(); 
$topper_array = Array(); 
$percentile_array = Array(); 
$max_marks = 0; 
$perc = 0; 
$hrs = 0; 
$mint = 0; 
$sec = 0; 
    $query = new AppQuery(); 
    $query->callProcedure('getSummaryResult',array($test_id,$user_id)); 
    $row_list = $this->loadRowList($query); 

    if(count($row_list) > 0) { 
     $max_marks = $row_list[0]->maximum_marks; 
     $perc = $row_list[0]->percentage; 
     $query->callProcedure('getCompletedTestTime',array($user_id,$test_id)); 
     $row_time = $this->loadRowList($query); 
     $query->callProcedure('getAllUserPerTest',array($test_id)); 
     $row_user = $this->loadRowList($query); 
     if(count($row_time)> 0 && count($row_user) > 0) { 
      foreach ($row_list as $list) { 
       $item['test_name'] = $list->test_name; 
       $item['total_question'] = $list->total_question; 
       $item['right_answer'] = $list->right_answer; 
       $item['wrong_answer'] = $list->wrong_answer; 
       $item['question_attempted'] = $list->question_attempted; 
       $item['question_not_attempted'] = $list->question_not_attempted; 
       $item['positive_marks'] = $list->positive_marks; 
       $item['negative_marks'] = $list->negative_marks; 
       $item['obtaine'] = $list->obtaine; 
        $item['maximum_test_time'] = $list->maximum_test_time; 
        $item['maximum_marks'] = $list->maximum_marks; 
        $item['test_date'] = $list->test_date; 
        $number = floatval($list->obtaine)* 100/intval($list->maximum_marks); 
        $item['percentage'] = number_format($number, 2, '.', ''); // upto 2 decimal places 
        $data_array['detail'] = $item; 
      } 
       $tmp = Array(); 
       $hrs = $row_time[0]->spent_hours; 
       $mint = $row_time[0]->spent_minute; 
       $sec = $row_time[0]->spent_second; 
       $completed_minute = $row_time[0]->compeleted_minute; 
       $completed_second = $row_time[0]->compeleted_second; 
       if($completed_second < 0) { 
        $completed_second = -1 * $completed_second; // only removing - sign 
       } 
       $tmp = $this->calculateTime($hrs, $mint, $sec); 
       $tmp['compeleted_minute'] = $completed_minute; 
       $tmp['compeleted_second'] = $completed_second; 
      $data_array['time'] = $tmp; 
      foreach ($row_user as $list) { 
       $tem['total_user'] = $list->total_user; 
        $data_array['users'] = $tem; 
      } 
      // Now get The subject wise Result 
      $temp = Array(); 
      $query->callProcedure('getTestResult',array($test_id,$user_id)); 
      $subject_result = $this->loadRowList($query); 
      foreach ($subject_result as $res) { 
       $temp['subject_name']= $res->subject_name; 
       $temp['marks_obtained'] = $res->obtaine; 
        $temp['total_question'] = $res->total_question; 
        $temp['question_attempted'] = $res->question_attempted; 
        $temp['wrong_answer'] = $res->wrong_answer; 
        // $temp['total_spent_hours'] = $res->total_spent_hours; 
        // $temp['total_spent_minute'] = $res->total_spent_minute; 
        // $temp['total_spent_second'] = $res->total_spent_second; 
        $time_arr2 = $this->calculateTime($res->total_spent_hours, $res->total_spent_minute, $res->total_spent_second); 
        $temp['total_spent_hours'] = $time_arr2['hours']; 
        $temp['total_spent_minute'] = $time_arr2['minute'];; 
        $temp['total_spent_second'] = $time_arr2['second']; 
        $temp['max_marks'] = intval($res->total_question) * intval($res->positive_marks); 
         $subject_array[] = $temp; 
      } 
      $data_array['subject_result'] = $subject_array; 

      //>>>>>>>>>>> Now get Answer of Question with Time spent>>>>>>>>>> 
      $temp = Array(); 
      $query->callProcedure('getAnswerwithTime',array($test_id,$user_id)); 
      $answer_list = $this->loadRowList($query); 
      foreach ($answer_list as $res) { 
     $temp['question']= utf8_encode($res->question); 
       $temp['user_answer']= $res->user_answer; 
       $temp['correct_answer'] = $res->correct_answer; 
       $temp['spent_hours'] = $res->spent_hours; 
       $temp['spent_minute'] = $res->spent_minute; 
       $temp['spent_second'] = $res->spent_second; 
        $temp['obtaine'] = $res->obtaine; 
       $answer_array[] = $temp;  
      } 
      $data_array['answer_with_time'] = $answer_array; 

      /*>>>>>>>>End>>>>>>>>>>>>>*/ 
      /*>>>>>>>>>>>>>>For Topper result for Comparing>>>>>>>>>>>>>>>>*/ 
      $temp = Array(); 
      $query->callProcedure('getTopperResult',array($test_id)); 
      $top_arr = $this->loadRowList($query); 
      foreach ($top_arr as $top) { 
       $temp['user_name'] = $top->user_name; 
       $temp['test_name'] = $top->test_name; 
       $temp['total_question'] = $top->total_question; 
       $temp['right_answer'] = $top->right_answer; 
       $temp['wrong_answer'] = $top->wrong_answer; 
       $temp['question_attempted'] = $top->question_attempted; 
       $temp['question_not_attempted'] = $top->question_not_attempted; 
       $temp['positive_marks'] = $top->positive_marks; 
       $temp['negative_marks'] = $top->negative_marks; 
       $temp['maximum_marks'] = $top->maximum_marks; 
       $temp['obtaine'] = $top->obtaine; 
       $temp['percentage'] = $top->percentage; 
       $temp['maximum_test_time'] = $top->maximum_test_time; 
       $temp['test_date'] = $top->test_date; 
       $timer = $this->calculateTime($top->spent_hours, $top->spent_minute, $top->spent_second); 
       $temp['spent_hours'] = $timer['hours']; 
       $temp['spent_minute'] = $timer['minute']; 
       $temp['spent_second'] = $timer['second']; 
       $temp['completed_minute'] = $top->completed_minute; 
       $sec_var = $top->completed_second; 
       if($sec_var < 0) { 
        $sec_var = -1 * $sec_var; 
       } 
       $temp['completed_second'] = $sec_var; 
       $percentile = $this->getPercentileRank($test_id,$top->percentage,$top->maximum_marks); 
       $temp['percentile'] = $percentile; // percentile 
      // $temp['rank'] = intval($percentile); // Rank 
       $topper_array[] = $temp; 
      } 
      //>>>>>>>>>>>>>>>>>> topper array contain Topper Percentile,now we need to get Rank according to Percentile 
      $topper_array = $this->rank($topper_array); 
      $data_array['toppers_result'] = $topper_array; 
       /*>>>>>>>>>>>>>>For Topper Result>>>>>>>>>>>>>>>>*/ 

      /*>>>>>>>>>>>>>>For Login user Percentile>>>>>>>>>>>>>>>>*/ 
      $percentile = $this->getPercentileRank($test_id, $perc, $max_marks); 
      $percentile_array = $this->loginUserRank($topper_array, $percentile); 
      $data_array['percentile'] = $percentile_array; 
       /*>>>>>>>>>>>>>>For Login user Percentile End>>>>>>>>>>>>>>>>*/ 
      /*>>>>>>>>>Get subject Wise Time of Toppers >>>>>>>>>>>>>*/ 
      $subject_wise_time = $this->getSubjectWiseTopperTime($test_id); 
      $data_array['subject_wise_topper_time'] = $subject_wise_time; 
      /*>>>>>>>>>Get subject Wise Time of Toppers End >>>>>>>>>>>>>*/ 

      /*>>>>>>>>>Get Answer with Time of Toppers >>>>>>>>>>>>>*/ 
      $topper_answer_with_time = $this->topperAnswerTime($test_id); 
      $data_array['topper_answer_with_time'] = $topper_answer_with_time; 
      /*>>>>>>>>>Get Answer with Time of Toppers Ends >>>>>>>>>>>>>*/ 

      return $this->modelMessage(1,'non','success',$data_array); exit; 
     } else { 
      return $this->modelMessage($this->getStatus(),$this->getError(),$this->getMessage()); exit; 

     } 

    } else { 
     return $this->modelMessage($this->getStatus(),$this->getError(),$this->getMessage()); exit; 

    } 
} 

我想調試這段代碼,但是我不知道我在這裏丟失了什麼。如何用15秒的時間返回響應?我做錯了什麼?

回答

0

當您調試比microtime中(真)計算所需的時間,我的猜測是DB querys例如:

$start=microtime(true); 
$answer_list = $this->loadRowList($query); 
$stop=microtime(true); 
$neededTime=$stop-$start; 
echo "Time for answer_list $neededTime s for query $query"; 

比你看到什麼需要的時間最長。看看你的查詢看看你的數據庫模式。在大多數情況下,您可以通過在數據庫表中添加一些索引來解決該問題。你可以用sql級別的解釋來「調試」查詢,這會告訴你如果你使用索引。

相關問題