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


Speeding up a very slow PHP script generating a large JSON array

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

下面的代码工作得很好并且做了我想要的一切,但是需要大约10秒来执行。可能听起来不多,但确实感觉不合适。我想知道我应该做些什么来加快初始数组的创建。

到目前为止,我已经考虑过几种方法来解决这个问题;

  • 我最初可以加载更少的数据,减少加载时间实际上装载量更少。这将影响总体性能不过,就像现在一样,一旦数据加载了,每个"section"都加载了瞬间被填充,超级快。
  • 我可以使用应用引擎缓存一些数据内置Memcache。这是一个简洁的想法,但是数据生成经常改变。通常情况下,缓存无法做到这一点。
  • 使用网络套接字。我想最终做到这一点,它在待办事项中列表,但这将意味着重写大量的代码,所以我不太我现在很喜欢这个

我确信在我的代码中也有一些"不好的做法",请随意指出它们并批评我…如果你喜欢的话,下面的代码与Gist相同。

<?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);

MonkeyZeus是完全正确的,原来是CloudStorageTools::getImageServingUrl()引起了绞刑。在编写了一个脚本来缓存它生成的URL之后,脚本现在运行在不到一秒的时间内。