High-performance websites and HipHop php instructions

Source: Internet
Author: User
Tags php compiler

Here, HipHop is not hip-hop. I don't like hip hop.
HipHop is Facebook's open-source php compiler. It can convert php into C ++ and then compile it into executable files to improve php Execution efficiency.
The installation process is complicated and there are many dependencies. Boost, Onigumura, tbb, icu, mysql, flex, re2c and so on, as well as libevent and curl of facebook patch version.
In fact, the two powerful open-source websites of facebook, hiphop and scribe, are under pseudoscience in accordance with the scientific philosophy, and are difficult to install and compile. The installation and compilation process is basically not reproducible, it seems that the user can install it without modifying the source code. So you know that
Therefore, for inexperienced users, according to official instructions, we can only say that there is a certain chance to install them.
Therefore, you do not need to reproduce the installation process. Even if I write it out, it may not be possible to install it according to the steps. Facebook provides official installation guides for ubuntu and centos. For details, refer to the official installation guide. However, one installation libunwind library is missing from the official guide.
Write a program to test the php performance after hiphop compilation.
<? Php
Function BubbleSort ($ str)
{
For ($ I = 0; $ I <count ($ str); $ I ++)
{
For ($ j = count ($ str)-2; $ j >=$ I; $ j --)
{
If ($ str [$ j + 1] <$ str [$ j])
{
$ Tmp = $ str [$ j + 1];
$ Str [$ j + 1] = $ str [$ j];
$ Str [$ j] = $ tmp;
}
}
}
Return $ str;
}

For ($ I = 0; I I <10000; $ I ++)
{
$ Str [$ I] = rand (0, 20000 );
}
BubbleSort ($ str );
Echo "finished ";
?>
Generate 10000 random numbers between 0 and, and then sort them by bubble. Because the efficiency of bubble is relatively low, you can intuitively see the difference in execution efficiency before and after compilation.
Compile with hiphop
[Root @ localhost scribe] # hphp -- keep-tempdir = 1 -- log = 3 random. php
Running hphp...
Creating temporary directory/tmp/hphp_4DOImV...
Parsing inputs...
Parsing./random. php...
Parsing inputs took 0 '00 "(2 MS) wall time
Pre-optimizing...
Pre-optimizing took 0 '00 "(0 MS) wall time
Inferring types...
Inferring types took 0 '00 "(0 MS) wall time
Post-optimizing...
Post-optimizing took 0' 00 "(0 MS) wall time
Creating CPP files...
Creating CPP files took 0 '00 "(35 MS) wall time
Compiling and linking CPP files...

Compiling and linking CPP files took 0 '35 "(35732 MS) wall time
Running executable/tmp/hphp_4DOImV/program -- file random. php...
Finishedall files saved in/tmp/hphp_4DOImV...
Running hphp took 0 '42 "(42126 MS) wall time
-- Keep-tempdir = 1 indicates that the temporary directory is not deleted after C ++ code is generated and compiled. -- Log = 3 indicates the log output level on the console, and 4 indicates the most detailed notice output. The maximum value is 4.
After compilation is completed, the path is/tmp/hphp_4DOImV.
First, let's look at the file generation. First, let's look at the efficiency of the original php Execution. Execute it three times and estimate the average time.
[Root @ localhost scribe] # time php random. php
Finished
Real 0m14. 776 s
User 0m14. 737 s
Sys 0m0. 024 s
[Root @ localhost scribe] # time php random. php
Finished
Real 0m14. 801 s
User 0m14. 744 s
Sys 0m0. 047 s
[Root @ localhost scribe] # time php random. php
Finished
Real 0m14. 787 s
User 0m14. 734 s
Sys 0m0. 037 s
Then go to/tmp/hphp_4DOImV
[Root @ localhost hphp_4DOImV] # ls
CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt Makefile php program sys
[Root @ localhost hphp_4DOImV] # time./program
Finished
Real 0m6. 562 s
User 0m6. 471 s
Sys 0m0. 068 s
[Root @ localhost hphp_4DOImV] # time./program
Finished
Real 0m6. 100 s
User 0m6. 067 s
Sys 0m0. 012 s
[Root @ localhost hphp_4DOImV] # time./program
Finished
Real 0m6. 107 s
User 0m6. 060 s
Sys 0m0. 027 s
Wow, looks very good, from about 15 seconds of execution time, after compilation is about 6 seconds. Performance improvement is impressive. It is very suitable for websites with high stress.
Then, let's see how to use it in the web environment. Hiphop is like this. After compilation, the executable file itself comes with a webserver. Therefore, we only need to start the executable file as a webserver, then use nginx for reverse generation, so nginx does not need to be configured. Start the program compiled by hiphop. Let's first look at the options provided by this executable file.
[Root @ localhost hphp_4DOImV] #./program -- help
Usage:

./Program [-m <mode>] [<options>] [<arg1>] [<arg2>]...

Options:
-- Help display this message
-M [-- mode] arg (= run) run | server | daemon | replay | translate
-C [-- config] arg load specified config file
-V [-- config-value] arg individual configuration string in a format
Of name = value, where name can be any valid
Configuration for a config file
-P [-- port] arg (=-1) start an HTTP server at specified port
-- Admin-port arg (=-1) start admin listerner at specified port
-U [-- user] arg run server under this user account
-F [-- file] arg executing specified file
-- Count arg (= 1) how many times to repeat execution
-- No-safe-access-check arg (= 0) whether to ignore safe file access check
-- Arg arguments
-- Extra-header arg extra-header to add to log lines
-- Build-id arg unique identifier of compiled server code
Here, the-m option and-p option are required for starting the web service. For debugging, we can.
[Root @ localhost hphp_4DOImV] #./program-m server-p 8080
Loading static content...
Loading static content took 0' 00 "(0 MS) wall time
Page server started
Admin server started
All servers started

Then we can access it with curl. Note: What is the name of php before compilation, and what is the name of php after compilation and running in webserver.
[Root @ localhost ~] # Curl-I http: // localhost: 8080/random. php
HTTP/1.1 200 OK
Content-Type: text/html; charset = UTF-8
Date: Mon, 13 Aug 2012 10:39:09 GMT
Content-Length: 8

Finished
In the production environment, you can replace the-m server option with-m daemon to start the background process.
Then, replace your nginx with port 8080 to allow normal access in the web environment.
There are several precautions that are not mentioned in the online articles.
1. After hiphop compilation, you can capture executable files and run them in the same environment as the operating system of the compiler. However, the runtime environment requires the same boost, re2c, oniguruma, mysql, gd, and other dynamic connection libraries required for hiphop installation.
2. the eval function is unavailable. Socket-related functions, such as abstract and interface, may cause the Segmentation Fault of conversion, resulting in the failure to convert and compile, but it is possible and not necessary.
3. All php files are in an executable file after compilation. After running daemon, how did you access it? How did you access it now? The file name and connection remain unchanged.
4. Any include or require statement cannot be connected by a relative method. The absolute connection of the operating system must be used. Otherwise, the system will prompt that the file cannot be found.
5. Don't try to compile phpinfo (). You can't get anything.
6. The generated C ++ is quite readable.
Finally, may God bless you for successful installation.

This article is from the "practice test truth" blog. For more information, contact the author!

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.