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!
9 | 10 | -------------------------------------------------------------------------------- /chapter_1/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |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.,';
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+/', '
';
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!
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!
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!
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!
9 | 10 | -------------------------------------------------------------------------------- /chapter_5/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |It works!
9 | 10 | -------------------------------------------------------------------------------- /chapter_6/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |It works!
9 | 10 | -------------------------------------------------------------------------------- /chapter_7/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |It works!
9 | 10 | -------------------------------------------------------------------------------- /chapter_8/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |It works!
9 | 10 | -------------------------------------------------------------------------------- /chapter_9/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |It works!
9 | 10 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 |