Esempio n. 1
0
<?php

require dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . "contrib/php/addressbook.proto.php";
$person = new Tutorial_Person();
$person->setId(21);
$person->setName("John Doe");
$data = serialize($person);
printf("# decoding benchmark (serialize / unserialize).\n");
printf("# target message size: %d bytes.\n", strlen($data));
printf("# start\n");
$begin = microtime(true);
$attempts = 100000;
for ($i = 0; $i < $attempts; $i++) {
    unserialize($data);
}
$end = microtime(true);
printf("# Result:\n");
printf("# decoding %d messages\n", $attempts);
printf("# processing time: %6f (%f/message)\n", $end - $begin, ($end - $begin) / $attempts);
printf("# total bytes: %d bytes\n", strlen($data) * $attempts);
<?php

require dirname(__FILE__) . DIRECTORY_SEPARATOR . join(DIRECTORY_SEPARATOR, array("php", "addressbook.proto.php"));
$db = "addressbook.db";
$addressbook = load($db);
$person = new Tutorial_Person();
$person->setId(1);
$person->setName("John Doe");
$person->setEmail("*****@*****.**");
$phone = new Tutorial_Person_PhoneNumber();
$phone->setNumber("1234 5 67 78");
$phone->setType(Tutorial_Person_PhoneType::HOME);
$person->appendPhone($phone);
$phone = new Tutorial_Person_PhoneNumber();
$phone->setNumber("1234 5 67 79");
$phone->setType(Tutorial_Person_PhoneType::HOME);
$person->appendPhone($phone);
echo $person;
exit;
echo "____________________\n";
$addressbook->appendPerson($person);
var_dump($addressbook);
save($db, $addressbook);
exit;
function load($path)
{
    $data = @file_get_contents($path);
    try {
        $result = ProtocolBuffers::decode("Tutorial_AddressBook", $data);
    } catch (ProtocolBuffersInvalidProtocolBufferException $e) {
        $result = new Tutorial_AddressBook();