/** * Serialize profiling data and send to a profiling data aggregator. * * Individual entries are represented as arrays and then encoded using * MessagePack, an efficient binary data-interchange format. Encoded * entries are accumulated into a buffer and sent in batch via UDP to the * profiling data aggregator. */ public function logData() { global $wgUDPProfilerHost, $wgUDPProfilerPort; $this->close(); if (!function_exists('socket_create')) { return; // avoid fatal } $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_connect($sock, $wgUDPProfilerHost, $wgUDPProfilerPort); $bufferLength = 0; $buffer = ''; foreach ($this->mCollated as $name => $entry) { $count = $entry['count']; $cpu = $entry['cpu']; $wall = $entry['wall']; if ($count === 1) { $data = array(self::TYPE_SINGLE, $name, $cpu, $wall); } else { $data = array(self::TYPE_RUNNING, $name, $count, $cpu->m1, $cpu->m2, $cpu->min, $cpu->max, $wall->m1, $wall->m2, $wall->min, $wall->max); } $encoded = MWMessagePack::pack($data); $length = strlen($encoded); // If adding this entry would cause the size of the buffer to // exceed the standard ethernet MTU size less the UDP header, // send all pending data and reset the buffer. Otherwise, continue // accumulating entries into the current buffer. if ($length + $bufferLength > 1450) { socket_send($sock, $buffer, $bufferLength, 0); $buffer = ''; $bufferLength = 0; } $buffer .= $encoded; $bufferLength += $length; } if ($bufferLength !== 0) { socket_send($sock, $buffer, $bufferLength, 0); } }
/** * Verify that values are serialized correctly. * @covers MWMessagePack::pack * @dataProvider providePacks */ public function testPack($type, $value, $expected) { $actual = bin2hex(MWMessagePack::pack($value)); $this->assertEquals($expected, $actual, $type); }