加速生成大型JSON数组的非常慢的PHP脚本

时间:2016-09-27 19:12:12

标签: php mysql arrays json google-app-engine

我目前在AppEngine上运行一个Web应用程序,它使用PHP初始生成一个巨大的JSON数组,然后Javascript用它来填充我的网络应用程序的元素。

以下代码完美无缺并完成我想要的一切,但需要大约10秒钟才能执行。可能听起来不是很多,但它确实感觉不合适。我想知道我应该做些什么来加快初始数组的创建。

到目前为止,我曾想过以某种方式攻击这个问题;

  • 我最初可以加载较少数据,减少加载时间 字面意思是加载量少。这会影响一般性能 但是,正如现在加载数据 后,每个"部分"是 即时填充和超快速。
  • 我可以使用App Engine缓存一些数据 内置Memcache。这是一个很好的想法,但产生的数据 经常变化。通常,缓存就不会这样做。
  • 使用网络 而是套接字。我想最终做到这一点,并且它是关于待办事项的 列表,但这意味着重写了很多代码,所以我也不是 现在喜欢这个。

我确定有一些"不良做法"我的代码中的点点滴滴,随意指出并骂我...... Here's the same code below as a Gist if you prefer it that way.

<?php
use google\appengine\api\cloud_storage\CloudStorageTools;


# ===================================== #
# ==== Generate App Settings Array ==== #
# ===================================== #
//Existing Class (not shown) already generated $app for us...
$app_settings_array = array('app_version' => $app->app_version, 'app_server' => $_SERVER['SERVER_SOFTWARE'], 'app_id' => $app->app_id, 'app_name' => $app->app_name, 'app_logo_dark' => $app->app_logo_dark, 'app_logo_light' => $app->app_logo_light, 'app_motd' => $app->app_motd, 'app_domain' => $app->app_domain);

# ===================================== #
# ==== Generate User Details Array ==== #
# ===================================== # 
$currentuser = $_SESSION['user_id']; //Get current user id from session
$user_info = $db->prepare("SELECT * FROM hr_personal WHERE employee_id = :user_id LIMIT 1");
$user_info->bindParam(':user_id', $currentuser);
$user_info->execute();
$user = $user_info->fetch(PDO::FETCH_OBJ);

$email_hash = hash_hmac('sha256', $user->employee_id, '_redactedkey'); 

$log_info = $db->prepare("SELECT log_time FROM app_log WHERE log_user = :user_id ORDER BY log_time ASC LIMIT 1"); //Get 
$log_info->bindParam(':user_id', $currentuser);
$log_info->execute();
$first_seen = $log_info->fetch(PDO::FETCH_OBJ);

$user_details_array = array('id' => $user->employee_id, 'id_hash' => $email_hash, 'first_seen' => $first_seen->log_time, 'title' => $user->employee_title, 'name' => $user->employee_knownas, 'avatar' => $user->employee_avatar, 'mobile' => $user->employee_mobile, 'email' => $user->employee_email);


# ============================= #
# ==== Generate Chat Array ==== #
# ============================= #
$currentuser = $_SESSION['user_id'];
$currentapp = $app->app_id;

$chat_array = array();  

foreach (range('A', 'Z') as $char) {
    $individuals = array(); 
    $countrow = $db->prepare("SELECT * FROM hr_personal where employee_knownas LIKE '$char%' AND employee_id IN (SELECT notification_submitter FROM notification_sent WHERE notification_id IN (SELECT notification_id FROM notification_wait WHERE user_id = '$currentuser' AND method = 'online' ORDER BY notification_id DESC))");
    $countrow->execute();
        if ($countrow->rowCount() > 0) {
        $mesage_query = "SELECT * FROM hr_personal where employee_knownas LIKE '$char%' AND employee_id IN (SELECT notification_submitter FROM notification_sent WHERE notification_id IN (SELECT notification_id FROM notification_wait WHERE user_id = '$currentuser' AND method = 'online' ORDER BY notification_id DESC))";
        $message_query_run = $db->query($mesage_query);
        while($row = $message_query_run->fetch(PDO::FETCH_ASSOC)) {
            if(!empty($row['employee_avatar'])){
              $options = ['size' => 200, 'crop' => true];
              $image_file = "gs://rouic-cdn/internal/".$row['employee_avatar'];
              $image_url = CloudStorageTools::getImageServingUrl($image_file, $options);
              $image_url = preg_replace("/^http:/i", "https:", $image_url); 
            } else {
                $image_url = "";
            }

            $whileUser = $row['employee_id'];
            $message_chain = array();

            $chain_query = "SELECT * FROM notification_wait WHERE method = 'online' AND user_id = '$currentuser' AND notification_id IN (SELECT notification_id FROM notification_sent WHERE notification_submitter = '$whileUser')";
            $chain_query_run = $db->query($chain_query);
            while($chainRow = $chain_query_run->fetch(PDO::FETCH_ASSOC)) {
                array_push($message_chain, array('id' => $chainRow['notification_id'], 'reply_id' => $chainRow['reply_id'], 'content' => $chainRow['message'], 'time' => $chainRow['time'], 'state' => $chainRow['state']));
            }

            array_push($individuals, array('id' => $row['employee_id'], 'group' => $char, 'name' => $row['employee_knownas'], 'avatar' => $image_url, 'message_chain' => $message_chain));
        }
            array_push($chat_array, array('group' => $char, 'data' => $individuals));
    }

}   


