Skip to content

Instantly share code, notes, and snippets.

@qxj
Created March 23, 2015 08:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qxj/ad976636b7938e4281c4 to your computer and use it in GitHub Desktop.
Save qxj/ad976636b7938e4281c4 to your computer and use it in GitHub Desktop.
avro php library benchmark
<?php
require_once('../lib/avro.php');
function create_record() {
$rec = array('member_id' => 1392, 'member_id2' => 999);
for ($i = 0; $i < 20; $i++) {
$rec['field' . $i] = 'test_value' . $i;
}
return $rec;
}
function create_writer() {
$schema_json = <<<_JSON
{"name":"member",
"type":"record",
"fields":[
{"name":"member_id", "type":"int"},
_JSON;
for ($i = 0; $i < 20; $i++) {
$schema_json .= '{"name":"field' . $i . '", "type":"string"},';
}
$schema_json .= '{"name":"member_id2", "type":"int"}]}';
$schema = AvroSchema::parse($schema_json);
$writer = new AvroIODatumWriter($schema);
return $writer;
}
for ($i = 0; $i < 10000; $i++) {
$writer = apc_fetch("writer");
if ($writer === false) {
$writer = create_writer();
apc_store("writer", $writer);
}
$io = new AvroStringIO();
$encoder = new AvroIOBinaryEncoder($io);
$writer->write(create_record(), $encoder);
$binary_string = $io->string();
// file_put_contents("/tmp/avro.log", $binary_string, FILE_APPEND);
}
<?php
// ab -n 10000 -c 10 http://url/to/avro_nginx_test.php
require_once('../lib/avro.php');
function create_record() {
$rec = array('member_id' => 1392, 'member_id2' => 999);
for ($i = 0; $i < 100; $i++) {
$rec['field' . $i] = 'test_value' . $i;
}
return $rec;
}
function create_writer() {
$schema_json = <<<_JSON
{"name":"member",
"type":"record",
"fields":[
{"name":"member_id", "type":"int"},
_JSON;
for ($i = 0; $i < 100; $i++) {
$schema_json .= '{"name":"field' . $i . '", "type":"string"},';
$jose['field' . $i] = 'test_value' . $i;
}
$schema_json .= '{"name":"member_id2", "type":"int"}]}';
$schema = AvroSchema::parse($schema_json);
$writer = new AvroIODatumWriter($schema);
return $writer;
}
$writer = apc_fetch("writer");
if ($writer === false) {
$writer = create_writer();
apc_store("writer", $writer);
}
$io = new AvroStringIO();
$encoder = new AvroIOBinaryEncoder($io);
$writer->write(create_record(), $encoder);
$binary_string = $io->string();
file_put_contents("/tmp/avro.log", AvroIOBinaryEncoder::encode_long(strlen($binary_string)) . $binary_string, FILE_APPEND);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment