[WebSocket] Performance

Added unit tests for Rsv bit checks
Using binary operators to check bits resulting in massive performance gains
This commit is contained in:
Chris Boden 2012-07-13 11:38:16 -04:00
parent 2d9967b066
commit a2188a449e
3 changed files with 32 additions and 28 deletions

View File

@ -15,7 +15,7 @@ interface FrameInterface extends DataInterface {
function isFinal(); function isFinal();
/** /**
* Was the payload masked? * Is the payload masked?
* @return bool * @return bool
*/ */
function isMasked(); function isMasked();

View File

@ -127,9 +127,7 @@ class Frame implements FrameInterface {
throw new \UnderflowException('Not enough bytes received to determine if this is the final frame in message'); throw new \UnderflowException('Not enough bytes received to determine if this is the final frame in message');
} }
$fbb = sprintf('%08b', ord(substr($this->data, 0, 1))); return 128 === (ord($this->data[0]) & 128);
return (boolean)(int)$fbb[0];
} }
/** /**
@ -141,9 +139,7 @@ class Frame implements FrameInterface {
throw new \UnderflowException('Not enough bytes received to determine reserved bit'); throw new \UnderflowException('Not enough bytes received to determine reserved bit');
} }
$fbb = sprintf('%08b', ord(substr($this->data, 0, 1))); return 64 === (ord($this->data[0]) & 64);
return (boolean)(int)$fbb[1];
} }
/** /**
@ -155,9 +151,7 @@ class Frame implements FrameInterface {
throw new \UnderflowException('Not enough bytes received to determine reserved bit'); throw new \UnderflowException('Not enough bytes received to determine reserved bit');
} }
$fbb = sprintf('%08b', ord(substr($this->data, 0, 1))); return 32 === (ord($this->data[0]) & 32);
return (boolean)(int)$fbb[2];
} }
/** /**
@ -169,9 +163,7 @@ class Frame implements FrameInterface {
throw new \UnderflowException('Not enough bytes received to determine reserved bit'); throw new \UnderflowException('Not enough bytes received to determine reserved bit');
} }
$fbb = sprintf('%08b', ord(substr($this->data, 0, 1))); return 16 == (ord($this->data[0]) & 16);
return (boolean)(int)$fbb[3];
} }
/** /**
@ -182,7 +174,7 @@ class Frame implements FrameInterface {
throw new \UnderflowException("Not enough bytes received ({$this->bytesRecvd}) to determine if mask is set"); throw new \UnderflowException("Not enough bytes received ({$this->bytesRecvd}) to determine if mask is set");
} }
return (boolean)bindec(substr(sprintf('%08b', ord(substr($this->data, 1, 1))), 0, 1)); return 128 === (ord($this->data[1]) & 128);
} }
/** /**

View File

@ -66,22 +66,42 @@ class FrameTest extends \PHPUnit_Framework_TestCase {
*/ */
public static function firstByteProvider() { public static function firstByteProvider() {
return array( return array(
array(false, 8, '00001000') array(false, false, false, true, 8, '00011000')
, array(true, 10, '10001010') , array(true, false, true, false, 10, '10101010')
, array(false, 15, '00001111') , array(false, false, false, false, 15, '00001111')
, array(true, 1, '10000001') , array(true, false, false, false, 1, '10000001')
, array(true, 15, '11111111') , array(true, true, true, true, 15, '11111111')
, array(true, true, false, false, 7, '11000111')
); );
} }
/** /**
* @dataProvider firstByteProvider * @dataProvider firstByteProvider
*/ */
public function testFinCodeFromBits($fin, $opcode, $bin) { public function testFinCodeFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) {
$this->_frame->addBuffer(Frame::encode($bin)); $this->_frame->addBuffer(Frame::encode($bin));
$this->assertEquals($fin, $this->_frame->isFinal()); $this->assertEquals($fin, $this->_frame->isFinal());
} }
/**
* @dataProvider firstByteProvider
*/
public function testGetRsvFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) {
$this->_frame->addBuffer(Frame::encode($bin));
$this->assertEquals($rsv1, $this->_frame->getRsv1());
$this->assertEquals($rsv2, $this->_frame->getRsv2());
$this->assertEquals($rsv3, $this->_frame->getRsv3());
}
/**
* @dataProvider firstByteProvider
*/
public function testOpcodeFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) {
$this->_frame->addBuffer(Frame::encode($bin));
$this->assertEquals($opcode, $this->_frame->getOpcode());
}
/** /**
* @dataProvider UnframeMessageProvider * @dataProvider UnframeMessageProvider
*/ */
@ -90,14 +110,6 @@ class FrameTest extends \PHPUnit_Framework_TestCase {
$this->assertTrue($this->_frame->isFinal()); $this->assertTrue($this->_frame->isFinal());
} }
/**
* @dataProvider firstByteProvider
*/
public function testOpcodeFromBits($fin, $opcode, $bin) {
$this->_frame->addBuffer(Frame::encode($bin));
$this->assertEquals($opcode, $this->_frame->getOpcode());
}
/** /**
* @dataProvider UnframeMessageProvider * @dataProvider UnframeMessageProvider
*/ */