问题描述
我有一个PHP脚本,该脚本在循环中运行了x次.该代码非常少,并执行检查.某些循环会在彼此之间的1秒钟内运行,并且会有所不同..某些循环将以最多8秒的延迟运行.
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.
如何或在服务器上进行什么调整/调整(或指示我的专用服务器的管理员),以将循环之间的时间减少到毫秒(或尽可能快)而不是1到8秒?
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?
谢谢!
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供应商:真正的英特尔处理器#1名称:英特尔(R)至强(R)CPU E5405 @ 2.00GHz处理器#1速度:1995.120 MHz处理器#1缓存大小:6144 KB
Processor #1 Vendor: GenuineIntelProcessor #1 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHzProcessor #1 speed: 1995.120 MHzProcessor #1 cache size: 6144 KB
处理器#2供应商:真正的英特尔处理器#2名称:英特尔(R)至强(R)CPU E5405 @ 2.00GHz处理器#2速度:1995.120 MHz处理器#2缓存大小:6144 KB
Processor #2 Vendor: GenuineIntelProcessor #2 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHzProcessor #2 speed: 1995.120 MHzProcessor #2 cache size: 6144 KB
处理器#3供应商:真正的英特尔处理器#3名称:英特尔(R)至强(R)CPU E5405 @ 2.00GHz3号处理器速度:1995.120 MHz3号处理器的高速缓存大小:6144 KB
Processor #3 Vendor: GenuineIntelProcessor #3 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHzProcessor #3 speed: 1995.120 MHzProcessor #3 cache size: 6144 KB
处理器#4供应商:真正的英特尔处理器#4名称:英特尔(R)至强(R)CPU E5405 @ 2.00GHz处理器#4速度:1995.120 MHz4号处理器的高速缓存大小:6144 KB
Processor #4 Vendor: GenuineIntelProcessor #4 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHzProcessor #4 speed: 1995.120 MHzProcessor #4 cache size: 6144 KB
处理器#5供应商:真正的英特尔处理器#5名称:英特尔(R)至强(R)CPU E5405 @ 2.00GHz5号处理器速度:1995.120 MHz5号处理器的高速缓存大小:6144 KB
Processor #5 Vendor: GenuineIntelProcessor #5 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHzProcessor #5 speed: 1995.120 MHzProcessor #5 cache size: 6144 KB
处理器#6供应商:真正的英特尔处理器#6名称:英特尔(R)至强(R)CPU E5405 @ 2.00GHz6号处理器速度:1995.120 MHz6号处理器的高速缓存大小:6144 KB
Processor #6 Vendor: GenuineIntelProcessor #6 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHzProcessor #6 speed: 1995.120 MHzProcessor #6 cache size: 6144 KB
处理器#7供应商:真正的英特尔处理器#7名称:英特尔(R)至强(R)CPU E5405 @ 2.00GHz7号处理器速度:1995.120 MHz7号处理器的高速缓存大小:6144 KB
Processor #7 Vendor: GenuineIntelProcessor #7 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHzProcessor #7 speed: 1995.120 MHzProcessor #7 cache size: 6144 KB
处理器#8供应商:真正的英特尔处理器#8名称:英特尔(R)至强(R)CPU E5405 @ 2.00GHz处理器#8速度:1995.120 MHz#8处理器高速缓存大小:6144 KB
Processor #8 Vendor: GenuineIntelProcessor #8 Name: Intel(R) Xeon(R) CPU E5405 @ 2.00GHzProcessor #8 speed: 1995.120 MHzProcessor #8 cache size: 6144 KB
内存信息
崩溃内核(0x0到0x0)的内存不在允许的范围内内存:可用8302344k/9175040k(2176k内核代码,保留80272k,901k数据,228k init,7466304k highmem)
Memory for crash kernel (0x0 to 0x0) notwithin permissible rangeMemory: 8302344k/9175040k available (2176k kernel code, 80272k reserved, 901k data, 228k init, 7466304k highmem)
系统信息
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
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
物理磁盘
SCSI设备sda:1952448512 512字节hdwr扇区(999654 MB)sda:写保护已关闭sda:模式检测:03 00 00 08SCSI设备sda:驱动器缓存:写回SCSI设备sda:1952448512 512字节hdwr扇区(999654 MB)sda:写保护已关闭sda:模式检测:03 00 00 08SCSI设备sda:驱动器缓存:写回sd 0:1:0:0:附加的scsi磁盘sdasd 4:0:0:0:附加的scsi可移动磁盘sdbsd 0:1:0:0:附加的scsi通用sg4类型0sd 4:0:0:0:附加的scsi通用sg7类型0
SCSI device sda: 1952448512 512-byte hdwr sectors (999654 MB)sda: Write Protect is offsda: Mode Sense: 03 00 00 08SCSI device sda: drive cache: write backSCSI device sda: 1952448512 512-byte hdwr sectors (999654 MB)sda: Write Protect is offsda: Mode Sense: 03 00 00 08SCSI device sda: drive cache: write backsd 0:1:0:0: Attached scsi disk sdasd 4:0:0:0: Attached scsi removable disk sdbsd 0:1:0:0: Attached scsi generic sg4 type 0sd 4:0:0:0: Attached scsi generic sg7 type 0
当前内存使用情况
total used free shared buffers cached
Mem:8306672 7887632 419040 0 508076 6496876-/+缓冲区/缓存:882680 7423992掉期:4095992 428 4095564总计:12402664 7888060 4514604
Mem: 8306672 7887632 419040 0 508076 6496876-/+ buffers/cache: 882680 7423992Swap: 4095992 428 4095564Total: 12402664 7888060 4514604
当前磁盘使用情况
使用的文件系统大小可用百分比已安装在/dev/mapper/VolGroup00-LogVol00 898G 200G 653G 24%//dev/sda1 99M 19M 76M 20%/启动无4.0G 0 4.0G 0%/dev/shm/var/tmpMnt 4.0G 161M 3.6G 5%/tmp
Filesystem Size Used Avail Use% Mounted on/dev/mapper/VolGroup00-LogVol00 898G 200G 653G 24% //dev/sda1 99M 19M 76M 20% /bootnone 4.0G 0 4.0G 0% /dev/shm/var/tmpMnt 4.0G 161M 3.6G 5% /tmp
推荐答案
我个人没有使用过,但是我怀疑您需要研究使用PHP中的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.
请参见 http://php.net/manual/zh/function .curl-multi-init.php
几乎可以肯定,您的瓶颈将是串行建立HTTP连接. AFAIK curl_multi_ *函数将允许它们并行执行.
Your bottleneck will almost certainly be making the HTTP connections serially. AFAIK the curl_multi_* functions will allow these to be performed in parallel.
这篇关于如何调整服务器以更快地运行PHP脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!