doutu3352 2017-04-05 08:48
浏览 39
已采纳

测量json_encode的时间并将json对象和计时器返回给AJAX

I need to measure the time of a json_encode operation and then return the value together with the json object without decoding it. How can I make this happen?

The code I have at the moment is:

$start2 = microtime(true);
$all = json_encode(array(iterator_to_array($cursor),$timeExecuted));
$end2 = microtime(true);
$result = ($end2-$start2);
echo $all;

How can I return $all and $result without decoding the json object?

  • 写回答

2条回答 默认 最新

  • douwei1944 2017-04-05 09:04
    关注

    Your problem is that you're trying to send a JSON response to the client but include in that JSON response the time it took to generate the response. That's kind of impossible, but you then approximate it by including the time it took to generate the response but excluding the time it took to generate the time it took to generate the response (more realistic).

    I think the simplest solution would be:

    $start2 = microtime(true);
    $all = json_encode(array(iterator_to_array($cursor),$timeExecuted));
    $end2 = microtime(true);
    $result = ($end2-$start2);
    echo json_encode(["all" => $all, "result" => $result ]);
    

    However in the client side you now need to do:

    success: function (data) {
         var all = JSON.parse(data.all);
         var time = data.result; 
    }
    

    You could also do some sort of botch fix which may or may not work like:

    ...
    echo "[" . json_encode($result). ", ". $all . "]";
    

    Then you can access result like:

    success: function (data) {
           var time = data[0];
           var all = data[1];
    }
    

    Beware when you use the 2nd way. It's generally bad practice because of odd encoding issues that may come up. Here it should be ok because $result is a number, but in general, it's not the best solution.

    Note: Don't forget to set content headers before echoing anything, this will tell AJAX that it received a JSON response and will parse it accordingly:

     header("Content-Type: text/json");
    

    3rd solution which may or may not work (not tested):

    $start2 = microtime(true);
    $all = json_encode(array(iterator_to_array($cursor),$timeExecuted));
    $end2 = microtime(true);
    $result = ($end2-$start2);
    header("Content-Type: text/json");
    header("X-Encoding-Time: ".$result);
    echo $all;
    

    And in your AJAX:

    success: function (data, textStatus, request) {
          var all = data;
          var result = request.getResponseHeader("X-Encoding-Time");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?