Thursday, November 24, 2011

ZMQ socket creation test

Working with Harish aka ZMQ guru :)

We needed to use ZMQ for a lot of backend queueing operations from the serving PHP code, I started inspecting the ZMQ-PHP bindings (docs : http://php.zero.mq/ ) .

On close inspections we found that the sockets do not persist over multiple PHP requests*** but persist for multiple requests to the queue within the same request. So we have to make new sockets equal to the different queues we will be connecting for every request. To test whether this is optimal, Harish suggested a test as follows : 

<?php
for($i = 0; $i < 10000; $i ++) {
    
    $pid = pcntl_fork ();
    if ($pid == - 1) {
        
        die ( 'Fork failed' );
        break;
        
    } elseif ($pid == 0) {
        
        $context = new ZMQContext ();
        $socket = new ZMQSocket ( $context, ZMQ::SOCKET_PUSH, 'mySock' );
        
        $socket->setSockOpt ( ZMQ::SOCKOPT_HWM, 5 );
        
        $dsn = "tcp://1.1.1.1:22222";
        
        $socket->connect ( $dsn );
        
        break;
        
    } else {
        pcntl_waitpid ( $pid, $status );
    }
}
?>

We timed it using :

jknair@linuxbox:~/Projects/queue$ time php zmqFork.php 

real 1m32.174s
user 0m26.186s
sys 0m49.355s

and then without the ZMQ stuff :

<?php
for($i = 0; $i < 10000; $i ++) {
    
    $pid = pcntl_fork ();
    if ($pid == - 1) {
        
        die ( 'Fork failed' );
        break;
        
    } elseif ($pid == 0) {  

        break;
        
    } else {
        pcntl_waitpid ( $pid, $status );
    }
}
?>

After timing it :

jknair@linuxbox:~/Projects/queue$ time php zmqFork.php 

real 1m23.990s
user 0m10.629s
sys 0m37.450s

Approximately 10 seconds for 10000 ZMQ sockets.
1ms for a ZMQ socket !!! No problemo :) 


***Update : the sockets persist within the same preforked apache/nginx process that can server multiple php requests but do  not persist across mutiple processes 

No comments:

Post a Comment