【问题标题】:Reducing Server Response Time Where Loads Many Resources减少加载许多资源的服务器响应时间
【发布时间】:2016-03-20 10:56:00
【问题描述】:

我有两个 PHP 脚本,它们从 API 加载许多可变资源,导致响应时间长达 2.2 秒到 4 秒。任何关于如何减少响应时间和提高效率的建议将不胜感激?

第一个脚本

require('path/to/local/API_2');

//Check if user has put a query and that it's not empty
if (isset($_GET['query']) && !empty($_GET['query'])) {
    //$query is user input
    $query = str_replace(" ", "+", $_GET['query']);
    $query = addslashes($query);

    //HTTP Request to API_1
    //Based on $query
    //Max Variable is ammount of results I want to get back in JSON format
    $varlist = file_get_contents("http://ADRESS_OF_API_1.com?$query&max=10");

    //Convert JSON to Array()
    $varlist = json_decode($varlist, true);

    //Initializing connection to API_2
    $myAPIKey = 'KEY';
    $client = new APIClient($myAPIKey, 'http://ADRESS_OF_API_2.com');
    $Api = new API_FUNCTION($client);

    $queries = 7;
    //Go through $varlist and get data for each element in array then use it in HTML
    //Proccess all 8 results from $varlist array()
    for ($i = 0; $i <= $queries; ++$i) {

        //Get info from API based on ID included in first API data
        //I don't use all info, but I can't control what I get back.
        $ALL_INFO = $Api->GET_FUNCTION_1($varlist[$i]['id']);

        //Seperate $ALL_INFO into info I use
        $varlist[$i]['INFO_1'] = $ALL_INFO['PATH_TO_INFO_1'];
        $varlist[$i]['INFO_2'] = $ALL_INFO['PATH_TO_INFO_2'];

        //Check if info exists
        if($varlist[$i]['INFO_1']) {
        //Concatenate information into HTML
            $result.='
                <div class="result">
                    <h3>'.$varlist[$i]['id'].'</h3>
                    <p>'.$varlist[$i]['INFO_1'].'</p>
                    <p>'.$varlist[$i]['INFO_2'].'</p>
                </div>';
        } else {
            //In case of no result for specific Info ID increase
            //Allows for 3 empty responses
            ++$queries;
        }
    }
} else {
    //If user didn't enter a query, relocates them back to main page to enter one.
    header("Location: http://websitename.com");
    die();
}`

注意:$result 等于每次循环中的 HTML 信息。

注意:几乎所有时间都花在for ($i = 0; $i &lt;= 7; ++$i) 循环。

第二个脚本

//Same API as before 
require('path/to/local/API_2');

//Check if query is set and not empty
if (isset($_GET['query']) && !empty($_GET['query'])) {
    //$query is specific $varlist[$i]['id'] for more information on that data
    $query['id'] = str_replace(" ", "+", $_GET['query']);
    $query['id'] = addslashes($query['id']);

    //Initializing connection to only API used in this script
    $myAPIKey = 'KEY';
    $client = new APIClient($myAPIKey, 'http://ADRESS_OF_API_2.com');
    $Api = new API_FUNCTION($client);

    $ALL_INFO_1 = $Api->GET_FUNCTION_1($query['id']);
    $query['INFO_ADRESS_1.1'] = $ALL_INFO_1['INFO_ADRESS_1'];
    $query['INFO_ADRESS_1.2'] = $ALL_INFO_2['INFO_ADRESS_2'];
    $ALL_INFO_2 = $Api->GET_FUNCTION_2($query['id']);
    $query['INFO_ADRESS_2.1'] = $ALL_INFO_3['INFO_ADRESS_3'];
    $ALL_INFO_3 = $Api->GET_FUNCTION_3($query['id']);
    $query['INFO_ADRESS_3.1'] = $ALL_INFO_4['INFO_ADRESS_4'];             
    $ALL_INFO_4 = $Api->GET_FUNCTION_4($query['id']);
    $query['INFO_ADRESS_4.1'] = $ALL_INFO_5['INFO_ADRESS_5'];
    $query['INFO_ADRESS_4.2'] = $ALL_INFO_6['INFO_ADRESS_6'];
    $ALL_INFO_5 = $Api->GET_FUNCTION_5($query['id']);
    $query['INFO_ADRESS_5.1'] = $ALL_INFO_7['INFO_ADRESS_7'];
}

$result = All of the $query data from the API;
} else {
    //If no query relocates them back to first PHP script page to enter one.
    header("Location: http://websitename.com/search");
    die();
}`

注意:与第一个脚本类似,大部分时间都花在获取信息上 来自辅助 API。

注意:在第二个脚本中,第一个 API 被一个 第一个脚本页面中的特定变量,所以 $varlist[$i]['id'] = $query['id']。

注意:同样,$result 是 HTML 数据。

【问题讨论】:

  • API 响应多久更改一次?我的意思是,如果这就像你要拉的新闻提要,它不会经常改变。您可以使用 cron 拉取和缓存请求,然后从缓存副本中拉取。
  • 查询变化时,API 响应会发生变化,这种变化非常频繁。不过,可能值得花时间看看是否有更常见的某些查询。
  • 如果 API2 有一个可以获取多个结果的方法,那么您可以将 http 请求从 8 个减少到 2 个。除此之外,尽可能缓存所有内容。如果您仍然无法减少时间,请确保您异步调用此代码(例如使用 ajax),这样您就可以向用户显示一些建议正在取得进展的内容(微调器、进度条等)
  • 您还可以将 API 调用从正常的页面加载中移出。使用通用页面响应用户以显示正在发生的事情,然后发出 ajax 请求以查询 API 并以数据响应。

标签: php apache performance api server


【解决方案1】:

您还可以将 API 调用从正常的页面加载中移出。使用通用页面响应用户以显示正在发生的事情,然后发出 ajax 请求以查询 API 并以数据响应。确实没有办法加快单个外部请求。您最好的选择是:

  1. 尽量减少请求的数量(即使这意味着您请求更多的数据一次然后过滤掉您的一方,而不是针对一小部分数据发送多个请求)。
  2. 缓存所有剩余请求并从缓存中提取。
  3. 响应一个小页面,让用户知道正在发生的事情,并对查询的数据发出单独的 ajax 请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-23
    • 2013-08-31
    • 2014-12-01
    • 1970-01-01
    • 2021-08-26
    • 2020-02-14
    相关资源
    最近更新 更多