process any amounts of data. any time - php benelux 2013

48
Process any amounts of data. Any time Juozas Kaziukėnas // juokaz.com // @juokaz

Upload: juozas-kaziukenas

Post on 18-Nov-2014

5.228 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Process any amounts of data. Any time - PHP Benelux 2013

Process any amounts of data. Any time

Juozas Kaziukėnas // juokaz.com // @juokaz

Page 2: Process any amounts of data. Any time - PHP Benelux 2013

Juozas Kaziukėnas, Lithuanian

You can call me Joe

More info http://juokaz.com

Page 3: Process any amounts of data. Any time - PHP Benelux 2013

Last year at PHPBNL12

Page 4: Process any amounts of data. Any time - PHP Benelux 2013

Planking competition

Page 5: Process any amounts of data. Any time - PHP Benelux 2013

The problem

Page 6: Process any amounts of data. Any time - PHP Benelux 2013

CRON

Page 7: Process any amounts of data. Any time - PHP Benelux 2013

PHP developers are lazy

Page 8: Process any amounts of data. Any time - PHP Benelux 2013
Page 9: Process any amounts of data. Any time - PHP Benelux 2013

Code should be lazy

Page 10: Process any amounts of data. Any time - PHP Benelux 2013

file_get_contens

Page 11: Process any amounts of data. Any time - PHP Benelux 2013

Buffering

Page 12: Process any amounts of data. Any time - PHP Benelux 2013

Wait

Page 13: Process any amounts of data. Any time - PHP Benelux 2013

Memory usage

Page 14: Process any amounts of data. Any time - PHP Benelux 2013

Saw graph

Page 15: Process any amounts of data. Any time - PHP Benelux 2013

Memory vs performance

Page 16: Process any amounts of data. Any time - PHP Benelux 2013

Reading files

Page 17: Process any amounts of data. Any time - PHP Benelux 2013

Line-by-line reading

$file_handle = fopen("myfile", "r");

while (!feof($file_handle)) {

$line = fgets($file_handle);

echo $line;

}

fclose($file_handle);

Page 18: Process any amounts of data. Any time - PHP Benelux 2013

Line-by-line processing

$file_handle = fopen("myfile", "r");

while (!feof($file_handle)) {

$line = fgets($file_handle);

process($line);

}

fclose($file_handle);

Page 19: Process any amounts of data. Any time - PHP Benelux 2013

XML

Page 20: Process any amounts of data. Any time - PHP Benelux 2013

Read XML files

Page 21: Process any amounts of data. Any time - PHP Benelux 2013

$z = new XMLReader;$z->open('data.xml');

$doc = new DOMDocument;

// move to the first <product /> nodewhile ($z->read() && $z->name !== 'product');

// now that we're at the right depth, hop to the next <product/> until the end of the treewhile ($z->name === 'product'){ // either one should work //$node = new SimpleXMLElement($z->readOuterXML()); $node = simplexml_import_dom($doc->importNode($z->expand(), true));

// now you can use $node without going insane about parsing var_dump($node->element_1);

// go to next <product /> $z->next('product');}

Page 22: Process any amounts of data. Any time - PHP Benelux 2013

$z = new XMLReader;$z->open('data.xml');

$doc = new DOMDocument;

// move to the first <product /> nodewhile ($z->read() && $z->name !== 'product');

// now that we're at the right depth, hop to the next <product/> until the end of the treewhile ($z->name === 'product'){ // either one should work //$node = new SimpleXMLElement($z->readOuterXML()); $node = simplexml_import_dom($doc->importNode($z->expand(), true));

// now you can use $node without going insane about parsing var_dump($node->element_1);

// go to next <product /> $z->next('product');}

Page 23: Process any amounts of data. Any time - PHP Benelux 2013

Running processes

Page 24: Process any amounts of data. Any time - PHP Benelux 2013

Pipes

Page 25: Process any amounts of data. Any time - PHP Benelux 2013

Linux

wget http://example.com/lol.xml.gz

gunzip lol.xml.gz

Page 26: Process any amounts of data. Any time - PHP Benelux 2013

Linux

wget -O- http://example.com/lol.xml.gz | gunzip

Page 27: Process any amounts of data. Any time - PHP Benelux 2013

$command = "wget -O- $url | gunzip";

$process = proc_open($command, array( array("pipe","r"), array("pipe","w"), array("pipe","w") ),$pipes);

while(!feof($pipes[1])) { $buffer .= fgets($pipes[1], 128); if (strpos($buffer, '<Item>')) {

// detected start of the item } if (strpos($buffer, '</Item>')) {

// detected end of the item }}fclose($pipes[1]);proc_close($process);

Page 28: Process any amounts of data. Any time - PHP Benelux 2013

Reading from MySQL

Page 29: Process any amounts of data. Any time - PHP Benelux 2013

mysql_unbuffered_query

$lh = mysql_connect( 'server', 'uname', 'pword' );

$qry = "SELECT * FROM my_bigass_table";

$rh = mysql_unbuffered_query( $qry, $lh );

while ( $res = mysql_fetch_row( $rh ) )

{

process($res);

}

Page 30: Process any amounts of data. Any time - PHP Benelux 2013

Outputting data

Page 31: Process any amounts of data. Any time - PHP Benelux 2013

PHP

$result = '';

foreach ($data as $item) {

$line = 'Name ' . $item['name'] . PHP_EOL;

$result .= $line;

}

echo $result;

Page 32: Process any amounts of data. Any time - PHP Benelux 2013

PHP

$result = '';

ob_end_clean();

foreach ($data as $item) {

echo 'Name ' . $item['name'] . PHP_EOL;

flush();

}

Page 33: Process any amounts of data. Any time - PHP Benelux 2013

PHP 5.5

Page 34: Process any amounts of data. Any time - PHP Benelux 2013

Generators

Page 35: Process any amounts of data. Any time - PHP Benelux 2013
Page 36: Process any amounts of data. Any time - PHP Benelux 2013

Yield

Page 37: Process any amounts of data. Any time - PHP Benelux 2013

Line-by-line processing

$file_handle = fopen("myfile", "r");

while (!feof($file_handle)) {

$line = fgets($file_handle);

process($line);

}

fclose($file_handle);

Page 38: Process any amounts of data. Any time - PHP Benelux 2013

Line-by-line processing

function read() { $file_handle = fopen("myfile", "r"); while (!feof($file_handle)) { $line = fgets($file_handle); yield $line; } fclose($file_handle);}

$data = read();foreach ($data as $line) {}

Page 39: Process any amounts of data. Any time - PHP Benelux 2013

Making HTTP requests

Page 40: Process any amounts of data. Any time - PHP Benelux 2013

Open connections limit

Page 41: Process any amounts of data. Any time - PHP Benelux 2013

Connection Sharing with CURL in PHP: How to re-use HTTP connections to knock 70% off REST network time.http://technosophos.com/content/connection-sharing-curl-php-how-re-use-http-connections-knock-70-

Page 42: Process any amounts of data. Any time - PHP Benelux 2013

Next steps

Page 43: Process any amounts of data. Any time - PHP Benelux 2013

Gearman

Page 44: Process any amounts of data. Any time - PHP Benelux 2013

Hadoop

Page 45: Process any amounts of data. Any time - PHP Benelux 2013

Why PHP?

Page 46: Process any amounts of data. Any time - PHP Benelux 2013

What’s possible

Page 47: Process any amounts of data. Any time - PHP Benelux 2013

Using it for good

Page 48: Process any amounts of data. Any time - PHP Benelux 2013

Thanks