SlideShare une entreprise Scribd logo
1  sur  28
Télécharger pour lire hors ligne
Anatomy of a PHP
    Request
              Joseph Scott
             15 April 2010
                  UPHPU
Lifespan
.php



Read   Parse   Compile   Execute   Output
Lifespan
.php



Read   Parse    Compile   Execute   Output


       Apache      /      mod_php
Lifespan
.php



Read   Parse   Compile   Execute   Output


               FastCGI


                Nginx
Reading
Open    Read     Close
Reading
Open            Read        Close


   Avoid   co$tly services like NFS
Parse
 hello.php

         <?php
         echo "Hello, World!n"


Parse error: syntax error, unexpected $end,
expecting ',' or ';' in /tmp/hello.php on line 3
Tokens
              <?php echo
$tokens = token_get_all( '

  "Hello, World!";' );
foreach ( $tokens as $token ) {
    if ( is_array( $token ) ) {
        echo token_name( $token[0] ) . " ( {$token[2]} ) -
    {$token[1]}n";
    } else {
        echo "{$token}n";
    }
}
Tokens

T_OPEN_TAG ( 1 ) - <?php
T_ECHO ( 1 ) - echo
T_WHITESPACE ( 1 ) -
T_CONSTANT_ENCAPSED_STRING ( 1 ) - "Hello,
  World!"
;
Compile
Branch analysis from position: 0
Return found
filename:     /tmp/hello.php
function name: (null)
number of ops: 3
compiled vars: none
line # op                              fetch         ext return operands
-------------------------------------------------------------------------------
   2 0 ECHO                                                       'Hello%2C+World%21%0A'
   3 1 RETURN                                                      1
        2* ZEND_HANDLE_EXCEPTION
Execute
Deceptively Simple Term

•Includes all sorts of activity
•Database / remote requests
Output
•PHP can buffer output
•Don’t forget about compression
Making Things Faster
Making Things Faster
.php



Read   Parse   Compile   Execute   Output
Making Things Faster
.php



Read   Parse APC
               Compile   Execute   Output
Making Things Faster
.php



Read   Parse APC
               Compile   Execute   Output




        opcode cache
The PHP Point of View
      opcodes

VLD - Vulcan Logic Dumper
Example PHP

$html = file_get_contents( 'http://www.google.com/' );
$regex_pattern = '!<a href="[^>]*">(.*?)</a>!';
preg_match_all( $regex_pattern, $html, $matches );
Example PHP - VLD
Branch analysis from position: 0
Return found
filename:     /drive/home/joseph/vld/parse-links.php
function name: (null)
number of ops: 17
compiled vars: !0 = $html, !1 = $regex_pattern, !2 =
$matches
Example PHP - VLD
line # op                              fetch         ext return operands
-------------------------------------------------------------------------------
   4 0 EXT_STMT
        1 EXT_FCALL_BEGIN
        2 SEND_VAL                                                  'http%3A%2F%2Fwww.google.com%2F'
        3 DO_FCALL                                         1           'file_get_contents'
        4 EXT_FCALL_END
        5 ASSIGN                                                 !0, $0
   5 6 EXT_STMT
        7 ASSIGN                                                 !1, '%21%3Ca+href%3D%22%5B%5E%3E
%5D%2A%22%3E%28.%2A%3F%29%3C%2Fa%3E%21'
   6 8 EXT_STMT
        9 EXT_FCALL_BEGIN
       10 SEND_VAR                                                    !1
       11 SEND_VAR                                                    !0
       12 SEND_REF                                                   !2
       13 DO_FCALL                                          3           'preg_match_all'
       14 EXT_FCALL_END
   7 15 RETURN                                                      1
       16* ZEND_HANDLE_EXCEPTION
PHP - strace

• System calls, expect LOTS of data
• strace -o out php parse-links.php
PHP - strace

• System calls, expect LOTS of data
• strace -o out php parse-links.php

        1,786 lines of output!
1.
                          PHP - strace
   socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3
2. fcntl64(3, F_GETFL) = 0x2 (flags O_RDWR)
3. fcntl64(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0
4. connect(3, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("64.233.169.99")}, 16) =
   -1 EINPROGRESS (Operation now in progress)
5. poll([{fd=3, events=POLLIN|POLLOUT|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=3,
   revents=POLLOUT}])
6. getsockopt(3, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
7. fcntl64(3, F_SETFL, O_RDWR) = 0
8. send(3, "GET / HTTP/1.0rn", 16, MSG_DONTWAIT) = 16
9. send(3, "Host: www.google.comrn", 22, MSG_DONTWAIT) = 22
10.send(3, "rn", 2, MSG_DONTWAIT) = 2
11.poll([{fd=3, events=POLLIN|POLLPRI|POLLERR|POLLHUP}], 1, 0) = 0 (Timeout)
12.poll([{fd=3, events=POLLIN|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=3, revents=POLLIN}])
13.recv(3, "HTTP/1.0 200 OKrnDate: Tue, 13 A"..., 8192, MSG_DONTWAIT) = 4636
14.poll([{fd=3, events=POLLIN|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=3, revents=POLLIN}])
15.recv(3, "is) class=gb2>Blogs</a> <div cla"..., 8192, MSG_DONTWAIT) = 4290
16.poll([{fd=3, events=POLLIN|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=3, revents=POLLIN}])
17.recv(3, "", 8192, MSG_DONTWAIT) = 0
18.poll([{fd=3, events=POLLIN|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=3, revents=POLLIN}])
19.recv(3, "", 8192, MSG_DONTWAIT) = 0
20.close(3) = 0
Measuring Tape for
  Programmers
Measuring Tape for
     Programmers
Xdebug can provide LOTS of information

           http://xdebug.org/
Xdebug Profiler
•Generates cachegrind output
 ➡KCachegrind
 ➡webgrind
 ➡MacCallGrind
 ➡WinCacheGrind
 ➡Valgrind
webgrind
Live Demo Time

Contenu connexe

Dernier

UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationIES VE
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
RAG Patterns and Vector Search in Generative AI
RAG Patterns and Vector Search in Generative AIRAG Patterns and Vector Search in Generative AI
RAG Patterns and Vector Search in Generative AIUdaiappa Ramachandran
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureEric D. Schabell
 
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServicePicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServiceRenan Moreira de Oliveira
 
GenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncGenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncObject Automation
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UbiTrack UK
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataCloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataSafe Software
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioChristian Posta
 

Dernier (20)

UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
RAG Patterns and Vector Search in Generative AI
RAG Patterns and Vector Search in Generative AIRAG Patterns and Vector Search in Generative AI
RAG Patterns and Vector Search in Generative AI
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability Adventure
 
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServicePicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
 
GenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncGenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation Inc
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial DataCloud Revolution: Exploring the New Wave of Serverless Spatial Data
Cloud Revolution: Exploring the New Wave of Serverless Spatial Data
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and Istio
 

Anatomy of a PHP Request

  • 1. Anatomy of a PHP Request Joseph Scott 15 April 2010 UPHPU
  • 2. Lifespan .php Read Parse Compile Execute Output
  • 3. Lifespan .php Read Parse Compile Execute Output Apache / mod_php
  • 4. Lifespan .php Read Parse Compile Execute Output FastCGI Nginx
  • 5. Reading Open Read Close
  • 6. Reading Open Read Close Avoid co$tly services like NFS
  • 7. Parse hello.php <?php echo "Hello, World!n" Parse error: syntax error, unexpected $end, expecting ',' or ';' in /tmp/hello.php on line 3
  • 8. Tokens <?php echo $tokens = token_get_all( ' "Hello, World!";' ); foreach ( $tokens as $token ) {     if ( is_array( $token ) ) {         echo token_name( $token[0] ) . " ( {$token[2]} ) - {$token[1]}n";     } else {         echo "{$token}n";     } }
  • 9. Tokens T_OPEN_TAG ( 1 ) - <?php T_ECHO ( 1 ) - echo T_WHITESPACE ( 1 ) - T_CONSTANT_ENCAPSED_STRING ( 1 ) - "Hello, World!" ;
  • 10. Compile Branch analysis from position: 0 Return found filename: /tmp/hello.php function name: (null) number of ops: 3 compiled vars: none line # op fetch ext return operands ------------------------------------------------------------------------------- 2 0 ECHO 'Hello%2C+World%21%0A' 3 1 RETURN 1 2* ZEND_HANDLE_EXCEPTION
  • 11. Execute Deceptively Simple Term •Includes all sorts of activity •Database / remote requests
  • 12. Output •PHP can buffer output •Don’t forget about compression
  • 14. Making Things Faster .php Read Parse Compile Execute Output
  • 15. Making Things Faster .php Read Parse APC Compile Execute Output
  • 16. Making Things Faster .php Read Parse APC Compile Execute Output opcode cache
  • 17. The PHP Point of View opcodes VLD - Vulcan Logic Dumper
  • 18. Example PHP $html = file_get_contents( 'http://www.google.com/' ); $regex_pattern = '!<a href="[^>]*">(.*?)</a>!'; preg_match_all( $regex_pattern, $html, $matches );
  • 19. Example PHP - VLD Branch analysis from position: 0 Return found filename: /drive/home/joseph/vld/parse-links.php function name: (null) number of ops: 17 compiled vars: !0 = $html, !1 = $regex_pattern, !2 = $matches
  • 20. Example PHP - VLD line # op fetch ext return operands ------------------------------------------------------------------------------- 4 0 EXT_STMT 1 EXT_FCALL_BEGIN 2 SEND_VAL 'http%3A%2F%2Fwww.google.com%2F' 3 DO_FCALL 1 'file_get_contents' 4 EXT_FCALL_END 5 ASSIGN !0, $0 5 6 EXT_STMT 7 ASSIGN !1, '%21%3Ca+href%3D%22%5B%5E%3E %5D%2A%22%3E%28.%2A%3F%29%3C%2Fa%3E%21' 6 8 EXT_STMT 9 EXT_FCALL_BEGIN 10 SEND_VAR !1 11 SEND_VAR !0 12 SEND_REF !2 13 DO_FCALL 3 'preg_match_all' 14 EXT_FCALL_END 7 15 RETURN 1 16* ZEND_HANDLE_EXCEPTION
  • 21. PHP - strace • System calls, expect LOTS of data • strace -o out php parse-links.php
  • 22. PHP - strace • System calls, expect LOTS of data • strace -o out php parse-links.php 1,786 lines of output!
  • 23. 1. PHP - strace socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3 2. fcntl64(3, F_GETFL) = 0x2 (flags O_RDWR) 3. fcntl64(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0 4. connect(3, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("64.233.169.99")}, 16) = -1 EINPROGRESS (Operation now in progress) 5. poll([{fd=3, events=POLLIN|POLLOUT|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=3, revents=POLLOUT}]) 6. getsockopt(3, SOL_SOCKET, SO_ERROR, [0], [4]) = 0 7. fcntl64(3, F_SETFL, O_RDWR) = 0 8. send(3, "GET / HTTP/1.0rn", 16, MSG_DONTWAIT) = 16 9. send(3, "Host: www.google.comrn", 22, MSG_DONTWAIT) = 22 10.send(3, "rn", 2, MSG_DONTWAIT) = 2 11.poll([{fd=3, events=POLLIN|POLLPRI|POLLERR|POLLHUP}], 1, 0) = 0 (Timeout) 12.poll([{fd=3, events=POLLIN|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=3, revents=POLLIN}]) 13.recv(3, "HTTP/1.0 200 OKrnDate: Tue, 13 A"..., 8192, MSG_DONTWAIT) = 4636 14.poll([{fd=3, events=POLLIN|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=3, revents=POLLIN}]) 15.recv(3, "is) class=gb2>Blogs</a> <div cla"..., 8192, MSG_DONTWAIT) = 4290 16.poll([{fd=3, events=POLLIN|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=3, revents=POLLIN}]) 17.recv(3, "", 8192, MSG_DONTWAIT) = 0 18.poll([{fd=3, events=POLLIN|POLLERR|POLLHUP}], 1, 60000) = 1 ([{fd=3, revents=POLLIN}]) 19.recv(3, "", 8192, MSG_DONTWAIT) = 0 20.close(3) = 0
  • 24. Measuring Tape for Programmers
  • 25. Measuring Tape for Programmers Xdebug can provide LOTS of information http://xdebug.org/
  • 26. Xdebug Profiler •Generates cachegrind output ➡KCachegrind ➡webgrind ➡MacCallGrind ➡WinCacheGrind ➡Valgrind

Notes de l'éditeur