SlideShare a Scribd company logo
1 of 28
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

More Related Content

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Recently uploaded (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

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

Editor's Notes