Taming the tiger - pnwphp
-
Upload
elizabeth-smith -
Category
Internet
-
view
84 -
download
0
Transcript of Taming the tiger - pnwphp
![Page 1: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/1.jpg)
TAMING THE TIGERDEALING WITH PHYSICS IN PROGRAMMING
![Page 2: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/2.jpg)
THE LAWS OF PHYSICS ALWAYS APPLY
• CPUS use electricity and produce heat
• Even computers “in the cloud” are on physical hardware
• There is a limit to the amount of throughput a nic can push
• The larger the data the longer it takes to move it, and the more surface it takes to store it
![Page 3: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/3.jpg)
ARRAYS ARE EVIL
• There are other ways to store data that are more efficient
• They should be used for small numbers of data
• No matter how hard you try, there is C overhead
![Page 4: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/4.jpg)
USE THE ITERATION, LUKE
• Lazy fetching exists for database fetching – use it!
• Always page (window) your result sets from the database – ALWAYS
• Use filters or generators to format or alter results on the fly
![Page 5: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/5.jpg)
STREAM YOUR DATA
• Work on chunks at a time
• Seek back and forth through data if necessary
• Use PHP streams as they were meant to be used
![Page 6: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/6.jpg)
STREAMS: COMPUTING CONCEPT
Definitions• Idea originating in 1950’s
• Standard way to get Input and Output
• A source or sink of data
Who uses them• C – stdin, stderr, stdout
• C++ iostream
• Perl IO
• Python io
• Java
• C#
![Page 7: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/7.jpg)
WHAT IS A STREAM?
• Access input and output generically
• Can write and read linearly
• May or may not be seekable
• Comes in chunks of data
![Page 8: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/8.jpg)
WHAT USES STREAMS?
• EVERYTHING
• include/require _once
• stream functions
• file system functions
• many other extensions
![Page 9: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/9.jpg)
ALL IO
Attach Context
Stream Transport
Stream Filter
Stream Wrapper
HOW PHP STREAMS WORK
![Page 10: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/10.jpg)
USING STREAMS
![Page 11: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/11.jpg)
WHAT ARE FILTERS?
• Performs operations on stream data
• Can be prepended or appended (even on the fly)
• Can be attached to read or write
• When a filter is added for read and write, two instances of the filter are created.
![Page 12: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/12.jpg)
USING FILTERS
![Page 13: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/13.jpg)
THINGS TO WATCH FOR!
• Data has an input and output state
• When reading in chunks, you may need to cache in between reads to make filters useful
• Use the right tool for the job
![Page 14: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/14.jpg)
PROCESS WITH THE APPROPRIATE TOOLS
• Load data into the appropriate place for processing
• Hint – arrays are IN MEMORY – that is generally not an appropriate place for processing
• Datastores are meant for storing and retrieving data, use them
![Page 15: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/15.jpg)
OFFLOAD WORK
• Put work items in queues and inform the user when they’re completed
• It’s not realistic to expect complex reports to be done in seconds, physics apply here too
• Caching complex work items is a good way to balance offloaded work with immediate results
![Page 16: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/16.jpg)
COMMUNICATE WITH OTHER PROCESSES
• Microservices are in essence jobbed systems communicated via http
• You can overload them to work via unix sockets as well
• Rachet or other websockets solutions allow for heavy work with multiplexed communication
• PHP can run in daemons, and even listen and communicate over sockets
![Page 17: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/17.jpg)
NETWORK SOCKET TYPES
• Stream
• Connection oriented (tcp)
• Datagram
• Connectionless (udp)
• Raw
• Low level protocols
![Page 18: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/18.jpg)
DEFINITIONS
• Socket• Bidirectional network stream that speaks a protocol
• Transport• Tells a network stream how to communicate
• Wrapper
• Tells a stream how to handle specific protocols and encodings
![Page 19: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/19.jpg)
USING SOCKETS
![Page 20: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/20.jpg)
THAT SOCKETS EXTENSION…
• New APIS in streams and filesystem functions are replacements
• Extension is very low level
• stream_socket_server
• stream_socket_client
![Page 21: Taming the tiger - pnwphp](https://reader030.fdocuments.us/reader030/viewer/2022032710/589c0ff81a28ab007e8b46df/html5/thumbnails/21.jpg)
About Me
http://emsmith.net
twitter - @auroraeosrose
IRC – freenode – auroraeosrose
#phpmentoring
https://joind.in/talk/f88ef