如何调整服务器以更快地运行我的 PHP 脚本?

发布于 2024-10-05 04:28:56 字数 6360 浏览 0 评论 0原文

我有一个 PHP 脚本,它循环运行 x 次。 代码非常少并且执行检查。 有些循环将在 1 秒内运行,并且会有所不同。有些循环将运行最多 8 秒的延迟。

我如何或如何在服务器上调整/调整(或指示我的专用服务器的管理员)以将循环之间的时间减少到毫秒(或尽可能快)而不是 1 到 8 秒?

谢谢你!

PHP 代码

<style type="text/css">
#check { font: Arial, Helvetica, sans-serif; font-size: 12px; margin-bottom: 40px; }
.red { color: #C00; }
.green { color: #060; }
</style>

<?php        date_default_timezone_set('Australia/Brisbane');   $var1 = "variable";     $var2 = 1;      $var3=="no";
                while ($var2 <= 50)     {

                        $sApiUrl = "https://www.apiurl.com/";

                        $aParams = Array(
                                    'uid'     => "user",   
                                    'pw'      => "key",    
                                    'command' => "command",     
                                    'sld'     => "main",        
                                    'tld'     => "ext");         


                        $oCurl = curl_init();
                        curl_setopt($oCurl, CURLOPT_URL, $sApiUrl);
                        curl_setopt($oCurl, CURLOPT_POST, 1);
                        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $aParams);
                        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
                        $sResponse = curl_exec($oCurl);
                        curl_close($oCurl);


                        $oOutput = new SimpleXmlElement($sResponse);


                        foreach ($oOutput as $v1)
                          {  
                             foreach ($v1 as $v2)
                             { 
                                foreach ($v2 as $v3)
                                {
                                    foreach ($v3 as $v4=>$temp)
                                    {
                                         if($v4=="text")
                                            $var4 = $temp;
                                             if($var4=="true")
                                              {                                         
                                                    $aParams = Array(
                                                    'uid'     => "user",   
                                                    'pw'      => "key",  
                                                    'command' => "command",   
                                                    'sld'     => "main",       
                                                    'tld'     => "ext",
                                                    'dosomething' => "yes");    


                                                    $oCurl = curl_init();
                                                    curl_setopt($oCurl, CURLOPT_URL, $sApiUrl);
                                                    curl_setopt($oCurl, CURLOPT_POST, 1);
                                                    curl_setopt($oCurl, CURLOPT_POSTFIELDS, $aParams);
                                                    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
                                                    curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
                                                    $sResponse = curl_exec($oCurl);
                                                    curl_close($oCurl); 

                                                    $var3=="yes";   
                                              }                           
                                    }
                                }
                             }
                          }         $checktime = date('h:i:s A');                           echo "<div id=\"check\">$checktime, checked $var1<br>";         if ($var4=="true")          echo "<div class=\"green\"> $var4";         else            echo "<div class=\"red\"> $var4";
                if ($var3=="yes")           echo " and yes";    echo "</div></div>";        $var3=="no";    $loops++;   }        ?>

处理器信息

处理器 #1 供应商:GenuineIntel 处理器 #1 名称:Intel(R) Xeon(R) CPU E5405 @ 2.00GHz 处理器 #1 速度:1995.120 MHz 处理器 #1 高速缓存大小:6144 KB

处理器 #2 供应商:GenuineIntel 处理器 #2 名称:Intel(R) Xeon(R) CPU E5405 @ 2.00GHz 处理器 #2 速度:1995.120 MHz 处理器 #2 高速缓存大小:6144 KB

处理器 #3 供应商:GenuineIntel 处理器 #3 名称:Intel(R) Xeon(R) CPU E5405 @ 2.00GHz 处理器 #3 速度:1995.120 MHz 处理器 #3 高速缓存大小:6144 KB

处理器 #4 供应商:GenuineIntel 处理器 #4 名称:Intel(R) Xeon(R) CPU E5405 @ 2.00GHz 处理器 #4 速度:1995.120 MHz 处理器 #4 高速缓存大小:6144 KB

处理器 #5 供应商:GenuineIntel 处理器 #5 名称:Intel(R) Xeon(R) CPU E5405 @ 2.00GHz 处理器 #5 速度:1995.120 MHz 处理器 #5 高速缓存大小:6144 KB

处理器 #6 供应商:GenuineIntel 处理器 #6 名称:Intel(R) Xeon(R) CPU E5405 @ 2.00GHz 处理器 #6 速度:1995.120 MHz 处理器 #6 高速缓存大小:6144 KB

处理器 #7 供应商:GenuineIntel 处理器 #7 名称:Intel(R) Xeon(R) CPU E5405 @ 2.00GHz 处理器 #7 速度:1995.120 MHz 处理器 #7 高速缓存大小:6144 KB

处理器 #8 供应商:GenuineIntel 处理器 #8 名称:Intel(R) Xeon(R) CPU E5405 @ 2.00GHz 处理器 #8 速度:1995.120 MHz 处理器 #8 缓存大小:6144 KB

内存信息

崩溃内核的内存(0x0 到 0x0)不在允许的范围内 内存:8302344k/9175040k 可用(2176k 内核代码、80272k 保留、901k 数据、228k init、7466304k highmem)

系统信息

Linux server.myserver.com 2.6.18-194.17.1.el5PAE #1 SMP 9 月 29 日星期三 13:31:51 EDT 2010 i686 i686 i386 GNU/Linux

物理磁盘

SCSI 设备 sda:1952448512 512 字节 hdwr 扇区 (999654 MB) sda:写保护已关闭 sda:模式检测:03 00 00 08 SCSI 设备 sda:驱动器缓存:写回 SCSI 设备 sda:1952448512 512 字节 hdwr 扇区 (999654 MB) sda:写保护已关闭 sda:模式检测:03 00 00 08 SCSI 设备 sda:驱动器缓存:写回 sd 0:1:0:0:附加的 scsi 磁盘 sda sd 4:0:0:0:附加的scsi可移动磁盘sdb sd 0:1:0:0:附加的 scsi 通用 sg4 类型 0 sd 4:0:0:0:附加的 scsi 通用 sg7 类型 0

当前内存使用情况

         total       used       free     shared    buffers     cached

内存:8306672 7887632 419040 0 508076 6496876 -/+ 缓冲区/高速缓存:882680 7423992 兑换:4095992 428 4095564 总计:12402664 7888060 4514604

当前磁盘使用情况

使用的文件系统大小 可用使用百分比 安装在 /dev/mapper/VolGroup00-LogVol00 898G 200G 653G 24% / /dev/sda1 99M 19M 76M 20% /boot 无 4.0G 0 4.0G 0% /dev/shm /var/tmpMnt 4.0G 161M 3.6G 5% /tmp

I have a PHP script which runs on a loop for x amount of times.
The code is very minimal and performs checks.
Some loops will run within 1 second of each other and it varies.. some loops will run with up to 8 seconds delay.

How or what can I adjust/tweak on the server (or instruct the manager of my dedicated server) to reduce the time between loops to milliseconds (or as quick as possible) rather than 1 to 8 seconds?

Thank you!

PHP code

<style type="text/css">
#check { font: Arial, Helvetica, sans-serif; font-size: 12px; margin-bottom: 40px; }
.red { color: #C00; }
.green { color: #060; }
</style>

<?php        date_default_timezone_set('Australia/Brisbane');   $var1 = "variable";     $var2 = 1;      $var3=="no";
                while ($var2 <= 50)     {

                        $sApiUrl = "https://www.apiurl.com/";

                        $aParams = Array(
                                    'uid'     => "user",   
                                    'pw'      => "key",    
                                    'command' => "command",     
                                    'sld'     => "main",        
                                    'tld'     => "ext");         


                        $oCurl = curl_init();
                        curl_setopt($oCurl, CURLOPT_URL, $sApiUrl);
                        curl_setopt($oCurl, CURLOPT_POST, 1);
                        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $aParams);
                        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
                        $sResponse = curl_exec($oCurl);
                        curl_close($oCurl);


                        $oOutput = new SimpleXmlElement($sResponse);


                        foreach ($oOutput as $v1)
                          {  
                             foreach ($v1 as $v2)
                             { 
                                foreach ($v2 as $v3)
                                {
                                    foreach ($v3 as $v4=>$temp)
                                    {
                                         if($v4=="text")
                                            $var4 = $temp;
                                             if($var4=="true")
                                              {                                         
                                                    $aParams = Array(
                                                    'uid'     => "user",   
                                                    'pw'      => "key",  
                                                    'command' => "command",   
                                                    'sld'     => "main",       
                                                    'tld'     => "ext",
                                                    'dosomething' => "yes");    


                                                    $oCurl = curl_init();
                                                    curl_setopt($oCurl, CURLOPT_URL, $sApiUrl);
                                                    curl_setopt($oCurl, CURLOPT_POST, 1);
                                                    curl_setopt($oCurl, CURLOPT_POSTFIELDS, $aParams);
                                                    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
                                                    curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
                                                    $sResponse = curl_exec($oCurl);
                                                    curl_close($oCurl); 

                                                    $var3=="yes";   
                                              }                           
                                    }
                                }
                             }
                          }         $checktime = date('h:i:s A');                           echo "<div id=\"check\">$checktime, checked $var1<br>";         if ($var4=="true")          echo "<div class=\"green\"> $var4";         else            echo "<div class=\"red\"> $var4";
                if ($var3=="yes")           echo " and yes";    echo "</div></div>";        $var3=="no";    $loops++;   }        ?>

Processor Information

Processor #1 Vendor: GenuineIntel
Processor #1 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Processor #1 speed: 1995.120 MHz
Processor #1 cache size: 6144 KB

Processor #2 Vendor: GenuineIntel
Processor #2 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Processor #2 speed: 1995.120 MHz
Processor #2 cache size: 6144 KB

Processor #3 Vendor: GenuineIntel
Processor #3 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Processor #3 speed: 1995.120 MHz
Processor #3 cache size: 6144 KB

Processor #4 Vendor: GenuineIntel
Processor #4 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Processor #4 speed: 1995.120 MHz
Processor #4 cache size: 6144 KB

Processor #5 Vendor: GenuineIntel
Processor #5 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Processor #5 speed: 1995.120 MHz
Processor #5 cache size: 6144 KB

Processor #6 Vendor: GenuineIntel
Processor #6 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Processor #6 speed: 1995.120 MHz
Processor #6 cache size: 6144 KB

Processor #7 Vendor: GenuineIntel
Processor #7 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Processor #7 speed: 1995.120 MHz
Processor #7 cache size: 6144 KB

Processor #8 Vendor: GenuineIntel
Processor #8 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
Processor #8 speed: 1995.120 MHz
Processor #8 cache size: 6144 KB

Memory Information

Memory for crash kernel (0x0 to 0x0) notwithin permissible range
Memory: 8302344k/9175040k available (2176k kernel code, 80272k reserved, 901k data, 228k init, 7466304k highmem)

System Information

Linux server.myserver.com 2.6.18-194.17.1.el5PAE #1 SMP Wed Sep 29 13:31:51 EDT 2010 i686 i686 i386 GNU/Linux

Physical Disks

SCSI device sda: 1952448512 512-byte hdwr sectors (999654 MB)
sda: Write Protect is off
sda: Mode Sense: 03 00 00 08
SCSI device sda: drive cache: write back
SCSI device sda: 1952448512 512-byte hdwr sectors (999654 MB)
sda: Write Protect is off
sda: Mode Sense: 03 00 00 08
SCSI device sda: drive cache: write back
sd 0:1:0:0: Attached scsi disk sda
sd 4:0:0:0: Attached scsi removable disk sdb
sd 0:1:0:0: Attached scsi generic sg4 type 0
sd 4:0:0:0: Attached scsi generic sg7 type 0

Current Memory Usage

         total       used       free     shared    buffers     cached

Mem: 8306672 7887632 419040 0 508076 6496876
-/+ buffers/cache: 882680 7423992
Swap: 4095992 428 4095564
Total: 12402664 7888060 4514604

Current Disk Usage

Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
898G 200G 653G 24% /
/dev/sda1 99M 19M 76M 20% /boot
none 4.0G 0 4.0G 0% /dev/shm
/var/tmpMnt 4.0G 161M 3.6G 5% /tmp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆 2024-10-12 04:28:56

您是否使用数据库、电子邮件、网络服务或任何其他类似组件?一般来说,PHP 脚本的性能最依赖于这些。

您可以使用 Xdebug 等分析工具来分析脚本并找到最慢的代码区域。然后您可以优化此代码(例如内部循环)以提高性能。

Do you use databases, emails, web services, or any other similar component? Generally, the performance of a PHP script is most dependent on these.

You can use a profiling tool like Xdebug to analyse your script and find the slowest areas of code. You can then optimise this code (e.g. inner loops) to improve performance.

人心善变 2024-10-12 04:28:56

我个人没有使用过这个,但我怀疑您需要研究在 PHP 中使用 curl_multi* 东西 - 这似乎允许您一次执行多个请求。

请参阅 http://php.net/manual/en/function.curl- multi-init.php

您的瓶颈几乎肯定是串行建立 HTTP 连接。据我所知,curl_multi_* 函数将允许这些函数并行执行。

I've not used this personally, but I suspect you need to look into using the curl_multi* stuff in PHP - this appears to allow you to perform multiple requests at once.

See http://php.net/manual/en/function.curl-multi-init.php

Your bottleneck will almost certainly be making the HTTP connections serially. AFAIK the curl_multi_* functions will allow these to be performed in parallel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文