Apache HTTP keepalive application test and change

Source: Internet
Author: User
Tags explode fread trim

Keep-alive:timeout=5, max=100
Timeout: Expires in 5 seconds (corresponding to httpd.conf in the parameter is: keepalivetimeout), Max is up to 100 requests, Force disconnect
It's a new connection over the timeout time, and Max will automatically subtract 1 until it's 0, forcing it to break off. See the following four graphs, note the date value (the time difference is within 5 seconds)!

Apache Change Header's connection:keep-alive

Change the keepalive parameter to off to make the connection:keep-alive into connection:closed

The header's output becomes connection:close.

To modify the Apache configuration file, the general space does not have this permission

The application in practice

The above implementation phttp_get is similar to the "Stay Connected" concept in MySQL memcache, which generally applies only to Web servers in fastcgi mode.
For HTTP communication between the machines, it is very logical to discover the advantages of Phttp_get in the testing process.
The advantages of Phttp_get are also not obvious for the service with longer processing time.
In summary, the phttp_get applies to the FastCGI-mode Web application calling the remote HTTP service, and this HTTP server has a relatively short response time.

Five, the service side need to pay attention to the matter

1,http server must support http/1.1 protocol
The 2,php application must return the header of Content-length: a specific implementation see:

http://cn.php.net/manual/en/function.ob-get-length.php

You need to add in your code:

The code is as follows Copy Code
Ob_start ();
$size =ob_get_length ();
Header ("Content-length: $size");
Ob_end_flush ();

Finally, attach the test code:

The code is as follows Copy Code

<?php

$url =http://10.69.2.206:8080/sms/ns2/save_msg.txt

function Ohttp_get ($host, $port, $query,& $body)
{
$FP =pfsockopen ($host, $port, $errno, $ERRSTR, 1);
if (! $fp)
{
Var_dump ($errno, $ERRSTR);
return-1;
}
$out = "Get ${query} http/1.1rn";
$out. = "Host: ${host}rn";
$out. = "Connection:closern";
$out. = "RN";
Fwrite ($fp, $out);
$line =trim (fgets ($fp));
$header. = $line;
List ($proto, $rcode, $result) =explode ("", $line);
$len =-1;
while (($line =trim (fgets ($FP)))!= "")
{
$header. = $line;
if (Strstr ($line, "content-length:"))
{
List ($CL, $len) =explode ("", $line);
}
if (Strstr ($line, "Connection:close"))
{
$close =true;
}
}
if ($len < 0)
{
echo "Ohttp_get must cope with content-length header!n";
return-1;
}
$body =fread ($fp, $len);
if ($close)
Fclose ($FP);
return $rcode;
}
function Phttp_get ($host, $port, $query,& $body)
{
$FP =pfsockopen ($host, $port, $errno, $ERRSTR, 1);
if (! $fp)
{
Var_dump ($errno, $ERRSTR);
return-1;
}
$out = "Get ${query} http/1.1rn";
$out. = "Host: ${host}rn";
$out. = "Connection:keep-alivern";
$out. = "RN";
Fwrite ($fp, $out);
$line =trim (fgets ($fp));
$header. = $line;
List ($proto, $rcode, $result) =explode ("", $line);
$len =-1;
while (($line =trim (fgets ($FP)))!= "")
{
$header. = $line;
if (Strstr ($line, "content-length:"))
{
List ($CL, $len) =explode ("", $line);
}
if (Strstr ($line, "Connection:close"))
{
$close =true;
}
}
if ($len < 0)
{
echo "Phttp_get must cope with content-length header!n";
return-1;
}
$body =fread ($fp, $len);
if ($close)
Fclose ($FP);
return $rcode;
}

$time 1=microtime (TRUE);
For ($i =0 $i <10000; $i + +)
{
$host = "10.69.2.206″;
$port = 8080;
$query = "/sms/ns2/save_msg.txt";
$body = "";
$r =ohttp_get ($host, $port, $query, $body);
if ($r!= 200)
{
echo "Return code: $RN";
}
}
$time 2=microtime (TRUE);
For ($i =0 $i <10000; $i + +)
{
$url = "Http://10.69.2.206:8080/sms/ns2/save_msg.txt";
$host = "10.69.2.206″;
$port = 8080;
$query = "/sms/ns2/save_msg.txt";
$body = "";
$r =phttp_get ($host, $port, $query, $body);
if ($r!= 200)
{
echo "Return code: $RN";
}
}
$time 3=microtime (TRUE);
for ($i =0; $i Array (' timeout ' => 1)
)
);
$body =file_get_contents ($url, 0, $ctx);
$r = 200;
if ($r!= 200)
{
echo "Return code: $RN";
}
}
$time 4=microtime (TRUE);

echo "Phttp_get:". ($time 3-$time 2). " n ";
echo "Ohttp_get:". ($time 2-$time 1). " n ";
echo "file_get_contents:". ($time 4-$time 3). " n ";

?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.