# ========================================== #
# ==== Generate Admin Groups (if admin) ==== #
# ========================================== #
    $admin_array = array();
    if($auth->checkPage('admin', $_SESSION['user_id']) == false){ //Existing CheckPage class returns true if user is allowed on page
        array_push($admin_array, array('access' => 'denied'));
        } else {
        //Generate All Clients  
        $client_array = array();
        $client_query = "SELECT * FROM clients WHERE client_app = '$currentapp'";
        $client_query_run = $db->query($client_query);
        while($row = $client_query_run->fetch(PDO::FETCH_ASSOC)) {
        if(!empty($row['client_avatar'])){

          $options = ['size' => 200, 'crop' => true];
          $image_file = "gs://rouic-cdn/internal/".$row['client_avatar'];
          $image_url = CloudStorageTools::getImageServingUrl($image_file, $options);
          $image_url = '<span class="thumbnail-wrapper d48 circular inline m-t-5">
                                        <img id="dynamicavy" src="'.preg_replace("/^http:/i", "https:", $image_url).'" width="48" height="48">
                        </span>';   

        } else {
            $image_url = "";
        }

        $short_desc = strlen($row['client_desc']) > 30 ? substr($row['client_desc'],0,30)."..." : $row['client_desc'];
        $desc = '<span class="expandable" full-length="'.$row['client_desc'].'">'.$short_desc.'</span>';
        $actions = '<button class="btn btn-xs btn-primary">Edit Details</button>';
        array_push($client_array, array($row['client_id'], $image_url, "<b>".ucwords($row['client_name'])."</b>", $row['client_dob'], $row['client_phone'], $row['client_address'], $desc, $actions));
        }

        //Generate All Departments
            $department_array = array();
            $department_query = "SELECT * FROM departments WHERE dep_app = '$currentapp'";
            $department_query_run = $db->query($department_query);
            while($row = $department_query_run->fetch(PDO::FETCH_ASSOC)) {
            if(!empty($row['dep_avatar'])){

              $options = ['size' => 200, 'crop' => true];
              $image_file = "gs://rouic-cdn/internal/".$row['dep_avatar'];
              $image_url = CloudStorageTools::getImageServingUrl($image_file, $options);
              $image_url = '<span class="thumbnail-wrapper d48 circular inline m-t-5">
                                            <img id="dynamicavy" src="'.preg_replace("/^http:/i", "https:", $image_url).'" width="48" height="48">
                            </span>';

            } else {
                $image_url = "";
            }

            $short_desc = strlen($row['dep_description']) > 30 ? substr($row['dep_description'],0,30)."..." : $row['dep_description'];
            $desc = '<span class="expandable" full-length="'.$row['dep_description'].'">'.$short_desc.'</span>';
            $actions = '<button class="btn btn-xs btn-primary">Edit Details</button>';
            array_push($department_array, array($row['dep_id'], $image_url, "<b>".ucwords($row['dep_name'])."</b>", $desc, $actions));  
            }
            array_push($admin_array, array('access' => 'granted', 'allDepartments' => $department_array, 'allClients' => $client_array));       
    }


... About 4 More sections redacted ...


# ===================== #
# ==== Final Array ==== #
# ===================== #
$init_array_compare = array('chat' => $chat_array, 'admin' => $admin_array, 'app_details' => $app_settings_array, 'user_details' => $user_details_array,  'user_permissions' => $user_permissions_array);
$hash = md5(json_encode($init_array_compare)); //I'm basically creating a hash of the results here so I can see if anything has changed if generated again 

$init_array = array('hash' => $hash, 'chat' => $chat_array, 'admin' => $admin_array, 'app_details' => $app_settings_array, 'user_details' => $user_details_array,  'user_permissions' => $user_permissions_array);


echo json_encode($init_array);

1 个答案:

答案 0 :(得分:1)

MonkeyZeus是完全正确的,原来是CloudStorageTools::getImageServingUrl()引起了悬挂。在编写脚本以缓存生成的URL之后,脚本现在在一秒钟内运行。