Author: cutting
Date: Thu Mar 31 22:49:37 2011
New Revision: 1087472
URL: http://svn.apache.org/viewvc?rev=1087472&view=rev
Log:
AVRO-709. Python: Optimize property lookup. Contributed by Justin Azoff.
Added:
avro/trunk/lang/py/test/av_bench.py
Modified:
avro/trunk/CHANGES.txt
avro/trunk/lang/py/src/avro/schema.py
Modified: avro/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1087472&r1=1087471&r2=1087472&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Thu Mar 31 22:49:37 2011
@@ -33,6 +33,8 @@ Avro 1.5.1 (unreleased)
AVRO-296. IDL: Use double-asterisk comments for schema documentation.
(cutting)
+ AVRO-709. Python: Optimize property lookup. (Justin Azoff via cutting)
+
BUG FIXES
AVRO-786. Java: Fix equals() to work on objects containing maps. (cutting)
Modified: avro/trunk/lang/py/src/avro/schema.py
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/py/src/avro/schema.py?rev=1087472&r1=1087471&r2=1087472&view=diff
==============================================================================
--- avro/trunk/lang/py/src/avro/schema.py (original)
+++ avro/trunk/lang/py/src/avro/schema.py Thu Mar 31 22:49:37 2011
@@ -113,18 +113,18 @@ class Schema(object):
# add members
if not hasattr(self, '_props'): self._props = {}
self.set_prop('type', type)
+ self.type = type
# Read-only properties dict. Printing schemas
# creates JSON properties directly from this dict.
props = property(lambda self: self._props)
- type = property(lambda self: self.get_prop('type'))
# utility functions to manipulate properties dict
def get_prop(self, key):
- return self.props.get(key)
+ return self._props.get(key)
def set_prop(self, key, value):
- self.props[key] = value
+ self._props[key] = value
def __str__(self):
names = Names()
@@ -310,13 +310,13 @@ class Field(object):
raise SchemaParseException(fail_msg)
self.set_prop('type', type_schema)
self.set_prop('name', name)
+ self.type = type_schema
+ self.name = name
# TODO(hammer): check to ensure default is valid
if has_default: self.set_prop('default', default)
if order is not None: self.set_prop('order', order)
# read-only properties
- type = property(lambda self: self.get_prop('type'))
- name = property(lambda self: self.get_prop('name'))
default = property(lambda self: self.get_prop('default'))
has_default = property(lambda self: self._has_default)
order = property(lambda self: self.get_prop('order'))
@@ -324,9 +324,9 @@ class Field(object):
# utility functions to manipulate properties dict
def get_prop(self, key):
- return self.props.get(key)
+ return self._props.get(key)
def set_prop(self, key, value):
- self.props[key] = value
+ self._props[key] = value
def to_json(self, names):
to_dump = self.props.copy()
Added: avro/trunk/lang/py/test/av_bench.py
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/py/test/av_bench.py?rev=1087472&view=auto
==============================================================================
--- avro/trunk/lang/py/test/av_bench.py (added)
+++ avro/trunk/lang/py/test/av_bench.py Thu Mar 31 22:49:37 2011
@@ -0,0 +1,77 @@
+#!/usr/bin/env python
+
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import sys
+import time
+from random import sample, choice, randint
+from string import lowercase
+
+import avro.datafile
+import avro.schema
+import avro.io
+
+
+types = ["A", "CNAME"]
+
+def rand_name():
+ return ''.join(sample(lowercase, 15))
+
+def rand_ip():
+ return "%s.%s.%s.%s" %(randint(0,255), randint(0,255), randint(0,255),
randint(0,255))
+
+def write(n):
+ schema_s="""
+ { "type": "record",
+ "name": "Query",
+ "fields" : [
+ {"name": "query", "type": "string"},
+ {"name": "response", "type": "string"},
+ {"name": "type", "type": "string", "default": "A"}
+ ]}"""
+ out = open("datafile.avr",'w')
+
+ schema = avro.schema.parse(schema_s)
+ writer = avro.io.DatumWriter(schema)
+ dw = avro.datafile.DataFileWriter(out, writer, schema) #,codec='deflate')
+ for _ in xrange(n):
+ response = rand_ip()
+ query = rand_name()
+ type = choice(types)
+ dw.append({'query': query, 'response': response, 'type': type})
+
+ dw.close()
+
+def read():
+ f = open("datafile.avr")
+ reader = avro.io.DatumReader()
+ af=avro.datafile.DataFileReader(f,reader)
+
+ x=0
+ for _ in af:
+ pass
+
+def t(f, *args):
+ s = time.time()
+ f(*args)
+ e = time.time()
+ return e-s
+
+if __name__ == "__main__":
+ n = int(sys.argv[1])
+ print "Write %0.4f" % t(write, n)
+ print "Read %0.4f" % t(read)