Book Image

Mastering The Faster Web with PHP, MySQL, and JavaScript

By : Andrew Caya
Book Image

Mastering The Faster Web with PHP, MySQL, and JavaScript

By: Andrew Caya

Overview of this book

This book will get you started with the latest benchmarking, profiling and monitoring tools for PHP, MySQL and JavaScript using Docker-based technologies. From optimizing PHP 7 code to learning asynchronous programming, from implementing Modern SQL solutions to discovering Functional JavaScript techniques, this book covers all the latest developments in Faster Web technologies. You will not only learn to determine the best optimization strategies, but also how to implement them. Along the way, you will learn how to profile your PHP scripts with Blackfire.io, monitor your Web applications, measure database performance, optimize SQL queries, explore Functional JavaScript, boost Web server performance in general and optimize applications when there is nothing left to optimize by going beyond performance. After reading this book, you will know how to boost the performance of any Web application and make it part of what has come to be known as the Faster Web.
Table of Contents (19 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Foreword
Contributors
Preface
Free Chapter
1
Faster Web – Getting Started
6
Querying a Modern SQL Database Efficiently
Index

Preface

The Faster Web can be defined as a series of qualities to be developed in all spheres of web technology in order to speed up any transaction between a client and a server. It also includes the principles behind UI design that can influence a user's perception of speed. Thus, understanding the Faster Web involves understanding the notions of performance, efficiency and perceived performance, and discovering most of the new underlying web technologies that make up what the internet has become today.

Who this book is for

Any web developer, system administrator or web enthusiast who wishes to understand the Faster Web better. Basic knowledge of Docker container technology is a plus.

What this book covers

Chapter 1, Faster Web – Getting Started, defines what is the Faster Web by trying to better understand the formal aspects of it and sets out to understand how to measure performance and determine if a website or Web application is part of the Faster Web or not.

Chapter 2, Continuous Profiling and Monitoring, aims to help the reader learn how to install and configure profiling and monitoring tools that will help them easily optimize PHP code in a continuous integration (CI) and a continuous deployment (CD) environment.

Chapter 3, Harnessing the Power of PHP 7 Data Structures and Functions, gets the reader to learn how to harness PHP 7's performance boosts through most of its key optimizations. It also helps them explore how better understanding data structures and data types, and using simplified functions can help a PHP application's global performance along its critical execution path. In addition, it covers how it is best to avoid using inefficient structures, like most dynamic ones, in our PHP code, and how some functional techniques can be of immediate help when optimizing PHP code.

Chapter 4, Envisioning the Future with Asynchronous PHP, outlines how to cope with input and output (I/O) poor latency by learning about generators and asynchronous non-blocking code, multithreading with the POSIX Threads (pthreads) library and multitasking with the ReactPHP library.

 Chapter 5, Measuring and Optimizing Database Performance, shows how to measure database performance, ranging from simple measurement techniques to advanced benchmarking tools.

Chapter 6, Querying Efficiently a Modern SQL Database, explains how to use Modern SQL techniques in order to optimize complex SQL queries.

Chapter 7, JavaScript and Danger-Driven Development, covers a few of JavaScript's best and worst parts, especially those that pertain to code efficiency and overall performance, and how a developer should always write safe, reliable and highly efficient JavaScript code, mostly by avoiding “danger-driven development”.

Chapter 8, Functional JavaScript, features how JavaScript is becoming more and more a functional language and how this programming paradigm will be a vector for performance in the near future by taking a quick look at upcoming language features that will help improve performance of JavaScript applications.

Chapter 9, Boosting a Web Server’s Performance, looks at what the HTTP/2 protocol is all about and how the SPDY project made it possible, how PHP-FPM and OPcache can help you boost the performance of your PHP scripts, how to use ESI technology by setting up a Varnish Cache server, how to use client-side caching and how other Faster Web tools can help you boost a web server's overall performance.

Chapter 10, Going Beyond Performance, shows how, when everything seems to have been fully optimized, we can still go beyond performance by better understanding the principles behind UI design when it comes to the perception of performance.

To get the most out of this book

In order to run the source code included in this book, we recommend that you start by installing Docker on your computer (https://docs.docker.com/engine/installation/). Docker is a software container platform that allows you to easily connect to your computer’s devices in an isolated and sophisticated chroot-like environment. Unlike virtual machines, containers do not come bundled with full operating systems, but only come with the required binaries in order to run some software. You can install Docker on Windows, Mac, or Linux. It should be noted, however, that some features, such as full-featured networking, are still not available when running Docker on macOS (https://docs.docker.com/docker-for-mac/networking/#known-limitations-use-cases-and-workarounds).

The main Docker image that we will be using throughout this book is Linux for PHP 8.1 (https://linuxforphp.net/) with a non thread-safe version of PHP 7.1.16 and MariaDB (MySQL) 10.2.8 (asclinux/linuxforphp-8.1:7.1.16-nts). To start the main container, enter the following command:

# docker run --rm -it \
> -v ${PWD}/:/srv/fasterweb \
> -p 8181:80 \
> asclinux/linuxforphp-8.1:7.1.16-nts \
> /bin/bash

If you prefer using multithreading technologies while optimizing your code, you can do so by running a thread-safe version of Linux for PHP (asclinux/linuxforphp-8.1:7.0.29-zts).

Moreover, you should docker commit any changes you make to the container and create new images of your container that you can docker run at a later time. If you are not familiar with the Docker command line and its run command, find the documentation at https://docs.docker.com/engine/reference/run/.

Finally, the three following commands must be run inside the Linux for PHP container whenever you start an original Linux for PHP image and wish to start working with most of the code examples contained in this book:

# /etc/init.d/mysql start
# /etc/init.d/php-fpm start
# /etc/init.d/httpd start

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com
  2. Select the SUPPORT tab
  3. Click on Code Downloads & Errata
  4. Enter the name of the book in the Search box and follow the onscreen instructions

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript. In case there's an update to the code, it will be updated on the existing GitHub repository.

All the code examples given in this book can be found, within the code repository, in a folder named according to the chapter's number. Thus, it is expected that you change your working directory at the beginning of each chapter in order to run the code examples given within. Thus, for chapter 1, you are expected to enter, on the container's CLI, the following commands:

# mv /srv/www /srv/www.OLD
# ln -s /srv/fasterweb/chapter_1 /srv/www

And, for the next chapter, you are expected to enter these commands:

# rm /srv/www
# ln -s /srv/fasterweb/chapter_2 /srv/www

And, so on for the following chapters.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Whenever possible, the developer should always prefer using const over let or var."

A block of code is set as follows:

function myJS()
{
    function add(n1, n2)
    {
        let number1 = Number(n1);
        let number2 = Number(n2);

        return number1 + number2;
    }

}

Any command-line input or output is written as follows:

# php parallel-download.php

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "If you scroll towards the end of the page, you should now see an xdebug section."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.