├── LICENSE ├── README.md ├── bash_history_container.txt ├── bash_history_host.txt ├── chapter_1 ├── cachegrind.out.412 ├── index.html ├── index.php ├── phpinfo.php ├── sakila-db.tar.gz ├── siege-4.0.2.tar.gz ├── urlfile_1.txt └── urlfile_2.txt ├── chapter_10 ├── chap10_slow_after.html ├── chap10_slow_before.php ├── index.html ├── index.php └── phpinfo.php ├── chapter_2 ├── .blackfire.travis.ini ├── .blackfire.yml ├── .travis.yml ├── LICENSE ├── agent ├── bash_history_container.txt ├── bash_history_host.txt ├── blackfire ├── blackfire-agent ├── blackfire.so ├── chap2post.php ├── chap2pre.php ├── fakedir │ ├── fakedir2 │ │ ├── file4.txt │ │ ├── file5.txt │ │ └── file6.txt │ ├── fakedir3 │ │ ├── file7.txt │ │ ├── file8.txt │ │ └── file9.txt │ ├── file1.txt │ ├── file2.txt │ └── file3.txt ├── index.html ├── index.php ├── php.ini ├── phpinfo.php ├── sakila-db │ ├── sakila-data.sql │ ├── sakila-schema.sql │ └── sakila.mwb └── telegraf.conf ├── chapter_3 ├── chap3_dynamic_1.php ├── chap3_dynamic_2.php ├── chap3_dynamic_3.php ├── chap3_dynamic_4.php ├── chap3_immutable_arrays.php ├── chap3_memoization_after.php ├── chap3_memoization_before.php ├── chap3_references.php ├── chap3_strict_typing.php ├── chap3_strict_typing_modified.php ├── chap3_string_interpolation.php ├── chap3_variables.php ├── index.html ├── index.php └── phpinfo.php ├── chapter_4 ├── chap4_IO_blocking.php ├── chap4_IO_non_blocking.php ├── chap4_async_race.php ├── chap4_pthreads.php ├── index.html ├── index.php ├── phpinfo.php └── react │ ├── .gitignore │ ├── composer.json │ ├── composer.lock │ ├── composer.phar │ └── examples │ ├── child-child.php │ ├── child-process.php │ ├── dns-resolver.php │ ├── echo-server.php │ ├── http-client.php │ ├── next-tick.php │ ├── parallel-download.php │ ├── pump-tons-of-data.php │ ├── scalability.php │ ├── tcp-chat.php │ ├── test-close-conn.php │ ├── test-memory-http.php │ └── test-memory.php ├── chapter_5 ├── dbt2-0.37.tar.gz ├── index.html ├── index.php ├── phpinfo.php └── sysbench-0.4.12.14.tar.gz ├── chapter_6 ├── employees.sql ├── index.html ├── index.php ├── json_example.sql ├── phpinfo.php └── sales.sql ├── chapter_7 ├── chap7_js_loops_1.html ├── chap7_js_loops_2.html ├── chap7_js_variables_1.html ├── chap7_js_variables_2.html ├── index.html ├── index.php └── phpinfo.php ├── chapter_8 ├── chap8_js_const_1.html ├── chap8_js_performance_1.html ├── chap8_js_performance_2.html ├── index.html ├── index.php └── phpinfo.php ├── chapter_9 ├── index.html ├── index.php └── phpinfo.php ├── index.html ├── index.php └── phpinfo.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Mastering The Faster Web with PHP, MySQL, and JavaScript 5 | 6 | Mastering The Faster Web with PHP, MySQL, and JavaScript 7 | 8 | This is the code repository for [Mastering The Faster Web with PHP, MySQL, and JavaScript](https://www.packtpub.com/web-development/mastering-faster-web-php-mysql-and-javascript?utm_source=github&utm_medium=repository&utm_campaign=9781788392211), published by Packt. 9 | 10 | **Develop state-of-the-art web applications using the latest web technologies** 11 | 12 | ## What is this book about? 13 | 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. 14 | 15 | This book covers the following exciting features: 16 | * Install, confgure, and use profling and benchmarking testing tools 17 | * Understand how to recognize optimizable data structures and functions to effectively optimize a PHP7 application 18 | * Diagnose bad SQL query performance and discover ways to optimize it 19 | * Grasp modern SQL techniques to optimize complex SQL queries 20 | * Identify and simplify overly complex JavaScript code 21 | 22 | If you feel this book is for you, get your [copy](https://www.amazon.com/dp/1788392213) today! 23 | 24 | https://www.packtpub.com/ 26 | 27 | 28 | ## Instructions and Navigations 29 | All of the code is organized into folders. For example, Chapter02. 30 | 31 | The code will look like the following: 32 | ``` 33 | function myJS() 34 | { 35 | function add(n1, n2) 36 | { 37 | let number1 = Number(n1); 38 | let number2 = Number(n2); 39 | 40 | return number1 + number2; 41 | } 42 | 43 | } 44 | ``` 45 | 46 | **Following is what you need for this book:** 47 | The audience for this book would be PHP developers who have some basic knowledge of PHP programming and Web technologies. JavaScript programming knowledge is not necessary. 48 | 49 | With the following software and hardware list you can run all code files present in the book (Chapter 1-10). 50 | 51 | ### Software and Hardware List 52 | 53 | | Chapter | Software required | OS required | 54 | | -------- | ------------------------------------| -----------------------------------| 55 | | 1-10 | Docker, PHP | Windows | 56 | 57 | 58 | 59 | ### Related products 60 | * Mastering PHP 7 [[Packt]](https://www.packtpub.com/application-development/mastering-php-7?utm_source=github&utm_medium=repository&utm_campaign=9781785882814) [[Amazon]](https://www.amazon.com/dp/1785882813) 61 | 62 | * Building RESTful Web Services with PHP 7 [[Packt]](https://www.packtpub.com/application-development/building-restful-web-services-php-7?utm_source=github&utm_medium=repository&utm_campaign=9781787127746) [[Amazon]](https://www.amazon.com/dp/1787127745) 63 | 64 | ## Get to Know the Author 65 | **Andrew Caya** started programming computers in GW-BASIC and QBASIC in the early 90s. Before becoming a PHP developer almost 10 years ago, he did some software development in C, C++, and Perl. He is now a Zend Certified PHP Engineer and a Zend Certified Architect. He is also the creator of Linux for PHP, the lead developer of a popular Joomla extension and a contributor to many open source projects. 66 | 67 | He is currently CEO, CTO and Founder of Foreach Code Factory, an instructor at Concordia University, an author and a technical reviewer for Packt Publishing, and a loving husband and father. 68 | 69 | 70 | ### Suggestions and Feedback 71 | [Click here](https://docs.google.com/forms/d/e/1FAIpQLSdy7dATC6QmEL81FIUuymZ0Wy9vH1jHkvpY57OiMeKGqib_Ow/viewform) if you have any feedback or suggestions. 72 | ### Download a free PDF 73 | 74 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
75 |

https://packt.link/free-ebook/9781788392211

-------------------------------------------------------------------------------- /bash_history_container.txt: -------------------------------------------------------------------------------- 1 | ######################################################## 2 | ### Chapter 1 on asclinux/linuxforphp-8.1:7.1.16-nts ### 3 | ######################################################## 4 | mv /srv/www /srv/www.OLD 5 | ln -s /srv/fasterweb/chapter_1 /srv/www 6 | cd /srv/www 7 | /etc/init.d/mysql start 8 | /etc/init.d/php-fpm start 9 | /etc/init.d/httpd start 10 | touch /srv/www/index.php 11 | echo "> /srv/www/index.php 12 | cat /srv/www/index.php 13 | ab -k -l -c 2 -n 2000 localhost/index.html 14 | ab -k -l -c 2 -n 2000 localhost/index.php 15 | wget -O siege-4.0.2.tar.gz http://download.joedog.org/siege/siege-4.0.2.tar.gz 16 | tar -xzvf siege-4.0.2.tar.gz 17 | cd siege-4.0.2 18 | ./configure 19 | make 20 | make install 21 | man siege 22 | siege -b -c 3000 -r 100 localhost/index.html 23 | cd /srv/www 24 | touch urlfile.txt 25 | for i in {1..4}; do echo "http://localhost/index.html" >> urlfile.txt ; done 26 | bombardment urlfile.txt 10 100 4 0 27 | echo "http://localhost/index.php" > urlfile.txt 28 | for i in {1..3}; do echo "http://localhost/index.php" >> urlfile.txt ; done 29 | bombardment urlfile.txt 10 100 4 0 30 | pecl install xdebug 31 | echo -e "zend_extension=$( php -i | grep extensions | awk '{print $3}' )/xdebug.so\n" >> /etc/php.ini 32 | echo -e "xdebug.remote_enable = 1\n" >> /etc/php.ini 33 | echo -e "xdebug.remote_enable_trigger = 1\n" >> /etc/php.ini 34 | echo -e "xdebug.remote_connect_back = 1\n" >> /etc/php.ini 35 | echo -e "xdebug.idekey = PHPSTORM\n" >> /etc/php.ini 36 | echo -e "xdebug.profiler_enable = 1\n" >> /etc/php.ini 37 | echo -e "xdebug.profiler_enable_trigger = 1\n" >> /etc/php.ini 38 | /etc/init.d/php-fpm restart 39 | tail -50 /etc/php.ini 40 | rm /srv/www 41 | ln -s /srv/fasterweb /srv/www 42 | cd /srv/www 43 | ifconfig 44 | ls -lsa /tmp 45 | cat /tmp/cachegrind.out.411 46 | rm /srv/www 47 | ln -s /srv/fasterweb/chapter_1 /srv/www 48 | cd /srv/www 49 | wget -O sakila-db.tar.gz https://downloads.mysql.com/docs/sakila-db.tar.gz 50 | tar -xzvf sakila-db.tar.gz 51 | mysql -uroot < sakila-db/sakila-schema.sql 52 | mysql -uroot < sakila-db/sakila-data.sql 53 | mysqlslap --user=root --host=localhost --concurrency=20--number-of-queries=1000 --create-schema=sakila --query="SELECT * FROM film;" --delimiter=";" --verbose --iterations=2 --debug-info 54 | mysqlslap --user=root --host=localhost --concurrency=50 --number-of-queries=1000 --create-schema=sakila --query="SELECT * FROM film;" --delimiter=";" --verbose --iterations=2 --debug-info 55 | ######################################################## 56 | ### Chapter 2 on asclinux/linuxforphp-8.1:7.1.16-nts ### 57 | ######################################################## 58 | rm /srv/www 59 | ln -s /srv/fasterweb/chapter_2 /srv/www 60 | cd /srv/www 61 | wget -O blackfire-agent https://packages.blackfire.io/binaries/blackfire-agent/1.17.0/blackfire-agent-linux_static_amd64 62 | mv blackfire-agent /usr/local/bin/ 63 | chmod +x /usr/local/bin/blackfire-agent 64 | mkdir -p /etc/blackfire 65 | cp /srv/www/agent /etc/blackfire/ 66 | mkdir -p /var/run/blackfire 67 | touch /var/run/blackfire/agent.sock 68 | blackfire-agent -register 69 | blackfire-agent start & 70 | wget -O blackfire https://packages.blackfire.io/binaries/blackfire-agent/1.17.0/blackfire-cli-linux_static_amd64 71 | mv blackfire /usr/local/bin/ 72 | chmod +x /usr/local/bin/blackfire 73 | blackfire config 74 | wget -O blackfire.so https://packages.blackfire.io/binaries/blackfire-php/1.20.0/blackfire-php-linux_amd64-php-71.so 75 | php -i | grep 'extension_dir' 76 | mv blackfire.so $( php -i | grep extensions | awk '{print $3}' ) 77 | sed -i 's/zend_extension=\/usr\/lib\/php\/extensions\/no-debug-non-zts-20160303\/xdebug.so/;zend_extension=\/usr\/lib\/php\/extensions\/no-debug-non-zts-20160303\/xdebug.so/' /etc/php.ini 78 | sed -i 's/^xdebug/;xdebug/' /etc/php.ini 79 | cat >>/etc/php.ini << 'EOF' 80 | 81 | [blackfire] 82 | extension=blackfire.so 83 | ; On Windows use the following configuration: 84 | ; extension=php_blackfire.dll 85 | 86 | ; Sets the socket where the agent is listening. 87 | ; Possible value can be a unix socket or a TCP address. 88 | ; Defaults to unix:///var/run/blackfire/agent.sock on Linux, 89 | ; unix:///usr/local/var/run/blackfire-agent.sock on MacOSX, 90 | ; and to tcp://127.0.0.1:8307 on Windows. 91 | ;blackfire.agent_socket = unix:///var/run/blackfire/agent.sock 92 | 93 | blackfire.agent_timeout = 0.25 94 | 95 | ; Log verbosity level (4: debug, 3: info, 2: warning, 1: error) 96 | ;blackfire.log_level = 1 97 | 98 | ; Log file (STDERR by default) 99 | ;blackfire.log_file = /tmp/blackfire.log 100 | 101 | ;blackfire.server_id = 102 | 103 | ;blackfire.server_token = 104 | EOF 105 | /etc/init.d/php-fpm restart 106 | blackfire curl http://localhost/index.php 107 | gem install travis 108 | travis encrypt-file /srv/www/.blackfire.travis.ini -r [your_Github_repository_name_here] 109 | sed -i 's/#Include \/etc\/httpd\/extra\/httpd-info.conf/Include \/etc\/httpd\/extra\/httpd-info.conf/' /etc/httpd/httpd.conf 110 | sed -i 's/Require ip 127/Require ip 172/' /etc/httpd/extra/httpd-info.conf 111 | /etc/init.d/httpd restart 112 | siege -b -c 3000 -r 100 localhost/chap2pre.php 113 | siege -b -c 3000 -r 100 localhost/chap2post.php 114 | ######################################################## 115 | ### Chapter 3 on asclinux/linuxforphp-8.1:7.1.16-nts ### 116 | ######################################################## 117 | rm /srv/www 118 | ln -s /srv/fasterweb/chapter_3 /srv/www 119 | cd /srv/www 120 | php chap3_strict_typing.php 121 | php chap3_strict_typing_modified.php 122 | php chap3_immutable_arrays.php 123 | php chap3_variables.php 124 | php chap3_string_interpolation.php 125 | php chap3_references.php 126 | php chap3_dynamic_1.php 127 | php chap3_dynamic_2.php 128 | php chap3_dynamic_3.php 129 | php chap3_dynamic_4.php 130 | php chap3_memoization_before.php 131 | php chap3_memoization_after.php 132 | ######################################################## 133 | ### Chapter 4 on asclinux/linuxforphp-8.1:7.1.16-nts ### 134 | ######################################################## 135 | rm /srv/www 136 | ln -s /srv/fasterweb/chapter_4 /srv/www 137 | cd /srv/www 138 | php chap4_IO_blocking.php 139 | php chap4_IO_non_blocking.php 140 | php chap4_async_race.php 141 | ######################################################## 142 | ### Chapter 4 on asclinux/linuxforphp-8.1:7.0.29-zts ### 143 | ######################################################## 144 | mv /srv/www /srv/www.OLD 145 | ln -s /srv/fasterweb/chapter_4 /srv/www 146 | cd /srv/www 147 | pecl install pthreads 148 | echo "extension=pthreads.so" >> /etc/php.ini 149 | php -v 150 | php -i 151 | php chap4_pthreads.php 152 | cd /srv/www/react 153 | php composer.phar self-update 154 | php composer.phar install 155 | cd examples 156 | php parallel-download.php 157 | php tcp-chat.php 158 | telnet localhost 4000 159 | php scalability.php 160 | wget -nv -O - http://localhost:8000 161 | wget -nv -O - http://localhost:8001 162 | wget -nv -O - http://localhost:8002 163 | wget -nv -O - http://localhost:8003 164 | wget -nv -O - http://localhost:8004 165 | wget -nv -O - http://localhost:8005 166 | wget -nv -O - http://localhost:8006 167 | wget -nv -O - http://localhost:8007 168 | wget -nv -O - http://localhost:8008 169 | wget -nv -O - http://localhost:8009 170 | ######################################################## 171 | ### Chapter 5 on asclinux/linuxforphp-8.1:7.1.16-nts ### 172 | ######################################################## 173 | rm /srv/www 174 | ln -s /srv/fasterweb/chapter_5 /srv/www 175 | cd /srv/www 176 | mysql -uroot 177 | # MariaDB > USE sakila; 178 | # MariaDB > SELECT * FROM actor WHERE first_name = ‘AL’; 179 | # MariaDB > EXPLAIN SELECT * FROM actor WHERE first_name= ‘AL’; 180 | # MariaDB > CREATE INDEX idx_first_name ON actor(first_name); 181 | # MariaDB > EXPLAIN SELECT first_name FROM actor WHERE first_name = ‘AL’; 182 | # MariaDB > exit 183 | mysqlslap --user=root --host=localhost --concurrency=1000 --number-of-queries=10000 --create-schema=sakila --query="SELECT * FROM actor WHERE first_name = 'AL';" --delimiter=";" --verbose --iterations=2 --debug-info 184 | mysqlslap --user=root --host=localhost --concurrency=1000 --number-of-queries=10000 --create-schema=sakila --query="SELECT first_name FROM actor WHERE first_name = 'AL';" --delimiter=";" --verbose --iterations=2 --debug-info 185 | sed -i '/myisam_sort_buffer_size =/a performance_schema = ON' /etc/mysql/my.cnf 186 | sed -i '/performance_schema =/a performance-schema-instrument = "stage/%=ON"' /etc/mysql/my.cnf 187 | sed -i '/performance-schema-instrument =/a performance-schema-consumer-events-stages-current = ON' /etc/mysql/my.cnf 188 | sed -i '/performance-schema-consumer-events-stages-current =/a performance-schema-consumer-events-stages-history = ON' /etc/mysql/my.cnf 189 | sed -i '/performance-schema-consumer-events-stages-history =/a performance-schema-consumer-events-stages-history-long = ON' /etc/mysql/my.cnf 190 | /etc/init.d/mysql restart 191 | mysql -uroot 192 | # MariaDB > USE performance_schema; 193 | # MariaDB > UPDATE setup_instruments SET ENABLED = 'YES', TIMED = 'YES'; 194 | # MariaDB > UPDATE setup_consumers SET ENABLED = 'YES'; 195 | # MariaDB > SHOW VARIABLES LIKE 'performance_schema'; 196 | # MariaDB > SELECT film.film_id 197 | # > FROM film 198 | # > WHERE film.rating = 'G' 199 | # > AND film.film_id NOT IN ( 200 | # > SELECT film.film_id 201 | # > FROM rental 202 | # > LEFT JOIN inventory ON rental.inventory_id = inventory.inventory_id 203 | # > LEFT JOIN film ON inventory.film_id = film.film_id 204 | # > ); 205 | # MariaDB > EXPLAIN SELECT film.film_id 206 | # > FROM film 207 | # > WHERE film.rating = 'G' 208 | # > AND film.film_id NOT IN ( 209 | # > SELECT film.film_id 210 | # > FROM rental 211 | # > LEFT JOIN inventory ON rental.inventory_id = inventory.inventory_id 212 | # > LEFT JOIN film ON inventory.film_id = film.film_id 213 | # > ); 214 | # MariaDB > SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT 215 | # > FROM performance_schema.events_statements_history_long WHERE SQL_TEXT like '%NOT IN%'; 216 | # MariaDB > SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT/1000000000000,6) AS Duration 217 | # > FROM performance_schema.events_stages_history_long WHERE NESTING_EVENT_ID=43; 218 | # MariaDB > SELECT film.film_id 219 | # > FROM rental 220 | # > INNER JOIN inventory ON rental.inventory_id = inventory.inventory_id 221 | # > RIGHT JOIN film ON inventory.film_id = film.film_id 222 | # > WHERE film.rating = 'G' 223 | # > AND rental.rental_id IS NULL 224 | # > GROUP BY film.film_id; 225 | # MariaDB > EXPLAIN SELECT film.film_id 226 | # > FROM rental 227 | # > INNER JOIN inventory ON rental.inventory_id = inventory.inventory_id 228 | # > RIGHT JOIN film ON inventory.film_id = film.film_id 229 | # > WHERE film.rating = 'G' 230 | # > AND rental.rental_id IS NULL 231 | # > GROUP BY film.film_id; 232 | # MariaDB > SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT 233 | # > FROM performance_schema.events_statements_history_long WHERE SQL_TEXT like '%GROUP BY%'; 234 | # MariaDB > SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT/1000000000000,6) AS Duration 235 | # > FROM performance_schema.events_stages_history_long WHERE NESTING_EVENT_ID=22717; 236 | # MariaDB > exit 237 | cd /srv/www 238 | wget -O dbt2-0.37.tar.gz https://master.dl.sourceforge.net/project/osdldbt/dbt2/0.37/dbt2-0.37.tar.gz 239 | tar -xvf dbt2-0.37.tar.gz 240 | cd dbt2-0.37 241 | ./configure --with-mysql 242 | make 243 | make install 244 | cpan install Statistics::Descriptive 245 | mkdir -p /srv/mysql/dbt2-tmp-data/dbt2-w3 246 | ./src/datagen -w 3 -d /srv/mysql/dbt2-tmp-data/dbt2-w3 --mysql 247 | vi scripts/mysql/mysql_load_db.sh 248 | ./scripts/mysql/mysql_load_db.sh -d dbt2 -f /srv/mysql/dbt2-tmp-data/dbt2-w3 -s /run/mysqld/mysqld.sock -u root 249 | ./scripts/run_mysql.sh -n dbt2 -o /run/mysqld/mysqld.sock -u root -w 3 -t 300 -c 20 250 | cd /srv/www 251 | wget -O sysbench-0.4.12.14.tar.gz https://downloads.mysql.com/source/sysbench-0.4.12.14.tar.gz 252 | tar -xvf sysbench-0.4.12.14.tar.gz 253 | cd sysbench-0.4.12.14 254 | ./configure 255 | make 256 | make install 257 | sysbench --test=oltp --oltp-table-size=1000000 --mysql-db=test --mysql-user=root prepare 258 | sysbench --test=oltp --oltp-table-size=1000000 --mysql-db=test --mysql-user=root --max-time=60 --oltp-read-only=on --max-requests=0 --num-threads=8 run 259 | ######################################################## 260 | ### Chapter 6 on asclinux/linuxforphp-8.1:7.1.16-nts ### 261 | ######################################################## 262 | rm /srv/www 263 | ln -s /srv/fasterweb/chapter_6 /srv/www 264 | cd /srv 265 | wget -O phpMyAdmin-4.7.7-all-languages.zip https://files.phpmyadmin.net/phpMyAdmin/4.7.7/phpMyAdmin-4.7.7-all-languages.zip 266 | unzip phpMyAdmin-4.7.7-all-languages.zip 267 | cp phpMyAdmin-4.7.7-all-languages/config.sample.inc.php phpMyAdmin-4.7.7-all-languages/config.inc.php 268 | sed -i "s/AllowNoPassword'] = false/AllowNoPassword'] = true/" phpMyAdmin-4.7.7-all-languages/config.inc.php 269 | cd fasterweb/chapter_6 270 | ln -s ../../phpMyAdmin-4.7.7-all-languages ./phpmyadmin 271 | mysql -uroot test < /srv/www/employees.sql 272 | mysql -uroot 273 | # MariaDB > SELECT CONCAT_WS('->', t1.last_name, t2.last_name, t3.last_name, t4.last_name, t5.last_name, t6.last_name) AS path 274 | # > FROM employees AS t1 275 | # > RIGHT JOIN employees AS t2 ON t2.superior = t1.id 276 | # > RIGHT JOIN employees AS t3 ON t3.superior = t2.id 277 | # > RIGHT JOIN employees AS t4 ON t4.superior = t3.id 278 | # > RIGHT JOIN employees AS t5 ON t5.superior = t4.id 279 | # > RIGHT JOIN employees AS t6 ON t6.superior = t5.id 280 | # > WHERE t1.superior IS NULL 281 | # > ORDER BY path; 282 | # MariaDB > SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT 283 | # > FROM performance_schema.events_statements_history_long WHERE SQL_TEXT like '%RIGHT JOIN%'; 284 | # MariaDB > SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT/1000000000000,6) AS Duration 285 | # > FROM performance_schema.events_stages_history_long WHERE NESTING_EVENT_ID=126; 286 | # MariaDB > WITH RECURSIVE hierarchy_list AS ( 287 | # > SELECT id, superior, CONVERT(last_name, CHAR(100)) AS path 288 | # > FROM employees 289 | # > WHERE superior IS NULL 290 | # > UNION ALL 291 | # > SELECT child.id, child.superior, CONVERT(CONCAT(parent.path, '->', child.last_name), CHAR(100)) AS path 292 | # > FROM employees AS child 293 | # > INNER JOIN hierarchy_list AS parent ON (child.superior = parent.id) 294 | # > ) 295 | # > SELECT path 296 | # > FROM hierarchy_list 297 | # > ORDER BY path; 298 | # MariaDB > SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT 299 | # > FROM performance_schema.events_statements_history_long WHERE SQL_TEXT like '%WITH RECURSIVE%'; 300 | # MariaDB > SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT/1000000000000,6) AS Duration 301 | # > FROM performance_schema.events_stages_history_long WHERE NESTING_EVENT_ID=259; 302 | # MariaDB > SELECT id, COUNT(*) AS Total, COUNT(CASE WHEN superior IS NOT NULL THEN id END) AS 'Number of superiors' 303 | # > FROM employees 304 | # > WHERE id = 2; 305 | # MariaDB > SELECT DISTINCT superior AS manager_id, (SELECT last_name FROM employees WHERE id = manager_id) AS last_name, SUM(salary) OVER(PARTITION BY superior) AS 'payroll per manager' 306 | # > FROM employees 307 | # > WHERE superior IS NOT NULL 308 | # > ORDER BY superior; 309 | # MariaDB > SELECT id, last_name, salary, superior AS manager_id, (SELECT last_name FROM employees WHERE id = manager_id) AS manager_last_name, SUM(salary) OVER(PARTITION BY superior ORDER BY manager_last_name, salary DESC, id) AS payroll_per_manager 310 | # > FROM employees 311 | # > WHERE superior IS NOT NULL 312 | # > ORDER BY manager_last_name, salary DESC, id; 313 | # MariaDB > SELECT id, last_name, salary, superior AS manager_id, SUM(salary) OVER(PARTITION BY superior ORDER BY manager_id, salary DESC, id) AS payroll_per_manager 314 | # > FROM employees 315 | # > WHERE superior IS NOT NULL 316 | # > ORDER BY manager_id, salary DESC, id; 317 | # MariaDB > SELECT superior AS manager_id, SUM(salary) 318 | # > FROM employees 319 | # > WHERE superior IS NOT NULL 320 | # > GROUP BY manager_id, id, salary; 321 | # MariaDB > SELECT superior AS manager_id, SUM(salary) 322 | # > FROM employees 323 | # > WHERE superior IS NOT NULL 324 | # > GROUP BY manager_id, salary WITH ROLLUP; 325 | # MariaDB > exit 326 | mysql -uroot test < /srv/www/json_example.sql 327 | mysql -uroot 328 | # MariaDB > SELECT id, 329 | # > JSON_VALUE(json, "$.name") AS name, 330 | # > JSON_VALUE(json, "$.roles[0]") AS main_role, 331 | # > JSON_VALUE(json, "$.active") AS active 332 | # > FROM json_example 333 | # > WHERE id = 1; 334 | # MariaDB > INSERT INTO `json_example` (`id`, `json`) VALUES (NULL, 'test'); 335 | # MariaDB > exit 336 | /etc/init.d/postgresql start 337 | cd /srv 338 | wget --no-check-certificate -O phpPgAdmin-5.1.tar.gz https://superb-sea2.dl.sourceforge.net/project/phppgadmin/phpPgAdmin%20%5Bstable%5D/phpPgAdmin-5.1/phpPgAdmin-5.1.tar.gz 339 | tar -xvf phpPgAdmin-5.1.tar.gz 340 | sed -i "s/extra_login_security'] = true/extra_login_security'] = false/" phpPgAdmin-5.1/conf/config.inc.php 341 | cd fasterweb/chapter_6 342 | ln -s ../../phpPgAdmin-5.1 ./phppgadmin 343 | cd /srv/www 344 | su postgres 345 | psql test < sales.sql 346 | psql 347 | postgres=# SELECT 348 | # SUM(total) as total, 349 | # SUM(total) FILTER(WHERE closed IS TRUE) as transaction_complete, 350 | # year 351 | # FROM 352 | # sales 353 | # GROUP BY 354 | # year; 355 | postgres=# SELECT 356 | # film.id, 357 | # film.title, 358 | # actor_bio.name, 359 | # actor_bio.biography 360 | # FROM film, 361 | # LATERAL (SELECT 362 | # actor.name 363 | # actor.biography, 364 | # FROM actor 365 | # WHERE actor.film_id = film.id) AS actor_bio; 366 | postgres=# \q 367 | ######################################################## 368 | ### Chapter 7 on asclinux/linuxforphp-8.1:7.1.16-nts ### 369 | ######################################################## 370 | rm /srv/www 371 | ln -s /srv/fasterweb/chapter_7 /srv/www 372 | cd /srv/www 373 | ######################################################## 374 | ### Chapter 8 on asclinux/linuxforphp-8.1:7.1.16-nts ### 375 | ######################################################## 376 | rm /srv/www 377 | ln -s /srv/fasterweb/chapter_8 /srv/www 378 | cd /srv/www 379 | ######################################################## 380 | ### Chapter 9 on asclinux/linuxforphp-8.1:7.1.16-nts ### 381 | ######################################################## 382 | rm /srv/www 383 | ln -s /srv/fasterweb/chapter_9 /srv/www 384 | cd /srv/www 385 | cat >>/etc/php.ini << EOF 386 | [OpCache] 387 | zend_extension = $( php -i | grep extensions | awk '{print $3}' )/opcache.so 388 | EOF 389 | sed -i 's/;opcache.enable=1/opcache.enable=1/' /etc/php.ini 390 | sed -i 's/Proxy "fcgi:\/\/127.0.0.1:9000"/Proxy "unix:\/run\/php-fpm.sock|fcgi:\/\/localhost\/"/' /etc/httpd/httpd.conf 391 | sed -i 's/# SetHandler "proxy:unix:/SetHandler "proxy:unix:/' /etc/httpd/httpd.conf 392 | sed -i 's/SetHandler "proxy:fcgi:/# SetHandler "proxy:fcgi:/' /etc/httpd/httpd.conf 393 | sed -i 's/listen = 127.0.0.1:9000/; listen = 127.0.0.1:9000\nlisten = \/run\/php-fpm.sock/' /etc/php-fpm.d/www.conf 394 | /etc/init.d/php-fpm restart 395 | chown apache:apache /run/php-fpm.sock 396 | /etc/init.d/httpd restart 397 | ######################################################## 398 | ### Chapter 9 on asclinux/linuxforphp-8.1:src ### 399 | ######################################################## 400 | pip install --upgrade pip 401 | pip install docutils sphinx 402 | cd /tmp 403 | wget https://github.com/varnishcache/varnish-cache/archive/varnish-6.0.0.tar.gz 404 | tar -xvf varnish-6.0.0.tar.gz 405 | cd varnish-cache-varnish-6.0.0/ 406 | sh autogen.sh 407 | sh configure 408 | make 409 | make install 410 | varnishd -a 0.0.0.0:80 -T 0.0.0.0:6082 -b 172.17.0.2:80 411 | ######################################################### 412 | ### Chapter 10 on asclinux/linuxforphp-8.1:7.1.16-nts ### 413 | ######################################################### 414 | rm /srv/www 415 | ln -s /srv/fasterweb/chapter_10 /srv/www 416 | cd /srv/www -------------------------------------------------------------------------------- /bash_history_host.txt: -------------------------------------------------------------------------------- 1 | ################# 2 | ### Chapter 1 ### 3 | ################# 4 | git clone https://github.com/andrewscaya/fasterweb 5 | cd fasterweb/ 6 | docker run -it --rm -p 8181:80 -v ${PWD}/:/srv/fasterweb asclinux/linuxforphp-8.1:7.1.16-nts /bin/bash 7 | ################# 8 | ### Chapter 2 ### 9 | ################# 10 | docker run -d --name influxdb -p 8086:8086 andrewscaya/influxdb 11 | docker network inspect bridge 12 | docker run --rm andrewscaya/telegraf -sample-config > telegraf.conf 13 | docker run --net=container:influxdb -v ${PWD}/telegraf.conf:/etc/telegraf/telegraf.conf:ro andrewscaya/telegraf 14 | docker run -d --name grafana -p 3000:3000 andrewscaya/grafana 15 | ################# 16 | ### Chapter 4 ### 17 | ################# 18 | docker run -it --rm -p 8282:80 -v ${PWD}/:/srv/fasterweb asclinux/linuxforphp-8.1:7.0.29-zts /bin/bash 19 | docker exec -it $( docker ps -q | awk '{ print $1 }' ) /bin/bash 20 | ################# 21 | ### Chapter 9 ### 22 | ################# 23 | docker run --rm -it -p 8383:80 asclinux/linuxforphp-8.1:src /bin/bash -c "cd ; wget -O tmp http://bit.ly/2jheBrr ; /bin/bash ./tmp 7.2.5 nts ; echo ' /srv/www/index.php ; /bin/bash" 24 | docker run -it -p 6082:6082 -p 8484:80 asclinux/linuxforphp-8.1:src /bin/bash 25 | docker network inspect bridge 26 | curl -v 172.17.0.3 | grep Forwarded 27 | curl -v https://linuxforphp.net -------------------------------------------------------------------------------- /chapter_1/cachegrind.out.412: -------------------------------------------------------------------------------- 1 | version: 1 2 | creator: xdebug 2.5.5 (PHP 7.1.3) 3 | cmd: /srv/www/index.php 4 | part: 1 5 | positions: line 6 | 7 | events: Time 8 | 9 | fl=(1) php:internal 10 | fn=(1) php::phpinfo 11 | 3 1221 12 | 13 | fl=(2) /srv/www/index.php 14 | fn=(2) {main} 15 | 16 | summary: 12235718 17 | 18 | 1 12234496 19 | cfl=(1) 20 | cfn=(1) 21 | calls=1 0 0 22 | 3 1221 23 | 24 | -------------------------------------------------------------------------------- /chapter_1/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | It works! 6 | 7 | 8 |

It works!

9 | 10 | -------------------------------------------------------------------------------- /chapter_1/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Slow page 7 | 8 | 9 | 62 | 63 | 64 | 65 | 66 |
67 | 68 |
69 | 70 | 71 | 72 | 73 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /chapter_10/chap10_slow_before.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | It works! 6 | 7 | 8 |

It works!

9 | 10 | -------------------------------------------------------------------------------- /chapter_10/index.php: -------------------------------------------------------------------------------- 1 | ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/blackfire.ini 29 | echo "blackfire.agent_socket=unix:///tmp/blackfire.sock" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/blackfire.ini 30 | fi 31 | 32 | script: 33 | - phpunit -------------------------------------------------------------------------------- /chapter_2/LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /chapter_2/agent: -------------------------------------------------------------------------------- 1 | [blackfire] 2 | ; 3 | ; setting: ca-cert 4 | ; desc : Sets the PEM encoded certificates 5 | ; default: 6 | ca-cert= 7 | 8 | ; 9 | ; setting: collector 10 | ; desc : Sets the URL of Blackfire's data collector 11 | ; default: https://blackfire.io 12 | collector=https://blackfire.io/ 13 | 14 | ; 15 | ; setting: log-file 16 | ; desc : Sets the path of the log file. Use stderr to log to stderr 17 | ; default: stderr 18 | log-file=stderr 19 | 20 | ; 21 | ; setting: log-level 22 | ; desc : log verbosity level (4: debug, 3: info, 2: warning, 1: error) 23 | ; default: 1 24 | log-level=1 25 | 26 | ; 27 | ; setting: server-id 28 | ; desc : Sets the server id used to authenticate with Blackfire API 29 | ; default: 30 | server-id= 31 | 32 | ; 33 | ; setting: server-token 34 | ; desc : Sets the server token used to authenticate with Blackfire 35 | API. It is unsafe to set this from the command line 36 | ; default: 37 | server-token= 38 | 39 | ; 40 | ; setting: socket 41 | ; desc : Sets the socket the agent should read traces from. Possible 42 | value can be a unix socket or a TCP address 43 | ; default: unix:///var/run/blackfire/agent.sock on Linux, 44 | unix:///usr/local/var/run/blackfire-agent.sock on MacOSX, and 45 | tcp://127.0.0.1:8307 on Windows. 46 | socket=unix:///var/run/blackfire/agent.sock 47 | 48 | ; 49 | ; setting: spec 50 | ; desc : Sets the path to the json specifications file 51 | ; default: 52 | spec= 53 | -------------------------------------------------------------------------------- /chapter_2/bash_history_container.txt: -------------------------------------------------------------------------------- 1 | ######################################################## 2 | ### Chapter 1 on asclinux/linuxforphp-8.1:7.1.16-nts ### 3 | ######################################################## 4 | mv /srv/www /srv/www.OLD 5 | ln -s /srv/fasterweb/chapter_1 /srv/www 6 | cd /srv/www 7 | /etc/init.d/mysql start 8 | /etc/init.d/php-fpm start 9 | /etc/init.d/httpd start 10 | touch /srv/www/index.php 11 | echo "> /srv/www/index.php 12 | cat /srv/www/index.php 13 | ab -k -l -c 2 -n 2000 localhost/index.html 14 | ab -k -l -c 2 -n 2000 localhost/index.php 15 | wget -O siege-4.0.2.tar.gz http://download.joedog.org/siege/siege-4.0.2.tar.gz 16 | tar -xzvf siege-4.0.2.tar.gz 17 | cd siege-4.0.2 18 | ./configure 19 | make 20 | make install 21 | man siege 22 | siege -b -c 3000 -r 100 localhost/index.html 23 | cd /srv/www 24 | touch urlfile.txt 25 | for i in {1..4}; do echo "http://localhost/index.html" >> urlfile.txt ; done 26 | bombardment urlfile.txt 10 100 4 0 27 | echo "http://localhost/index.php" > urlfile.txt 28 | for i in {1..3}; do echo "http://localhost/index.php" >> urlfile.txt ; done 29 | bombardment urlfile.txt 10 100 4 0 30 | pecl install xdebug 31 | echo -e "zend_extension=$( php -i | grep extensions | awk '{print $3}' )/xdebug.so\n" >> /etc/php.ini 32 | echo -e "xdebug.remote_enable = 1\n" >> /etc/php.ini 33 | echo -e "xdebug.remote_enable_trigger = 1\n" >> /etc/php.ini 34 | echo -e "xdebug.remote_connect_back = 1\n" >> /etc/php.ini 35 | echo -e "xdebug.idekey = PHPSTORM\n" >> /etc/php.ini 36 | echo -e "xdebug.profiler_enable = 1\n" >> /etc/php.ini 37 | echo -e "xdebug.profiler_enable_trigger = 1\n" >> /etc/php.ini 38 | vi /etc/php.ini 39 | /etc/init.d/php-fpm restart 40 | ifconfig 41 | ls -lsa /tmp 42 | cat /tmp/cachegrind.out.411 43 | cd 44 | wget -O sakila-db.tar.gz https://downloads.mysql.com/docs/sakila-db.tar.gz 45 | tar -xzvf sakila-db.tar.gz 46 | mysql -uroot < sakila-db/sakila-schema.sql 47 | mysql -uroot < sakila-db/sakila-data.sql 48 | mysqlslap --user=root --host=localhost --concurrency=20--number-of-queries=1000 --create-schema=sakila --query="SELECT * FROM film;" --delimiter=";" --verbose --iterations=2 --debug-info 49 | mysqlslap --user=root --host=localhost --concurrency=50 --number-of-queries=1000 --create-schema=sakila --query="SELECT * FROM film;" --delimiter=";" --verbose --iterations=2 --debug-info 50 | ######################################################## 51 | ### Chapter 2 on asclinux/linuxforphp-8.1:7.1.16-nts ### 52 | ######################################################## 53 | rm /srv/www 54 | ln -s /srv/fasterweb/chapter_2 /srv/www 55 | cd /srv/www 56 | wget -O blackfire-agent https://packages.blackfire.io/binaries/blackfire-agent/1.17.0/blackfire-agent-linux_static_amd64 57 | mv blackfire-agent /usr/local/bin/ 58 | chmod +x /usr/local/bin/blackfire-agent 59 | mkdir -p /etc/blackfire 60 | cp /srv/www/agent /etc/blackfire/ 61 | mkdir -p /var/run/blackfire 62 | touch /var/run/blackfire/agent.sock 63 | blackfire-agent -register 64 | blackfire-agent start & 65 | wget -O blackfire https://packages.blackfire.io/binaries/blackfire-agent/1.17.0/blackfire-cli-linux_static_amd64 66 | mv blackfire /usr/local/bin/ 67 | chmod +x /usr/local/bin/blackfire 68 | blackfire config 69 | wget -O blackfire.so https://packages.blackfire.io/binaries/blackfire-php/1.20.0/blackfire-php-linux_amd64-php-71.so 70 | php -i | grep 'extension_dir' 71 | mv blackfire.so $( php -i | grep extensions | awk '{print $3}' ) 72 | sed -i 's/zend_extension=\/usr\/lib\/php\/extensions\/no-debug-non-zts-20160303\/xdebug.so/;zend_extension=\/usr\/lib\/php\/extensions\/no-debug-non-zts-20160303\/xdebug.so/' /etc/php.ini 73 | sed -i 's/^xdebug/;xdebug/' /etc/php.ini 74 | cat >>/etc/php.ini << 'EOF' 75 | 76 | [blackfire] 77 | extension=blackfire.so 78 | ; On Windows use the following configuration: 79 | ; extension=php_blackfire.dll 80 | 81 | ; Sets the socket where the agent is listening. 82 | ; Possible value can be a unix socket or a TCP address. 83 | ; Defaults to unix:///var/run/blackfire/agent.sock on Linux, 84 | ; unix:///usr/local/var/run/blackfire-agent.sock on MacOSX, 85 | ; and to tcp://127.0.0.1:8307 on Windows. 86 | ;blackfire.agent_socket = unix:///var/run/blackfire/agent.sock 87 | 88 | blackfire.agent_timeout = 0.25 89 | 90 | ; Log verbosity level (4: debug, 3: info, 2: warning, 1: error) 91 | ;blackfire.log_level = 1 92 | 93 | ; Log file (STDERR by default) 94 | ;blackfire.log_file = /tmp/blackfire.log 95 | 96 | ;blackfire.server_id = 97 | 98 | ;blackfire.server_token = 99 | EOF 100 | /etc/init.d/php-fpm restart 101 | blackfire curl http://localhost/index.php 102 | gem install travis 103 | travis encrypt-file /srv/www/.blackfire.travis.ini -r [your_Github_repository_name_here] 104 | sed -i 's/#Include \/etc\/httpd\/extra\/httpd-info.conf/Include \/etc\/httpd\/extra\/httpd-info.conf/' /etc/httpd/httpd.conf 105 | sed -i 's/Require ip 127/Require ip 172/' /etc/httpd/extra/httpd-info.conf 106 | /etc/init.d/httpd restart 107 | siege -b -c 3000 -r 100 localhost/chap2pre.php 108 | siege -b -c 3000 -r 100 localhost/chap2post.php 109 | ######################################################## 110 | ### Chapter 3 on asclinux/linuxforphp-8.1:7.1.16-nts ### 111 | ######################################################## 112 | rm /srv/www 113 | ln -s /srv/fasterweb/chapter_3 /srv/www 114 | cd /srv/www 115 | php chap3_strict_typing.php 116 | php chap3_strict_typing_modified.php 117 | php chap3_immutable_arrays.php 118 | php chap3_variables.php 119 | php chap3_string_interpolation.php 120 | php chap3_references.php 121 | php chap3_dynamic_1.php 122 | php chap3_dynamic_2.php 123 | php chap3_dynamic_3.php 124 | php chap3_dynamic_4.php 125 | php chap3_memoization_before.php 126 | php chap3_memoization_after.php 127 | ######################################################## 128 | ### Chapter 4 on asclinux/linuxforphp-8.1:7.1.16-nts ### 129 | ######################################################## 130 | rm /srv/www 131 | ln -s /srv/fasterweb/chapter_4 /srv/www 132 | cd /srv/www 133 | php chap4_IO_blocking.php 134 | php chap4_IO_non_blocking.php 135 | php chap4_async_race.php 136 | ######################################################## 137 | ### Chapter 4 on asclinux/linuxforphp-8.1:7.0.29-zts ### 138 | ######################################################## 139 | mv /srv/www /srv/www.OLD 140 | ln -s /srv/fasterweb/chapter_4 /srv/www 141 | cd /srv/www 142 | pecl install pthreads 143 | echo "extension=pthreads.so" >> /etc/php.ini 144 | php -v 145 | php -i 146 | php chap4_pthreads.php 147 | cd /srv/www/react 148 | php composer.phar self-update 149 | php composer.phar install 150 | cd examples 151 | php parallel-download.php 152 | php tcp-chat.php 153 | telnet localhost 4000 154 | php scalability.php 155 | wget -nv -O - http://localhost:8000 156 | wget -nv -O - http://localhost:8001 157 | wget -nv -O - http://localhost:8002 158 | wget -nv -O - http://localhost:8003 159 | wget -nv -O - http://localhost:8004 160 | wget -nv -O - http://localhost:8005 161 | wget -nv -O - http://localhost:8006 162 | wget -nv -O - http://localhost:8007 163 | wget -nv -O - http://localhost:8008 164 | wget -nv -O - http://localhost:8009 165 | ######################################################## 166 | ### Chapter 5 on asclinux/linuxforphp-8.1:7.1.16-nts ### 167 | ######################################################## 168 | rm /srv/www 169 | ln -s /srv/fasterweb/chapter_5 /srv/www 170 | cd /srv/www 171 | mysql -uroot 172 | # MariaDB > USE sakila; 173 | # MariaDB > SELECT * FROM actor WHERE first_name = ‘AL’; 174 | # MariaDB > EXPLAIN SELECT * FROM actor WHERE first_name= ‘AL’; 175 | # MariaDB > CREATE INDEX idx_first_name ON actor(first_name); 176 | # MariaDB > EXPLAIN SELECT first_name FROM actor WHERE first_name = ‘AL’; 177 | # MariaDB > exit 178 | mysqlslap --user=root --host=localhost --concurrency=1000 --number-of-queries=10000 --create-schema=sakila --query="SELECT * FROM actor WHERE first_name = 'AL';" --delimiter=";" --verbose --iterations=2 --debug-info 179 | mysqlslap --user=root --host=localhost --concurrency=1000 --number-of-queries=10000 --create-schema=sakila --query="SELECT first_name FROM actor WHERE first_name = 'AL';" --delimiter=";" --verbose --iterations=2 --debug-info 180 | sed -i '/myisam_sort_buffer_size =/a performance_schema = ON' /etc/mysql/my.cnf 181 | sed -i '/performance_schema =/a performance-schema-instrument = "stage/%=ON"' /etc/mysql/my.cnf 182 | sed -i '/performance-schema-instrument =/a performance-schema-consumer-events-stages-current = ON' /etc/mysql/my.cnf 183 | sed -i '/performance-schema-consumer-events-stages-current =/a performance-schema-consumer-events-stages-history = ON' /etc/mysql/my.cnf 184 | sed -i '/performance-schema-consumer-events-stages-history =/a performance-schema-consumer-events-stages-history-long = ON' /etc/mysql/my.cnf 185 | /etc/init.d/mysql restart 186 | mysql -uroot 187 | # MariaDB > USE performance_schema; 188 | # MariaDB > UPDATE setup_instruments SET ENABLED = 'YES', TIMED = 'YES'; 189 | # MariaDB > UPDATE setup_consumers SET ENABLED = 'YES'; 190 | # MariaDB > SHOW VARIABLES LIKE 'performance_schema'; 191 | # MariaDB > SELECT film.film_id 192 | # > FROM film 193 | # > WHERE film.rating = 'G' 194 | # > AND film.film_id NOT IN ( 195 | # > SELECT film.film_id 196 | # > FROM rental 197 | # > LEFT JOIN inventory ON rental.inventory_id = inventory.inventory_id 198 | # > LEFT JOIN film ON inventory.film_id = film.film_id 199 | # > ); 200 | # MariaDB > EXPLAIN SELECT film.film_id 201 | # > FROM film 202 | # > WHERE film.rating = 'G' 203 | # > AND film.film_id NOT IN ( 204 | # > SELECT film.film_id 205 | # > FROM rental 206 | # > LEFT JOIN inventory ON rental.inventory_id = inventory.inventory_id 207 | # > LEFT JOIN film ON inventory.film_id = film.film_id 208 | # > ); 209 | # MariaDB > SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT 210 | # > FROM performance_schema.events_statements_history_long WHERE SQL_TEXT like '%NOT IN%'; 211 | # MariaDB > SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT/1000000000000,6) AS Duration 212 | # > FROM performance_schema.events_stages_history_long WHERE NESTING_EVENT_ID=43; 213 | # MariaDB > SELECT film.film_id 214 | # > FROM rental 215 | # > INNER JOIN inventory ON rental.inventory_id = inventory.inventory_id 216 | # > RIGHT JOIN film ON inventory.film_id = film.film_id 217 | # > WHERE film.rating = 'G' 218 | # > AND rental.rental_id IS NULL 219 | # > GROUP BY film.film_id; 220 | # MariaDB > EXPLAIN SELECT film.film_id 221 | # > FROM rental 222 | # > INNER JOIN inventory ON rental.inventory_id = inventory.inventory_id 223 | # > RIGHT JOIN film ON inventory.film_id = film.film_id 224 | # > WHERE film.rating = 'G' 225 | # > AND rental.rental_id IS NULL 226 | # > GROUP BY film.film_id; 227 | # MariaDB > SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT 228 | # > FROM performance_schema.events_statements_history_long WHERE SQL_TEXT like '%GROUP BY%'; 229 | # MariaDB > SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT/1000000000000,6) AS Duration 230 | # > FROM performance_schema.events_stages_history_long WHERE NESTING_EVENT_ID=22717; 231 | # MariaDB > exit 232 | cd /srv/www 233 | wget -O dbt2-0.37.tar.gz https://master.dl.sourceforge.net/project/osdldbt/dbt2/0.37/dbt2-0.37.tar.gz 234 | tar -xvf dbt2-0.37.tar.gz 235 | cd dbt2-0.37 236 | ./configure --with-mysql 237 | make 238 | make install 239 | cpan install Statistics::Descriptive 240 | mkdir -p /srv/mysql/dbt2-tmp-data/dbt2-w3 241 | ./src/datagen -w 3 -d /srv/mysql/dbt2-tmp-data/dbt2-w3 --mysql 242 | vi scripts/mysql/mysql_load_db.sh 243 | ./scripts/mysql/mysql_load_db.sh -d dbt2 -f /srv/mysql/dbt2-tmp-data/dbt2-w3 -s /run/mysqld/mysqld.sock -u root 244 | ./scripts/run_mysql.sh -n dbt2 -o /run/mysqld/mysqld.sock -u root -w 3 -t 300 -c 20 245 | cd /srv/www 246 | wget -O sysbench-0.4.12.14.tar.gz https://downloads.mysql.com/source/sysbench-0.4.12.14.tar.gz 247 | tar -xvf sysbench-0.4.12.14.tar.gz 248 | cd sysbench-0.4.12.14 249 | ./configure 250 | make 251 | make install 252 | sysbench --test=oltp --oltp-table-size=1000000 --mysql-db=test --mysql-user=root prepare 253 | sysbench --test=oltp --oltp-table-size=1000000 --mysql-db=test --mysql-user=root --max-time=60 --oltp-read-only=on --max-requests=0 --num-threads=8 run 254 | ######################################################## 255 | ### Chapter 6 on asclinux/linuxforphp-8.1:7.1.16-nts ### 256 | ######################################################## 257 | rm /srv/www 258 | ln -s /srv/fasterweb/chapter_6 /srv/www 259 | cd /srv 260 | wget -O phpMyAdmin-4.7.7-all-languages.zip https://files.phpmyadmin.net/phpMyAdmin/4.7.7/phpMyAdmin-4.7.7-all-languages.zip 261 | unzip phpMyAdmin-4.7.7-all-languages.zip 262 | cp phpMyAdmin-4.7.7-all-languages/config.sample.inc.php phpMyAdmin-4.7.7-all-languages/config.inc.php 263 | sed -i "s/AllowNoPassword'] = false/AllowNoPassword'] = true/" phpMyAdmin-4.7.7-all-languages/config.inc.php 264 | cd fasterweb/chapter_6 265 | ln -s ../../phpMyAdmin-4.7.7-all-languages ./phpmyadmin 266 | mysql -uroot test < /srv/www/employees.sql 267 | mysql -uroot 268 | # MariaDB > SELECT CONCAT_WS('->', t1.last_name, t2.last_name, t3.last_name, t4.last_name, t5.last_name, t6.last_name) AS path 269 | # > FROM employees AS t1 270 | # > RIGHT JOIN employees AS t2 ON t2.superior = t1.id 271 | # > RIGHT JOIN employees AS t3 ON t3.superior = t2.id 272 | # > RIGHT JOIN employees AS t4 ON t4.superior = t3.id 273 | # > RIGHT JOIN employees AS t5 ON t5.superior = t4.id 274 | # > RIGHT JOIN employees AS t6 ON t6.superior = t5.id 275 | # > WHERE t1.superior IS NULL 276 | # > ORDER BY path; 277 | # MariaDB > SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT 278 | # > FROM performance_schema.events_statements_history_long WHERE SQL_TEXT like '%RIGHT JOIN%'; 279 | # MariaDB > SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT/1000000000000,6) AS Duration 280 | # > FROM performance_schema.events_stages_history_long WHERE NESTING_EVENT_ID=126; 281 | # MariaDB > WITH RECURSIVE hierarchy_list AS ( 282 | # > SELECT id, superior, CONVERT(last_name, CHAR(100)) AS path 283 | # > FROM employees 284 | # > WHERE superior IS NULL 285 | # > UNION ALL 286 | # > SELECT child.id, child.superior, CONVERT(CONCAT(parent.path, '->', child.last_name), CHAR(100)) AS path 287 | # > FROM employees AS child 288 | # > INNER JOIN hierarchy_list AS parent ON (child.superior = parent.id) 289 | # > ) 290 | # > SELECT path 291 | # > FROM hierarchy_list 292 | # > ORDER BY path; 293 | # MariaDB > SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT 294 | # > FROM performance_schema.events_statements_history_long WHERE SQL_TEXT like '%WITH RECURSIVE%'; 295 | # MariaDB > SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT/1000000000000,6) AS Duration 296 | # > FROM performance_schema.events_stages_history_long WHERE NESTING_EVENT_ID=259; 297 | # MariaDB > SELECT id, COUNT(*) AS Total, COUNT(CASE WHEN superior IS NOT NULL THEN id END) AS 'Number of superiors' 298 | # > FROM employees 299 | # > WHERE id = 2; 300 | # MariaDB > SELECT DISTINCT superior AS manager_id, (SELECT last_name FROM employees WHERE id = manager_id) AS last_name, SUM(salary) OVER(PARTITION BY superior) AS 'payroll per manager' 301 | # > FROM employees 302 | # > WHERE superior IS NOT NULL 303 | # > ORDER BY superior; 304 | # MariaDB > SELECT id, last_name, salary, superior AS manager_id, (SELECT last_name FROM employees WHERE id = manager_id) AS manager_last_name, SUM(salary) OVER(PARTITION BY superior ORDER BY manager_last_name, salary DESC, id) AS payroll_per_manager 305 | # > FROM employees 306 | # > WHERE superior IS NOT NULL 307 | # > ORDER BY manager_last_name, salary DESC, id; 308 | # MariaDB > SELECT id, last_name, salary, superior AS manager_id, SUM(salary) OVER(PARTITION BY superior ORDER BY manager_id, salary DESC, id) AS payroll_per_manager 309 | # > FROM employees 310 | # > WHERE superior IS NOT NULL 311 | # > ORDER BY manager_id, salary DESC, id; 312 | # MariaDB > SELECT superior AS manager_id, SUM(salary) 313 | # > FROM employees 314 | # > WHERE superior IS NOT NULL 315 | # > GROUP BY manager_id, id, salary; 316 | # MariaDB > SELECT superior AS manager_id, SUM(salary) 317 | # > FROM employees 318 | # > WHERE superior IS NOT NULL 319 | # > GROUP BY manager_id, salary WITH ROLLUP; 320 | # MariaDB > exit 321 | mysql -uroot test < /srv/www/json_example.sql 322 | mysql -uroot 323 | # MariaDB > SELECT id, 324 | # > JSON_VALUE(json, "$.name") AS name, 325 | # > JSON_VALUE(json, "$.roles[0]") AS main_role, 326 | # > JSON_VALUE(json, "$.active") AS active 327 | # > FROM json_example 328 | # > WHERE id = 1; 329 | # MariaDB > INSERT INTO `json_example` (`id`, `json`) VALUES (NULL, 'test'); 330 | # MariaDB > exit 331 | /etc/init.d/postgresql start 332 | cd /srv 333 | wget --no-check-certificate -O phpPgAdmin-5.1.tar.gz https://superb-sea2.dl.sourceforge.net/project/phppgadmin/phpPgAdmin%20%5Bstable%5D/phpPgAdmin-5.1/phpPgAdmin-5.1.tar.gz 334 | tar -xvf phpPgAdmin-5.1.tar.gz 335 | sed -i "s/extra_login_security'] = true/extra_login_security'] = false/" phpPgAdmin-5.1/conf/config.inc.php 336 | cd fasterweb/chapter_6 337 | ln -s ../../phpPgAdmin-5.1 ./phppgadmin 338 | cd /srv/www 339 | su postgres 340 | psql test < sales.sql 341 | psql 342 | postgres=# SELECT 343 | # SUM(total) as total, 344 | # SUM(total) FILTER(WHERE closed IS TRUE) as transaction_complete, 345 | # year 346 | # FROM 347 | # sales 348 | # GROUP BY 349 | # year; 350 | postgres=# SELECT 351 | # film.id, 352 | # film.title, 353 | # actor_bio.name, 354 | # actor_bio.biography 355 | # FROM film, 356 | # LATERAL (SELECT 357 | # actor.name 358 | # actor.biography, 359 | # FROM actor 360 | # WHERE actor.film_id = film.id) AS actor_bio; 361 | postgres=# \q 362 | ######################################################## 363 | ### Chapter 7 on asclinux/linuxforphp-8.1:7.1.16-nts ### 364 | ######################################################## 365 | rm /srv/www 366 | ln -s /srv/fasterweb/chapter_7 /srv/www 367 | cd /srv/www 368 | ######################################################## 369 | ### Chapter 8 on asclinux/linuxforphp-8.1:7.1.16-nts ### 370 | ######################################################## 371 | rm /srv/www 372 | ln -s /srv/fasterweb/chapter_8 /srv/www 373 | cd /srv/www 374 | ######################################################## 375 | ### Chapter 9 on asclinux/linuxforphp-8.1:7.1.16-nts ### 376 | ######################################################## 377 | rm /srv/www 378 | ln -s /srv/fasterweb/chapter_9 /srv/www 379 | cd /srv/www 380 | cat >>/etc/php.ini << EOF 381 | [OpCache] 382 | zend_extension = $( php -i | grep extensions | awk '{print $3}' )/opcache.so 383 | EOF 384 | sed -i 's/;opcache.enable=1/opcache.enable=1/' /etc/php.ini 385 | sed -i 's/Proxy "fcgi:\/\/127.0.0.1:9000"/Proxy "unix:\/run\/php-fpm.sock|fcgi:\/\/localhost\/"/' /etc/httpd/httpd.conf 386 | sed -i 's/# SetHandler "proxy:unix:/SetHandler "proxy:unix:/' /etc/httpd/httpd.conf 387 | sed -i 's/SetHandler "proxy:fcgi:/# SetHandler "proxy:fcgi:/' /etc/httpd/httpd.conf 388 | sed -i 's/listen = 127.0.0.1:9000/; listen = 127.0.0.1:9000\nlisten = \/run\/php-fpm.sock/' /etc/php-fpm.d/www.conf 389 | /etc/init.d/php-fpm restart 390 | chown apache:apache /run/php-fpm.sock 391 | /etc/init.d/httpd restart 392 | ######################################################## 393 | ### Chapter 9 on asclinux/linuxforphp-8.1:src ### 394 | ######################################################## 395 | pip install --upgrade pip 396 | pip install docutils sphinx 397 | cd /tmp 398 | wget https://github.com/varnishcache/varnish-cache/archive/varnish-6.0.0.tar.gz 399 | tar -xvf varnish-6.0.0.tar.gz 400 | cd varnish-cache-varnish-6.0.0/ 401 | sh autogen.sh 402 | sh configure 403 | make 404 | make install 405 | varnishd -a 0.0.0.0:80 -T 0.0.0.0:6082 -b 172.17.0.2:80 406 | ######################################################### 407 | ### Chapter 10 on asclinux/linuxforphp-8.1:7.1.16-nts ### 408 | ######################################################### 409 | rm /srv/www 410 | ln -s /srv/fasterweb/chapter_10 /srv/www 411 | cd /srv/www -------------------------------------------------------------------------------- /chapter_2/bash_history_host.txt: -------------------------------------------------------------------------------- 1 | ################# 2 | ### Chapter 1 ### 3 | ################# 4 | git clone https://github.com/andrewscaya/fasterweb 5 | cd fasterweb/ 6 | docker run -it --rm -p 8181:80 -v ${PWD}/:/srv/fasterweb asclinux/linuxforphp-8.1:7.1.16-nts /bin/bash 7 | ################# 8 | ### Chapter 2 ### 9 | ################# 10 | docker run -d --name influxdb -p 8086:8086 andrewscaya/influxdb 11 | docker network inspect bridge 12 | docker run --rm andrewscaya/telegraf -sample-config > telegraf.conf 13 | docker run --net=container:influxdb -v ${PWD}/telegraf.conf:/etc/telegraf/telegraf.conf:ro andrewscaya/telegraf 14 | docker run -d --name grafana -p 3000:3000 andrewscaya/grafana 15 | ################# 16 | ### Chapter 4 ### 17 | ################# 18 | docker run -it --rm -p 8282:80 -v ${PWD}/:/srv/fasterweb asclinux/linuxforphp-8.1:7.0.29-zts /bin/bash 19 | docker exec -it $( docker ps -q | awk '{ print $1 }' ) /bin/bash 20 | ################# 21 | ### Chapter 9 ### 22 | ################# 23 | docker run --rm -it -p 8383:80 asclinux/linuxforphp-8.1:src /bin/bash -c "cd ; wget -O tmp http://bit.ly/2jheBrr ; /bin/bash ./tmp 7.2.5 nts ; echo ' /srv/www/index.php ; /bin/bash" 24 | docker run -it -p 6082:6082 -p 8484:80 asclinux/linuxforphp-8.1:src /bin/bash 25 | docker network inspect bridge 26 | curl -v 172.17.0.3 | grep Forwarded 27 | curl -v https://linuxforphp.net -------------------------------------------------------------------------------- /chapter_2/blackfire: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_2/blackfire -------------------------------------------------------------------------------- /chapter_2/blackfire-agent: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_2/blackfire-agent -------------------------------------------------------------------------------- /chapter_2/blackfire.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_2/blackfire.so -------------------------------------------------------------------------------- /chapter_2/chap2post.php: -------------------------------------------------------------------------------- 1 | ' . preg_replace('/\n+/', '
', getDiskUsage($directory)) 18 | : $du; 19 | 20 | $fileList = []; 21 | 22 | $iterator = new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS); 23 | 24 | foreach($iterator as $entry) { 25 | 26 | $fileName = $entry->getFilename(); 27 | 28 | $dirFlag = $entry->isDir(); 29 | 30 | if (!$dirFlag && $fileName[0] != '.') { 31 | $fileList[$fileName] = 'size is ' . $entry->getSize(); 32 | } else { 33 | if ($dirFlag && $fileName[0] != '.') { 34 | $fileList[$fileName] = getDirList( 35 | $directory . DIRECTORY_SEPARATOR . $fileName, 36 | $du 37 | ); 38 | } 39 | } 40 | 41 | } 42 | 43 | return $fileList; 44 | } 45 | 46 | $du = ''; 47 | 48 | $baseDirectory = dirname(__FILE__); 49 | 50 | $fileList = getDirList($baseDirectory, $du); 51 | 52 | echo '

'; 53 | 54 | echo 'Disk Usage : ' . $du . '


'; 55 | 56 | echo 'Directory Name : ' . $baseDirectory . '

'; 57 | 58 | echo 'File listing :'; 59 | 60 | echo '

';
61 | 
62 | print_r($fileList);
63 | 
64 | echo '
'; -------------------------------------------------------------------------------- /chapter_2/chap2pre.php: -------------------------------------------------------------------------------- 1 | ' . preg_replace('/\n+/', '
', $result) 20 | : $du; 21 | 22 | $fileList = []; 23 | 24 | $iterator = new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS); 25 | 26 | foreach($iterator as $entry) { 27 | 28 | if (!$entry->isDir() && $entry->getFilename()[0] != '.') { 29 | $fileList[$entry->getFilename()] = 'size is ' . $entry->getSize(); 30 | } else { 31 | if ($entry->isDir() && $entry->getFilename()[0] != '.') { 32 | $fileList[$entry->getFilename()] = getDirList( 33 | $directory . DIRECTORY_SEPARATOR . $entry->getFilename(), 34 | $du 35 | ); 36 | } 37 | } 38 | 39 | } 40 | 41 | return $fileList; 42 | } 43 | 44 | $du = ''; 45 | 46 | $baseDirectory = dirname(__FILE__); 47 | 48 | $fileList = getDirList($baseDirectory, $du); 49 | 50 | echo '

'; 51 | 52 | echo 'Disk Usage : ' . $du . '


'; 53 | 54 | echo 'Directory Name : ' . $baseDirectory . '

'; 55 | 56 | echo 'File listing :'; 57 | 58 | echo '

';
59 | 
60 | print_r($fileList);
61 | 
62 | echo '
'; -------------------------------------------------------------------------------- /chapter_2/fakedir/fakedir2/file4.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_2/fakedir/fakedir2/file4.txt -------------------------------------------------------------------------------- /chapter_2/fakedir/fakedir2/file5.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_2/fakedir/fakedir2/file5.txt -------------------------------------------------------------------------------- /chapter_2/fakedir/fakedir2/file6.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_2/fakedir/fakedir2/file6.txt -------------------------------------------------------------------------------- /chapter_2/fakedir/fakedir3/file7.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_2/fakedir/fakedir3/file7.txt -------------------------------------------------------------------------------- /chapter_2/fakedir/fakedir3/file8.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_2/fakedir/fakedir3/file8.txt -------------------------------------------------------------------------------- /chapter_2/fakedir/fakedir3/file9.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_2/fakedir/fakedir3/file9.txt -------------------------------------------------------------------------------- /chapter_2/fakedir/file1.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_2/fakedir/file1.txt -------------------------------------------------------------------------------- /chapter_2/fakedir/file2.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_2/fakedir/file2.txt -------------------------------------------------------------------------------- /chapter_2/fakedir/file3.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_2/fakedir/file3.txt -------------------------------------------------------------------------------- /chapter_2/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | It works! 6 | 7 | 8 |

It works!

9 | 10 | -------------------------------------------------------------------------------- /chapter_2/index.php: -------------------------------------------------------------------------------- 1 | 100% because 396 | -- some titles belong to more than 1 category 397 | -- 398 | 399 | CREATE VIEW sales_by_film_category 400 | AS 401 | SELECT 402 | c.name AS category 403 | , SUM(p.amount) AS total_sales 404 | FROM payment AS p 405 | INNER JOIN rental AS r ON p.rental_id = r.rental_id 406 | INNER JOIN inventory AS i ON r.inventory_id = i.inventory_id 407 | INNER JOIN film AS f ON i.film_id = f.film_id 408 | INNER JOIN film_category AS fc ON f.film_id = fc.film_id 409 | INNER JOIN category AS c ON fc.category_id = c.category_id 410 | GROUP BY c.name 411 | ORDER BY total_sales DESC; 412 | 413 | -- 414 | -- View structure for view `actor_info` 415 | -- 416 | 417 | CREATE DEFINER=CURRENT_USER SQL SECURITY INVOKER VIEW actor_info 418 | AS 419 | SELECT 420 | a.actor_id, 421 | a.first_name, 422 | a.last_name, 423 | GROUP_CONCAT(DISTINCT CONCAT(c.name, ': ', 424 | (SELECT GROUP_CONCAT(f.title ORDER BY f.title SEPARATOR ', ') 425 | FROM sakila.film f 426 | INNER JOIN sakila.film_category fc 427 | ON f.film_id = fc.film_id 428 | INNER JOIN sakila.film_actor fa 429 | ON f.film_id = fa.film_id 430 | WHERE fc.category_id = c.category_id 431 | AND fa.actor_id = a.actor_id 432 | ) 433 | ) 434 | ORDER BY c.name SEPARATOR '; ') 435 | AS film_info 436 | FROM sakila.actor a 437 | LEFT JOIN sakila.film_actor fa 438 | ON a.actor_id = fa.actor_id 439 | LEFT JOIN sakila.film_category fc 440 | ON fa.film_id = fc.film_id 441 | LEFT JOIN sakila.category c 442 | ON fc.category_id = c.category_id 443 | GROUP BY a.actor_id, a.first_name, a.last_name; 444 | 445 | -- 446 | -- Procedure structure for procedure `rewards_report` 447 | -- 448 | 449 | DELIMITER // 450 | 451 | CREATE PROCEDURE rewards_report ( 452 | IN min_monthly_purchases TINYINT UNSIGNED 453 | , IN min_dollar_amount_purchased DECIMAL(10,2) UNSIGNED 454 | , OUT count_rewardees INT 455 | ) 456 | LANGUAGE SQL 457 | NOT DETERMINISTIC 458 | READS SQL DATA 459 | SQL SECURITY DEFINER 460 | COMMENT 'Provides a customizable report on best customers' 461 | proc: BEGIN 462 | 463 | DECLARE last_month_start DATE; 464 | DECLARE last_month_end DATE; 465 | 466 | /* Some sanity checks... */ 467 | IF min_monthly_purchases = 0 THEN 468 | SELECT 'Minimum monthly purchases parameter must be > 0'; 469 | LEAVE proc; 470 | END IF; 471 | IF min_dollar_amount_purchased = 0.00 THEN 472 | SELECT 'Minimum monthly dollar amount purchased parameter must be > $0.00'; 473 | LEAVE proc; 474 | END IF; 475 | 476 | /* Determine start and end time periods */ 477 | SET last_month_start = DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH); 478 | SET last_month_start = STR_TO_DATE(CONCAT(YEAR(last_month_start),'-',MONTH(last_month_start),'-01'),'%Y-%m-%d'); 479 | SET last_month_end = LAST_DAY(last_month_start); 480 | 481 | /* 482 | Create a temporary storage area for 483 | Customer IDs. 484 | */ 485 | CREATE TEMPORARY TABLE tmpCustomer (customer_id SMALLINT UNSIGNED NOT NULL PRIMARY KEY); 486 | 487 | /* 488 | Find all customers meeting the 489 | monthly purchase requirements 490 | */ 491 | INSERT INTO tmpCustomer (customer_id) 492 | SELECT p.customer_id 493 | FROM payment AS p 494 | WHERE DATE(p.payment_date) BETWEEN last_month_start AND last_month_end 495 | GROUP BY customer_id 496 | HAVING SUM(p.amount) > min_dollar_amount_purchased 497 | AND COUNT(customer_id) > min_monthly_purchases; 498 | 499 | /* Populate OUT parameter with count of found customers */ 500 | SELECT COUNT(*) FROM tmpCustomer INTO count_rewardees; 501 | 502 | /* 503 | Output ALL customer information of matching rewardees. 504 | Customize output as needed. 505 | */ 506 | SELECT c.* 507 | FROM tmpCustomer AS t 508 | INNER JOIN customer AS c ON t.customer_id = c.customer_id; 509 | 510 | /* Clean up */ 511 | DROP TABLE tmpCustomer; 512 | END // 513 | 514 | DELIMITER ; 515 | 516 | DELIMITER $$ 517 | 518 | CREATE FUNCTION get_customer_balance(p_customer_id INT, p_effective_date DATETIME) RETURNS DECIMAL(5,2) 519 | DETERMINISTIC 520 | READS SQL DATA 521 | BEGIN 522 | 523 | #OK, WE NEED TO CALCULATE THE CURRENT BALANCE GIVEN A CUSTOMER_ID AND A DATE 524 | #THAT WE WANT THE BALANCE TO BE EFFECTIVE FOR. THE BALANCE IS: 525 | # 1) RENTAL FEES FOR ALL PREVIOUS RENTALS 526 | # 2) ONE DOLLAR FOR EVERY DAY THE PREVIOUS RENTALS ARE OVERDUE 527 | # 3) IF A FILM IS MORE THAN RENTAL_DURATION * 2 OVERDUE, CHARGE THE REPLACEMENT_COST 528 | # 4) SUBTRACT ALL PAYMENTS MADE BEFORE THE DATE SPECIFIED 529 | 530 | DECLARE v_rentfees DECIMAL(5,2); #FEES PAID TO RENT THE VIDEOS INITIALLY 531 | DECLARE v_overfees INTEGER; #LATE FEES FOR PRIOR RENTALS 532 | DECLARE v_payments DECIMAL(5,2); #SUM OF PAYMENTS MADE PREVIOUSLY 533 | 534 | SELECT IFNULL(SUM(film.rental_rate),0) INTO v_rentfees 535 | FROM film, inventory, rental 536 | WHERE film.film_id = inventory.film_id 537 | AND inventory.inventory_id = rental.inventory_id 538 | AND rental.rental_date <= p_effective_date 539 | AND rental.customer_id = p_customer_id; 540 | 541 | SELECT IFNULL(SUM(IF((TO_DAYS(rental.return_date) - TO_DAYS(rental.rental_date)) > film.rental_duration, 542 | ((TO_DAYS(rental.return_date) - TO_DAYS(rental.rental_date)) - film.rental_duration),0)),0) INTO v_overfees 543 | FROM rental, inventory, film 544 | WHERE film.film_id = inventory.film_id 545 | AND inventory.inventory_id = rental.inventory_id 546 | AND rental.rental_date <= p_effective_date 547 | AND rental.customer_id = p_customer_id; 548 | 549 | 550 | SELECT IFNULL(SUM(payment.amount),0) INTO v_payments 551 | FROM payment 552 | 553 | WHERE payment.payment_date <= p_effective_date 554 | AND payment.customer_id = p_customer_id; 555 | 556 | RETURN v_rentfees + v_overfees - v_payments; 557 | END $$ 558 | 559 | DELIMITER ; 560 | 561 | DELIMITER $$ 562 | 563 | CREATE PROCEDURE film_in_stock(IN p_film_id INT, IN p_store_id INT, OUT p_film_count INT) 564 | READS SQL DATA 565 | BEGIN 566 | SELECT inventory_id 567 | FROM inventory 568 | WHERE film_id = p_film_id 569 | AND store_id = p_store_id 570 | AND inventory_in_stock(inventory_id); 571 | 572 | SELECT FOUND_ROWS() INTO p_film_count; 573 | END $$ 574 | 575 | DELIMITER ; 576 | 577 | DELIMITER $$ 578 | 579 | CREATE PROCEDURE film_not_in_stock(IN p_film_id INT, IN p_store_id INT, OUT p_film_count INT) 580 | READS SQL DATA 581 | BEGIN 582 | SELECT inventory_id 583 | FROM inventory 584 | WHERE film_id = p_film_id 585 | AND store_id = p_store_id 586 | AND NOT inventory_in_stock(inventory_id); 587 | 588 | SELECT FOUND_ROWS() INTO p_film_count; 589 | END $$ 590 | 591 | DELIMITER ; 592 | 593 | DELIMITER $$ 594 | 595 | CREATE FUNCTION inventory_held_by_customer(p_inventory_id INT) RETURNS INT 596 | READS SQL DATA 597 | BEGIN 598 | DECLARE v_customer_id INT; 599 | DECLARE EXIT HANDLER FOR NOT FOUND RETURN NULL; 600 | 601 | SELECT customer_id INTO v_customer_id 602 | FROM rental 603 | WHERE return_date IS NULL 604 | AND inventory_id = p_inventory_id; 605 | 606 | RETURN v_customer_id; 607 | END $$ 608 | 609 | DELIMITER ; 610 | 611 | DELIMITER $$ 612 | 613 | CREATE FUNCTION inventory_in_stock(p_inventory_id INT) RETURNS BOOLEAN 614 | READS SQL DATA 615 | BEGIN 616 | DECLARE v_rentals INT; 617 | DECLARE v_out INT; 618 | 619 | #AN ITEM IS IN-STOCK IF THERE ARE EITHER NO ROWS IN THE rental TABLE 620 | #FOR THE ITEM OR ALL ROWS HAVE return_date POPULATED 621 | 622 | SELECT COUNT(*) INTO v_rentals 623 | FROM rental 624 | WHERE inventory_id = p_inventory_id; 625 | 626 | IF v_rentals = 0 THEN 627 | RETURN TRUE; 628 | END IF; 629 | 630 | SELECT COUNT(rental_id) INTO v_out 631 | FROM inventory LEFT JOIN rental USING(inventory_id) 632 | WHERE inventory.inventory_id = p_inventory_id 633 | AND rental.return_date IS NULL; 634 | 635 | IF v_out > 0 THEN 636 | RETURN FALSE; 637 | ELSE 638 | RETURN TRUE; 639 | END IF; 640 | END $$ 641 | 642 | DELIMITER ; 643 | 644 | SET SQL_MODE=@OLD_SQL_MODE; 645 | SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS; 646 | SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS; 647 | 648 | 649 | -------------------------------------------------------------------------------- /chapter_2/sakila-db/sakila.mwb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_2/sakila-db/sakila.mwb -------------------------------------------------------------------------------- /chapter_3/chap3_dynamic_1.php: -------------------------------------------------------------------------------- 1 | $i; 13 | } 14 | 15 | return; 16 | } 17 | 18 | foreach (populateArray() as $key => $value) { 19 | $data[$key] = $value; 20 | } 21 | 22 | $time = microtime(true) - $start; 23 | 24 | echo 'Time elapsed: ' . $time . PHP_EOL; 25 | 26 | echo memory_get_usage() . ' bytes' . PHP_EOL; -------------------------------------------------------------------------------- /chapter_3/chap3_immutable_arrays.php: -------------------------------------------------------------------------------- 1 | 'This is the first key', 10 | 'key2' => 'This is the second key', 11 | 'key3' => 'This is the third key', 12 | ]; 13 | } 14 | 15 | echo $array[8181]['key2'] . PHP_EOL; 16 | 17 | $time = microtime(true) - $start; 18 | 19 | echo 'Time elapsed: ' . $time . PHP_EOL; 20 | 21 | echo memory_get_usage() . ' bytes' . PHP_EOL; -------------------------------------------------------------------------------- /chapter_3/chap3_memoization_after.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | It works! 6 | 7 | 8 |

It works!

9 | 10 | -------------------------------------------------------------------------------- /chapter_3/index.php: -------------------------------------------------------------------------------- 1 | 1, 17 | CURLOPT_URL => 'http://www.google.ca', 18 | CURLOPT_USERAGENT => 'Faster Web cURL Request' 19 | )); 20 | 21 | $responses[] = curl_exec($curl); 22 | 23 | curl_close($curl); 24 | 25 | $i++; 26 | } 27 | 28 | $time = microtime(true) - $start; 29 | 30 | echo 'Time elapsed: ' . $time . PHP_EOL; 31 | 32 | echo memory_get_usage() . ' bytes' . PHP_EOL; -------------------------------------------------------------------------------- /chapter_4/chap4_IO_non_blocking.php: -------------------------------------------------------------------------------- 1 | 1, 21 | CURLOPT_URL => 'http://www.google.ca', 22 | CURLOPT_USERAGENT => 'Faster Web cURL Request' 23 | )); 24 | 25 | curl_multi_add_handle($multiHandle, $curlHandles[$i]); 26 | } 27 | 28 | $running = null; 29 | 30 | do { 31 | curl_multi_exec($multiHandle, $running); 32 | } while ($running); 33 | 34 | for ($i = 0; $i < 10; $i++) { 35 | curl_multi_remove_handle($multiHandle, $curlHandles[$i]); 36 | 37 | $responses[] = curl_multi_getcontent($curlHandles[$i]); 38 | } 39 | 40 | curl_multi_close($multiHandle); 41 | 42 | $time = microtime(true) - $start; 43 | 44 | echo 'Time elapsed: ' . $time . PHP_EOL; 45 | 46 | echo memory_get_usage() . ' bytes' . PHP_EOL; -------------------------------------------------------------------------------- /chapter_4/chap4_async_race.php: -------------------------------------------------------------------------------- 1 | $car) { 53 | $penalty = rand(0, 8); 54 | if($key == $penalty) { 55 | // We must advance the car pointer in order to truly apply the penalty to the "current" car 56 | $car->next(); 57 | } else { 58 | // Check if the "current" car pointer points to an active race car 59 | if($car->current() !== NULL) { 60 | // Check if the "current" car pointer points to a car that has completed a lap 61 | if($car->current() == 1) { 62 | $lapNumber = $laps[$key]++; 63 | $carNumber = $key + 1; 64 | if ($lapNumber == 10 && $flag === FALSE) { 65 | echo "*** Car $carNumber IS THE WINNER! ***\n"; 66 | $flag = TRUE; 67 | } else { 68 | echo "Car $carNumber has completed lap $lapNumber\n"; 69 | } 70 | } 71 | // Advance the car pointer 72 | $car->next(); 73 | // If the next car is no longer active, remove the car from the race 74 | if (!$car->valid()) { 75 | unset($cars[$key]); 76 | } 77 | } 78 | } 79 | } 80 | 81 | // No active cars left! The race is over! 82 | if (empty($cars)) return; 83 | } 84 | } 85 | 86 | runner(array(car1($laps[0]), car2($laps[1]), car3($laps[2])), $laps); 87 | -------------------------------------------------------------------------------- /chapter_4/chap4_pthreads.php: -------------------------------------------------------------------------------- 1 | arg = $arg; 13 | } 14 | 15 | public function run() { 16 | if ($this->arg) { 17 | $sleep = mt_rand(1, 10); 18 | printf('%s: %s -start -sleeps %d' . "\n", date("g:i:sa"), $this->arg, $sleep); 19 | sleep($sleep); 20 | printf('%s: %s -finish' . "\n", date("g:i:sa"), $this->arg); 21 | } 22 | } 23 | } 24 | 25 | $stack = array(); 26 | 27 | // Create Multiple Thread 28 | foreach ( range('1', '9') as $id ) { 29 | $stack[] = new TestThreads($id); 30 | } 31 | 32 | // Execute threads 33 | foreach ( $stack as $thread ) { 34 | $thread->start(); 35 | } 36 | 37 | sleep(1); 38 | 39 | $time = microtime(true) - $start; 40 | 41 | echo 'Time elapsed: ' . $time . PHP_EOL; 42 | 43 | echo memory_get_usage() . ' bytes' . PHP_EOL; -------------------------------------------------------------------------------- /chapter_4/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | It works! 6 | 7 | 8 |

It works!

9 | 10 | -------------------------------------------------------------------------------- /chapter_4/index.php: -------------------------------------------------------------------------------- 1 | =5.4.0" 25 | }, 26 | "require-dev": { 27 | "phpunit/phpunit": "^6.0||^5.7||^4.8.35" 28 | }, 29 | "type": "library", 30 | "extra": { 31 | "branch-alias": { 32 | "dev-master": "2.0-dev" 33 | } 34 | }, 35 | "autoload": { 36 | "psr-0": { 37 | "Evenement": "src" 38 | } 39 | }, 40 | "notification-url": "https://packagist.org/downloads/", 41 | "license": [ 42 | "MIT" 43 | ], 44 | "authors": [ 45 | { 46 | "name": "Igor Wiedler", 47 | "email": "igor@wiedler.ch" 48 | } 49 | ], 50 | "description": "Événement is a very simple event dispatching library for PHP", 51 | "keywords": [ 52 | "event-dispatcher", 53 | "event-emitter" 54 | ], 55 | "time": "2017-07-17T17:39:19+00:00" 56 | }, 57 | { 58 | "name": "guzzlehttp/psr7", 59 | "version": "1.4.2", 60 | "source": { 61 | "type": "git", 62 | "url": "https://github.com/guzzle/psr7.git", 63 | "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c" 64 | }, 65 | "dist": { 66 | "type": "zip", 67 | "url": "https://api.github.com/repos/guzzle/psr7/zipball/f5b8a8512e2b58b0071a7280e39f14f72e05d87c", 68 | "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c", 69 | "shasum": "" 70 | }, 71 | "require": { 72 | "php": ">=5.4.0", 73 | "psr/http-message": "~1.0" 74 | }, 75 | "provide": { 76 | "psr/http-message-implementation": "1.0" 77 | }, 78 | "require-dev": { 79 | "phpunit/phpunit": "~4.0" 80 | }, 81 | "type": "library", 82 | "extra": { 83 | "branch-alias": { 84 | "dev-master": "1.4-dev" 85 | } 86 | }, 87 | "autoload": { 88 | "psr-4": { 89 | "GuzzleHttp\\Psr7\\": "src/" 90 | }, 91 | "files": [ 92 | "src/functions_include.php" 93 | ] 94 | }, 95 | "notification-url": "https://packagist.org/downloads/", 96 | "license": [ 97 | "MIT" 98 | ], 99 | "authors": [ 100 | { 101 | "name": "Michael Dowling", 102 | "email": "mtdowling@gmail.com", 103 | "homepage": "https://github.com/mtdowling" 104 | }, 105 | { 106 | "name": "Tobias Schultze", 107 | "homepage": "https://github.com/Tobion" 108 | } 109 | ], 110 | "description": "PSR-7 message implementation that also provides common utility methods", 111 | "keywords": [ 112 | "http", 113 | "message", 114 | "request", 115 | "response", 116 | "stream", 117 | "uri", 118 | "url" 119 | ], 120 | "time": "2017-03-20T17:10:46+00:00" 121 | }, 122 | { 123 | "name": "psr/http-message", 124 | "version": "1.0.1", 125 | "source": { 126 | "type": "git", 127 | "url": "https://github.com/php-fig/http-message.git", 128 | "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" 129 | }, 130 | "dist": { 131 | "type": "zip", 132 | "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", 133 | "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", 134 | "shasum": "" 135 | }, 136 | "require": { 137 | "php": ">=5.3.0" 138 | }, 139 | "type": "library", 140 | "extra": { 141 | "branch-alias": { 142 | "dev-master": "1.0.x-dev" 143 | } 144 | }, 145 | "autoload": { 146 | "psr-4": { 147 | "Psr\\Http\\Message\\": "src/" 148 | } 149 | }, 150 | "notification-url": "https://packagist.org/downloads/", 151 | "license": [ 152 | "MIT" 153 | ], 154 | "authors": [ 155 | { 156 | "name": "PHP-FIG", 157 | "homepage": "http://www.php-fig.org/" 158 | } 159 | ], 160 | "description": "Common interface for HTTP messages", 161 | "homepage": "https://github.com/php-fig/http-message", 162 | "keywords": [ 163 | "http", 164 | "http-message", 165 | "psr", 166 | "psr-7", 167 | "request", 168 | "response" 169 | ], 170 | "time": "2016-08-06T14:39:51+00:00" 171 | }, 172 | { 173 | "name": "react/cache", 174 | "version": "v0.4.1", 175 | "source": { 176 | "type": "git", 177 | "url": "https://github.com/reactphp/cache.git", 178 | "reference": "558f614891341b1d817a8cdf9a358948ec49638f" 179 | }, 180 | "dist": { 181 | "type": "zip", 182 | "url": "https://api.github.com/repos/reactphp/cache/zipball/558f614891341b1d817a8cdf9a358948ec49638f", 183 | "reference": "558f614891341b1d817a8cdf9a358948ec49638f", 184 | "shasum": "" 185 | }, 186 | "require": { 187 | "php": ">=5.3.0", 188 | "react/promise": "~2.0|~1.1" 189 | }, 190 | "type": "library", 191 | "autoload": { 192 | "psr-4": { 193 | "React\\Cache\\": "src\\" 194 | } 195 | }, 196 | "notification-url": "https://packagist.org/downloads/", 197 | "license": [ 198 | "MIT" 199 | ], 200 | "description": "Async caching.", 201 | "keywords": [ 202 | "cache" 203 | ], 204 | "time": "2016-02-25T18:17:16+00:00" 205 | }, 206 | { 207 | "name": "react/child-process", 208 | "version": "v0.4.3", 209 | "source": { 210 | "type": "git", 211 | "url": "https://github.com/reactphp/child-process.git", 212 | "reference": "b81d06daaafb5d7d592b6f7f8b1b7905cdef9ac6" 213 | }, 214 | "dist": { 215 | "type": "zip", 216 | "url": "https://api.github.com/repos/reactphp/child-process/zipball/b81d06daaafb5d7d592b6f7f8b1b7905cdef9ac6", 217 | "reference": "b81d06daaafb5d7d592b6f7f8b1b7905cdef9ac6", 218 | "shasum": "" 219 | }, 220 | "require": { 221 | "evenement/evenement": "^2.0 || ^1.0", 222 | "php": ">=5.3.0", 223 | "react/event-loop": "^0.4 || ^0.3", 224 | "react/stream": "^0.5 || ^0.4.4" 225 | }, 226 | "require-dev": { 227 | "phpunit/phpunit": "^5.0 || ^4.8.10", 228 | "sebastian/environment": "~1.0" 229 | }, 230 | "type": "library", 231 | "autoload": { 232 | "psr-4": { 233 | "React\\ChildProcess\\": "src" 234 | } 235 | }, 236 | "notification-url": "https://packagist.org/downloads/", 237 | "license": [ 238 | "MIT" 239 | ], 240 | "description": "Library for executing child processes.", 241 | "keywords": [ 242 | "process" 243 | ], 244 | "time": "2017-03-14T13:23:20+00:00" 245 | }, 246 | { 247 | "name": "react/dns", 248 | "version": "v0.4.11", 249 | "source": { 250 | "type": "git", 251 | "url": "https://github.com/reactphp/dns.git", 252 | "reference": "8558bba4f2784aa997670d15fc6f7461a8eb4e53" 253 | }, 254 | "dist": { 255 | "type": "zip", 256 | "url": "https://api.github.com/repos/reactphp/dns/zipball/8558bba4f2784aa997670d15fc6f7461a8eb4e53", 257 | "reference": "8558bba4f2784aa997670d15fc6f7461a8eb4e53", 258 | "shasum": "" 259 | }, 260 | "require": { 261 | "php": ">=5.3.0", 262 | "react/cache": "~0.4.0|~0.3.0", 263 | "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5", 264 | "react/promise": "^2.1 || ^1.2.1", 265 | "react/promise-timer": "^1.2", 266 | "react/socket": "^1.0 || ^0.8 || ^0.7 || ^0.6 || ^0.5 || ^0.4.4", 267 | "react/stream": "^1.0 || ^0.7 || ^0.6 || ^0.5 || ^0.4.5" 268 | }, 269 | "require-dev": { 270 | "clue/block-react": "^1.2", 271 | "phpunit/phpunit": "^5.0 || ^4.8.10" 272 | }, 273 | "type": "library", 274 | "autoload": { 275 | "psr-4": { 276 | "React\\Dns\\": "src" 277 | } 278 | }, 279 | "notification-url": "https://packagist.org/downloads/", 280 | "license": [ 281 | "MIT" 282 | ], 283 | "description": "Async DNS resolver for ReactPHP", 284 | "keywords": [ 285 | "async", 286 | "dns", 287 | "dns-resolver", 288 | "reactphp" 289 | ], 290 | "time": "2017-08-25T08:22:48+00:00" 291 | }, 292 | { 293 | "name": "react/event-loop", 294 | "version": "v0.4.3", 295 | "source": { 296 | "type": "git", 297 | "url": "https://github.com/reactphp/event-loop.git", 298 | "reference": "8bde03488ee897dc6bb3d91e4e17c353f9c5252f" 299 | }, 300 | "dist": { 301 | "type": "zip", 302 | "url": "https://api.github.com/repos/reactphp/event-loop/zipball/8bde03488ee897dc6bb3d91e4e17c353f9c5252f", 303 | "reference": "8bde03488ee897dc6bb3d91e4e17c353f9c5252f", 304 | "shasum": "" 305 | }, 306 | "require": { 307 | "php": ">=5.4.0" 308 | }, 309 | "require-dev": { 310 | "phpunit/phpunit": "~4.8" 311 | }, 312 | "suggest": { 313 | "ext-event": "~1.0", 314 | "ext-libev": "*", 315 | "ext-libevent": ">=0.1.0" 316 | }, 317 | "type": "library", 318 | "autoload": { 319 | "psr-4": { 320 | "React\\EventLoop\\": "src" 321 | } 322 | }, 323 | "notification-url": "https://packagist.org/downloads/", 324 | "license": [ 325 | "MIT" 326 | ], 327 | "description": "Event loop abstraction layer that libraries can use for evented I/O.", 328 | "keywords": [ 329 | "asynchronous", 330 | "event-loop" 331 | ], 332 | "time": "2017-04-27T10:56:23+00:00" 333 | }, 334 | { 335 | "name": "react/http", 336 | "version": "v0.4.4", 337 | "source": { 338 | "type": "git", 339 | "url": "https://github.com/reactphp/http.git", 340 | "reference": "aac319bd789cbc7b478d42cde2d03596e97e3222" 341 | }, 342 | "dist": { 343 | "type": "zip", 344 | "url": "https://api.github.com/repos/reactphp/http/zipball/aac319bd789cbc7b478d42cde2d03596e97e3222", 345 | "reference": "aac319bd789cbc7b478d42cde2d03596e97e3222", 346 | "shasum": "" 347 | }, 348 | "require": { 349 | "evenement/evenement": "^2.0 || ^1.0", 350 | "php": ">=5.3.0", 351 | "react/socket": "^0.4", 352 | "react/stream": "^0.4.4", 353 | "ringcentral/psr7": "^1.2" 354 | }, 355 | "require-dev": { 356 | "phpunit/phpunit": "^4.8.10||^5.0" 357 | }, 358 | "type": "library", 359 | "autoload": { 360 | "psr-4": { 361 | "React\\Http\\": "src" 362 | } 363 | }, 364 | "notification-url": "https://packagist.org/downloads/", 365 | "license": [ 366 | "MIT" 367 | ], 368 | "description": "Library for building an evented http server.", 369 | "keywords": [ 370 | "http" 371 | ], 372 | "time": "2017-02-13T14:12:50+00:00" 373 | }, 374 | { 375 | "name": "react/http-client", 376 | "version": "v0.4.17", 377 | "source": { 378 | "type": "git", 379 | "url": "https://github.com/reactphp/http-client.git", 380 | "reference": "75ee8a113f156834aaabfe0055e8db531cb4892c" 381 | }, 382 | "dist": { 383 | "type": "zip", 384 | "url": "https://api.github.com/repos/reactphp/http-client/zipball/75ee8a113f156834aaabfe0055e8db531cb4892c", 385 | "reference": "75ee8a113f156834aaabfe0055e8db531cb4892c", 386 | "shasum": "" 387 | }, 388 | "require": { 389 | "evenement/evenement": "~2.0", 390 | "guzzlehttp/psr7": "^1.0", 391 | "php": ">=5.4.0", 392 | "react/dns": "0.4.*", 393 | "react/event-loop": "0.4.*", 394 | "react/promise": "~2.2", 395 | "react/socket-client": "^0.5 || ^0.4 || ^0.3", 396 | "react/stream": "0.4.*" 397 | }, 398 | "require-dev": { 399 | "phpunit/phpunit": "^5.0 || ^4.8.10" 400 | }, 401 | "type": "library", 402 | "autoload": { 403 | "psr-4": { 404 | "React\\HttpClient\\": "src" 405 | } 406 | }, 407 | "notification-url": "https://packagist.org/downloads/", 408 | "license": [ 409 | "MIT" 410 | ], 411 | "description": "Asynchronous HTTP client library.", 412 | "keywords": [ 413 | "http" 414 | ], 415 | "time": "2017-03-20T09:55:48+00:00" 416 | }, 417 | { 418 | "name": "react/promise", 419 | "version": "v2.5.1", 420 | "source": { 421 | "type": "git", 422 | "url": "https://github.com/reactphp/promise.git", 423 | "reference": "62785ae604c8d69725d693eb370e1d67e94c4053" 424 | }, 425 | "dist": { 426 | "type": "zip", 427 | "url": "https://api.github.com/repos/reactphp/promise/zipball/62785ae604c8d69725d693eb370e1d67e94c4053", 428 | "reference": "62785ae604c8d69725d693eb370e1d67e94c4053", 429 | "shasum": "" 430 | }, 431 | "require": { 432 | "php": ">=5.4.0" 433 | }, 434 | "require-dev": { 435 | "phpunit/phpunit": "~4.8" 436 | }, 437 | "type": "library", 438 | "autoload": { 439 | "psr-4": { 440 | "React\\Promise\\": "src/" 441 | }, 442 | "files": [ 443 | "src/functions_include.php" 444 | ] 445 | }, 446 | "notification-url": "https://packagist.org/downloads/", 447 | "license": [ 448 | "MIT" 449 | ], 450 | "authors": [ 451 | { 452 | "name": "Jan Sorgalla", 453 | "email": "jsorgalla@gmail.com" 454 | } 455 | ], 456 | "description": "A lightweight implementation of CommonJS Promises/A for PHP", 457 | "keywords": [ 458 | "promise", 459 | "promises" 460 | ], 461 | "time": "2017-03-25T12:08:31+00:00" 462 | }, 463 | { 464 | "name": "react/promise-timer", 465 | "version": "v1.2.0", 466 | "source": { 467 | "type": "git", 468 | "url": "https://github.com/reactphp/promise-timer.git", 469 | "reference": "3bc527fbd1201a193ab41c19b9a770d71a3514af" 470 | }, 471 | "dist": { 472 | "type": "zip", 473 | "url": "https://api.github.com/repos/reactphp/promise-timer/zipball/3bc527fbd1201a193ab41c19b9a770d71a3514af", 474 | "reference": "3bc527fbd1201a193ab41c19b9a770d71a3514af", 475 | "shasum": "" 476 | }, 477 | "require": { 478 | "php": ">=5.3", 479 | "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5", 480 | "react/promise": "~2.1|~1.2" 481 | }, 482 | "require-dev": { 483 | "phpunit/phpunit": "^5.0 || ^4.8" 484 | }, 485 | "type": "library", 486 | "autoload": { 487 | "psr-4": { 488 | "React\\Promise\\Timer\\": "src/" 489 | }, 490 | "files": [ 491 | "src/functions.php" 492 | ] 493 | }, 494 | "notification-url": "https://packagist.org/downloads/", 495 | "license": [ 496 | "MIT" 497 | ], 498 | "authors": [ 499 | { 500 | "name": "Christian Lück", 501 | "email": "christian@lueck.tv" 502 | } 503 | ], 504 | "description": "Trivial timeout implementation for Promises", 505 | "homepage": "https://github.com/react/promise-timer", 506 | "keywords": [ 507 | "async", 508 | "event-loop", 509 | "promise", 510 | "reactphp", 511 | "timeout", 512 | "timer" 513 | ], 514 | "time": "2017-08-08T16:30:19+00:00" 515 | }, 516 | { 517 | "name": "react/react", 518 | "version": "v0.4.2", 519 | "source": { 520 | "type": "git", 521 | "url": "https://github.com/reactphp/react.git", 522 | "reference": "457b6b8a16a37c11278cac0870d6d2ff911c5765" 523 | }, 524 | "dist": { 525 | "type": "zip", 526 | "url": "https://api.github.com/repos/reactphp/react/zipball/457b6b8a16a37c11278cac0870d6d2ff911c5765", 527 | "reference": "457b6b8a16a37c11278cac0870d6d2ff911c5765", 528 | "shasum": "" 529 | }, 530 | "require": { 531 | "php": ">=5.4.0", 532 | "react/cache": "0.4.*", 533 | "react/child-process": "0.4.*", 534 | "react/dns": "0.4.*", 535 | "react/event-loop": "0.4.*", 536 | "react/http": "0.4.*", 537 | "react/http-client": "0.4.*", 538 | "react/promise": "~2.1", 539 | "react/socket": "0.4.*", 540 | "react/socket-client": "0.4.*", 541 | "react/stream": "0.4.*" 542 | }, 543 | "require-dev": { 544 | "phpunit/phpunit": "~4.0" 545 | }, 546 | "suggest": { 547 | "ext-event": "Allows for use of a more performant event-loop implementation.", 548 | "ext-libev": "Allows for use of a more performant event-loop implementation.", 549 | "ext-libevent": "Allows for use of a more performant event-loop implementation." 550 | }, 551 | "type": "library", 552 | "extra": { 553 | "branch-alias": { 554 | "dev-master": "0.5-dev" 555 | } 556 | }, 557 | "notification-url": "https://packagist.org/downloads/", 558 | "license": [ 559 | "MIT" 560 | ], 561 | "description": "Nuclear Reactor written in PHP.", 562 | "keywords": [ 563 | "asynchronous", 564 | "event-loop", 565 | "reactor" 566 | ], 567 | "time": "2014-12-11T02:06:55+00:00" 568 | }, 569 | { 570 | "name": "react/socket", 571 | "version": "v0.4.6", 572 | "source": { 573 | "type": "git", 574 | "url": "https://github.com/reactphp/socket.git", 575 | "reference": "cf074e53c974df52388ebd09710a9018894745d2" 576 | }, 577 | "dist": { 578 | "type": "zip", 579 | "url": "https://api.github.com/repos/reactphp/socket/zipball/cf074e53c974df52388ebd09710a9018894745d2", 580 | "reference": "cf074e53c974df52388ebd09710a9018894745d2", 581 | "shasum": "" 582 | }, 583 | "require": { 584 | "evenement/evenement": "~2.0|~1.0", 585 | "php": ">=5.3.0", 586 | "react/event-loop": "0.4.*|0.3.*", 587 | "react/promise": "^2.0 || ^1.1", 588 | "react/stream": "^0.4.5" 589 | }, 590 | "require-dev": { 591 | "clue/block-react": "^1.1", 592 | "phpunit/phpunit": "~4.8", 593 | "react/socket-client": "^0.5.1" 594 | }, 595 | "type": "library", 596 | "autoload": { 597 | "psr-4": { 598 | "React\\Socket\\": "src" 599 | } 600 | }, 601 | "notification-url": "https://packagist.org/downloads/", 602 | "license": [ 603 | "MIT" 604 | ], 605 | "description": "Async, streaming plaintext TCP/IP and secure TLS socket server for React PHP", 606 | "keywords": [ 607 | "Socket" 608 | ], 609 | "time": "2017-01-26T09:23:38+00:00" 610 | }, 611 | { 612 | "name": "react/socket-client", 613 | "version": "v0.4.6", 614 | "source": { 615 | "type": "git", 616 | "url": "https://github.com/reactphp/socket-client.git", 617 | "reference": "49e730523b73d912e56f7a41f53ed3fc083ae167" 618 | }, 619 | "dist": { 620 | "type": "zip", 621 | "url": "https://api.github.com/repos/reactphp/socket-client/zipball/49e730523b73d912e56f7a41f53ed3fc083ae167", 622 | "reference": "49e730523b73d912e56f7a41f53ed3fc083ae167", 623 | "shasum": "" 624 | }, 625 | "require": { 626 | "php": ">=5.4.0", 627 | "react/dns": "0.4.*", 628 | "react/event-loop": "0.4.*", 629 | "react/promise": "~2.0", 630 | "react/stream": "0.4.*" 631 | }, 632 | "type": "library", 633 | "extra": { 634 | "branch-alias": { 635 | "dev-master": "0.4-dev" 636 | } 637 | }, 638 | "autoload": { 639 | "psr-4": { 640 | "React\\SocketClient\\": "src" 641 | } 642 | }, 643 | "notification-url": "https://packagist.org/downloads/", 644 | "license": [ 645 | "MIT" 646 | ], 647 | "description": "Async connector to open TCP/IP and SSL/TLS based connections.", 648 | "keywords": [ 649 | "Socket" 650 | ], 651 | "time": "2016-12-06T10:54:49+00:00" 652 | }, 653 | { 654 | "name": "react/stream", 655 | "version": "v0.4.6", 656 | "source": { 657 | "type": "git", 658 | "url": "https://github.com/reactphp/stream.git", 659 | "reference": "44dc7f51ea48624110136b535b9ba44fd7d0c1ee" 660 | }, 661 | "dist": { 662 | "type": "zip", 663 | "url": "https://api.github.com/repos/reactphp/stream/zipball/44dc7f51ea48624110136b535b9ba44fd7d0c1ee", 664 | "reference": "44dc7f51ea48624110136b535b9ba44fd7d0c1ee", 665 | "shasum": "" 666 | }, 667 | "require": { 668 | "evenement/evenement": "^2.0|^1.0", 669 | "php": ">=5.3.8" 670 | }, 671 | "require-dev": { 672 | "clue/stream-filter": "~1.2", 673 | "react/event-loop": "^0.4|^0.3", 674 | "react/promise": "^2.0|^1.0" 675 | }, 676 | "suggest": { 677 | "react/event-loop": "^0.4", 678 | "react/promise": "^2.0" 679 | }, 680 | "type": "library", 681 | "autoload": { 682 | "psr-4": { 683 | "React\\Stream\\": "src" 684 | } 685 | }, 686 | "notification-url": "https://packagist.org/downloads/", 687 | "license": [ 688 | "MIT" 689 | ], 690 | "description": "Basic readable and writable stream interfaces that support piping.", 691 | "keywords": [ 692 | "pipe", 693 | "stream" 694 | ], 695 | "time": "2017-01-25T14:44:14+00:00" 696 | }, 697 | { 698 | "name": "ringcentral/psr7", 699 | "version": "1.2.1", 700 | "source": { 701 | "type": "git", 702 | "url": "https://github.com/ringcentral/psr7.git", 703 | "reference": "2594fb47cdc659f3fcf0aa1559b7355460555303" 704 | }, 705 | "dist": { 706 | "type": "zip", 707 | "url": "https://api.github.com/repos/ringcentral/psr7/zipball/2594fb47cdc659f3fcf0aa1559b7355460555303", 708 | "reference": "2594fb47cdc659f3fcf0aa1559b7355460555303", 709 | "shasum": "" 710 | }, 711 | "require": { 712 | "php": ">=5.3", 713 | "psr/http-message": "~1.0" 714 | }, 715 | "provide": { 716 | "psr/http-message-implementation": "1.0" 717 | }, 718 | "require-dev": { 719 | "phpunit/phpunit": "~4.0" 720 | }, 721 | "type": "library", 722 | "extra": { 723 | "branch-alias": { 724 | "dev-master": "1.0-dev" 725 | } 726 | }, 727 | "autoload": { 728 | "psr-4": { 729 | "RingCentral\\Psr7\\": "src/" 730 | }, 731 | "files": [ 732 | "src/functions_include.php" 733 | ] 734 | }, 735 | "notification-url": "https://packagist.org/downloads/", 736 | "license": [ 737 | "MIT" 738 | ], 739 | "authors": [ 740 | { 741 | "name": "Michael Dowling", 742 | "email": "mtdowling@gmail.com", 743 | "homepage": "https://github.com/mtdowling" 744 | } 745 | ], 746 | "description": "PSR-7 message implementation", 747 | "keywords": [ 748 | "http", 749 | "message", 750 | "stream", 751 | "uri" 752 | ], 753 | "time": "2016-03-25T17:36:49+00:00" 754 | } 755 | ], 756 | "packages-dev": [], 757 | "aliases": [], 758 | "minimum-stability": "stable", 759 | "stability-flags": [], 760 | "prefer-stable": false, 761 | "prefer-lowest": false, 762 | "platform": [], 763 | "platform-dev": [] 764 | } 765 | -------------------------------------------------------------------------------- /chapter_4/react/composer.phar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_4/react/composer.phar -------------------------------------------------------------------------------- /chapter_4/react/examples/child-child.php: -------------------------------------------------------------------------------- 1 | on('exit', function($exitCode, $termSignal) { 10 | echo "Child exit\n"; 11 | }); 12 | 13 | $loop->addTimer(0.001, function($timer) use ($process) { 14 | $process->start($timer->getLoop()); 15 | 16 | $process->stdout->on('data', function($output) { 17 | echo "Child script says: {$output}"; 18 | }); 19 | }); 20 | 21 | $loop->addPeriodicTimer(5, function($timer) { 22 | echo "Parent cannot be blocked by child\n"; 23 | }); 24 | 25 | $loop->run(); 26 | -------------------------------------------------------------------------------- /chapter_4/react/examples/dns-resolver.php: -------------------------------------------------------------------------------- 1 | create('8.8.8.8', $loop); 10 | 11 | $domain = 'igor.io'; 12 | 13 | $dns 14 | ->resolve($domain) 15 | ->then(function ($ip) { 16 | echo "Host: $ip\n"; 17 | }, function ($e) { 18 | echo "Error: {$e->getMessage()}\n"; 19 | }); 20 | 21 | echo "Resolving domain $domain...\n"; 22 | 23 | $loop->run(); 24 | -------------------------------------------------------------------------------- /chapter_4/react/examples/echo-server.php: -------------------------------------------------------------------------------- 1 | on('connection', function ($conn) { 11 | $conn->pipe($conn); 12 | }); 13 | 14 | echo "Socket server listening on port 4000.\n"; 15 | echo "You can connect to it by running: telnet localhost 4000\n"; 16 | 17 | $socket->listen(4000); 18 | $loop->run(); 19 | -------------------------------------------------------------------------------- /chapter_4/react/examples/http-client.php: -------------------------------------------------------------------------------- 1 | createCached('8.8.8.8', $loop); 11 | 12 | $factory = new React\HttpClient\Factory(); 13 | $client = $factory->create($loop, $dnsResolver); 14 | 15 | $request = $client->request('GET', 'https://api.github.com/repos/reactphp/react/commits'); 16 | $request->on('response', function ($response) { 17 | $buffer = ''; 18 | 19 | $response->on('data', function ($data) use (&$buffer) { 20 | $buffer .= $data; 21 | echo "."; 22 | }); 23 | 24 | $response->on('end', function () use (&$buffer) { 25 | $decoded = json_decode($buffer, true); 26 | $latest = $decoded[0]['commit']; 27 | $author = $latest['author']['name']; 28 | $date = date('F j, Y', strtotime($latest['author']['date'])); 29 | 30 | echo "\n"; 31 | echo "Latest commit on react was done by {$author} on {$date}\n"; 32 | echo "{$latest['message']}\n"; 33 | }); 34 | }); 35 | $request->on('end', function ($error, $response) { 36 | echo $error; 37 | }); 38 | $request->end(); 39 | 40 | $loop->run(); 41 | -------------------------------------------------------------------------------- /chapter_4/react/examples/next-tick.php: -------------------------------------------------------------------------------- 1 | addTimer( 28 | 0, 29 | function () { 30 | echo 'timer' . PHP_EOL; 31 | } 32 | ); 33 | 34 | $loop->nextTick( 35 | function ($loop) { 36 | echo 'next-tick #1' . PHP_EOL; 37 | 38 | $loop->nextTick( 39 | function () { 40 | echo 'next-tick #2' . PHP_EOL; 41 | } 42 | ); 43 | } 44 | ); 45 | 46 | $loop->futureTick( 47 | function ($loop) { 48 | echo 'future-tick #1' . PHP_EOL; 49 | 50 | $loop->futureTick( 51 | function () { 52 | echo 'future-tick #2' . PHP_EOL; 53 | } 54 | ); 55 | } 56 | ); 57 | 58 | $loop->run(); 59 | -------------------------------------------------------------------------------- /chapter_4/react/examples/parallel-download.php: -------------------------------------------------------------------------------- 1 | 'http://nodejs.org/dist/v0.6.18/node-v0.6.18.tar.gz', 21 | 'php-5.5.15.tar.gz' => 'http://it.php.net/get/php-5.5.15.tar.gz/from/this/mirror', 22 | ); 23 | 24 | foreach ($files as $file => $url) { 25 | 26 | $readStream = fopen($url, 'r'); 27 | $writeStream = fopen($file, 'w'); 28 | 29 | stream_set_blocking($readStream, 0); 30 | stream_set_blocking($writeStream, 0); 31 | 32 | $read = new React\Stream\Stream($readStream, $loop); 33 | $write = new React\Stream\Stream($writeStream, $loop); 34 | 35 | $read->on('end', function () use ($file, &$files) { 36 | unset($files[$file]); 37 | echo "Finished downloading $file\n"; 38 | }); 39 | 40 | $read->pipe($write); 41 | 42 | } 43 | 44 | $loop->addPeriodicTimer(5, function ($timer) use (&$files) { 45 | if (0 === count($files)) { 46 | $timer->cancel(); 47 | } 48 | 49 | foreach ($files as $file => $url) { 50 | 51 | $mbytes = filesize($file) / (1024 * 1024); 52 | $formatted = number_format($mbytes, 3); 53 | echo "$file: $formatted MiB\n"; 54 | 55 | } 56 | }); 57 | 58 | echo "This script will show the download status every 5 seconds.\n"; 59 | 60 | $loop->run(); 61 | 62 | $time = microtime(true) - $start; 63 | 64 | echo 'Time elapsed: ' . $time . PHP_EOL; 65 | 66 | echo memory_get_usage() . ' bytes' . PHP_EOL; -------------------------------------------------------------------------------- /chapter_4/react/examples/pump-tons-of-data.php: -------------------------------------------------------------------------------- 1 | on('connection', function ($conn) { 14 | $shitload = str_repeat('a', 1024*1024*32); 15 | $conn->write($shitload); 16 | $conn->end(); 17 | }); 18 | 19 | echo "Socket server listening on port 4000.\n"; 20 | echo "You can connect to it by running: telnet localhost 4000\n"; 21 | 22 | $socket->listen(4000); 23 | $loop->run(); 24 | -------------------------------------------------------------------------------- /chapter_4/react/examples/scalability.php: -------------------------------------------------------------------------------- 1 | addReadStream($s, function ($s) use ($i) { 19 | $c = stream_socket_accept($s); 20 | $len = strlen($i) + 4; 21 | fwrite($c,"HTTP/1.1 200 OK\r\nContent-Length: $len\r\n\r\nHi:$i\n"); 22 | echo "Served on port 800$i\n"; 23 | }); 24 | 25 | } 26 | 27 | echo "Access your brand new HTTP server on 127.0.0.1:800x. Replace x with any number from 0-9\n"; 28 | 29 | $loop->run(); 30 | -------------------------------------------------------------------------------- /chapter_4/react/examples/tcp-chat.php: -------------------------------------------------------------------------------- 1 | on('connection', function ($conn) use ($conns) { 19 | $conns->attach($conn); 20 | 21 | $conn->on('data', function ($data) use ($conns, $conn) { 22 | foreach ($conns as $current) { 23 | 24 | if ($conn === $current) { 25 | continue; 26 | } 27 | 28 | $current->write($conn->getRemoteAddress().': '); 29 | $current->write($data); 30 | } 31 | 32 | }); 33 | 34 | $conn->on('end', function () use ($conns, $conn) { 35 | $conns->detach($conn); 36 | }); 37 | }); 38 | 39 | echo "Socket server listening on port 4000.\n"; 40 | echo "You can connect to it by running: telnet localhost 4000\n"; 41 | 42 | $socket->listen(4000); 43 | $loop->run(); 44 | -------------------------------------------------------------------------------- /chapter_4/react/examples/test-close-conn.php: -------------------------------------------------------------------------------- 1 | on('connection', function ($conn) use (&$i, $loop) { 10 | $i++; 11 | 12 | $conn->on('end', function () use (&$i) { 13 | $i--; 14 | }); 15 | }); 16 | 17 | $loop->addPeriodicTimer(2, function () use (&$i) { 18 | echo "$i open connections?\n"; 19 | }); 20 | 21 | $socket->listen(8080); 22 | $loop->run(); 23 | -------------------------------------------------------------------------------- /chapter_4/react/examples/test-memory-http.php: -------------------------------------------------------------------------------- 1 | on('request', function ($request, $response) use (&$i) { 16 | $i++; 17 | $response->writeHead(); 18 | $response->end("Hello World!\n"); 19 | }); 20 | 21 | $loop->addPeriodicTimer(2, function () use (&$i) { 22 | $kmem = memory_get_usage(true) / 1024; 23 | echo "Request: $i\n"; 24 | echo "Memory: $kmem KiB\n"; 25 | }); 26 | 27 | $socket->listen(8080); 28 | $loop->run(); 29 | -------------------------------------------------------------------------------- /chapter_4/react/examples/test-memory.php: -------------------------------------------------------------------------------- 1 | addPeriodicTimer(0.001, function () use (&$i, $loop) { 10 | $i++; 11 | 12 | // $loop->addTimer(1, function ($timer) { 13 | // }); 14 | 15 | $loop->addPeriodicTimer(1, function ($timer) { 16 | $timer->cancel(); 17 | }); 18 | }); 19 | 20 | $loop->addPeriodicTimer(2, function () use (&$i) { 21 | $kmem = memory_get_usage(true) / 1024; 22 | echo "Run: $i\n"; 23 | echo "Memory: $kmem KiB\n"; 24 | }); 25 | 26 | $loop->run(); 27 | -------------------------------------------------------------------------------- /chapter_5/dbt2-0.37.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-the-Faster-Web-with-PHP-MySQL-and-JavaScript/e388caf023328048f90abb11263d8bd69c8b745e/chapter_5/dbt2-0.37.tar.gz -------------------------------------------------------------------------------- /chapter_5/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | It works! 6 | 7 | 8 |

It works!

9 | 10 | -------------------------------------------------------------------------------- /chapter_5/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | It works! 6 | 7 | 8 |

It works!

9 | 10 | -------------------------------------------------------------------------------- /chapter_6/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | JS Loops 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /chapter_7/chap7_js_loops_2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | JS Loops 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /chapter_7/chap7_js_variables_1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | JS Variables 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /chapter_7/chap7_js_variables_2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | JS Variables 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /chapter_7/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | It works! 6 | 7 | 8 |

It works!

9 | 10 | -------------------------------------------------------------------------------- /chapter_7/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | JS Performance 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /chapter_8/chap8_js_performance_1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | JS Performance 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /chapter_8/chap8_js_performance_2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | JS Performance 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /chapter_8/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | It works! 6 | 7 | 8 |

It works!

9 | 10 | -------------------------------------------------------------------------------- /chapter_8/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | It works! 6 | 7 | 8 |

It works!

9 | 10 | -------------------------------------------------------------------------------- /chapter_9/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | It works! 6 | 7 | 8 |

It works!

9 | 10 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 |