diff --git a/compiler/generate/t_py_generator.cc b/compiler/generate/t_py_generator.cc index 12fc9b68..d48692cf 100644 --- a/compiler/generate/t_py_generator.cc +++ b/compiler/generate/t_py_generator.cc @@ -2272,7 +2272,7 @@ void t_py_generator::generate_py_sizeof(std::ofstream& out, if (type->is_map()) { indent(out) << "size += sum(map(getsizeof, chain.from_iterable(self." << - fname << ")))" << endl; + fname << ".iteritems())))" << endl; } else { indent(out) << diff --git a/library/python/pysandesh/test/msg_test.sandesh b/library/python/pysandesh/test/msg_test.sandesh index b223a883..8e9c84fa 100644 --- a/library/python/pysandesh/test/msg_test.sandesh +++ b/library/python/pysandesh/test/msg_test.sandesh @@ -10,14 +10,22 @@ systemlog sandesh SystemLogTest { 1: "Async Test"; + 2: list list1; + 3: map map1; + 4: uuid_t uuid1; } struct StructObject { - 1: "Object"; + 1: "Object"; + 2: bool flag1; + 3: list list1; + 4: string s1; } objectlog sandesh ObjectLogTest { - 1: StructObject object; + 1: StructObject object; + 2: list list_obj1; + 3: u64 u64_1; } struct StructKeyHint { diff --git a/library/python/pysandesh/test/sandesh_msg_test.py b/library/python/pysandesh/test/sandesh_msg_test.py index 7fe20ce8..22d6cde5 100755 --- a/library/python/pysandesh/test/sandesh_msg_test.py +++ b/library/python/pysandesh/test/sandesh_msg_test.py @@ -14,6 +14,8 @@ import socket import test_utils import time +import uuid +from itertools import chain sys.path.insert(1, sys.path[0]+'/../../../python') @@ -109,6 +111,63 @@ def test_sandesh_queue_level_drop(self): aggregate_stats().messages_sent_dropped_queue_level) # end test_sandesh_queue_level_drop + def test_sandesh_sizeof(self): + # test sizeof SystemLogTest sandesh without setting any members + system_log = SystemLogTest() + exp_system_log_size = sys.getsizeof('Async Test') + self.assertEqual(exp_system_log_size, system_log.__sizeof__()) + + # test sizeof SystemLogTest by setting list1 and uuid1 + l1 = [10, 10934, 34] + uuid1 = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}') + system_log = SystemLogTest(list1=l1, uuid1=uuid1) + # verify that the size of individual elements in the list is + # accounted while calclulating the size of a list + exp_system_log_size += sys.getsizeof(l1) + exp_system_log_size += sum(map(sys.getsizeof, l1)) + exp_system_log_size += sys.getsizeof(uuid1) + self.assertEqual(exp_system_log_size, system_log.__sizeof__()) + + # test sizeof SystemLogTest by setting list1, uuid1 and map1 + m1 = {1: 'collector', 2: 'query-engine', 3: 'opserver'} + system_log = SystemLogTest(list1=l1, map1=m1, uuid1=uuid1) + # verify that the size of individual elements (key, value) + # in the map is accounted which calculating the size of a map + exp_system_log_size += sys.getsizeof(m1) + exp_system_log_size += \ + sum(map(sys.getsizeof, chain.from_iterable(m1.iteritems()))) + self.assertEqual(exp_system_log_size, system_log.__sizeof__()) + + # test the size of struct + exp_struct_obj_size = 0 + f1 = False + lxml1 = 'EOM' + lxml2 = 'sizeof' + l1 = [lxml1, lxml2] + struct_obj1 = StructObject(flag1=f1, list1=l1) + exp_struct_obj_size += sys.getsizeof('Object') + exp_struct_obj_size += sys.getsizeof(f1) + exp_struct_obj_size += sys.getsizeof(lxml1) + exp_struct_obj_size += sys.getsizeof(lxml2) + exp_struct_obj_size += sys.getsizeof(l1) + self.assertEqual(exp_struct_obj_size, struct_obj1.__sizeof__()) + + # test the size of ObjectLogTest + objlog = ObjectLogTest(object=struct_obj1) + exp_objlog_size = sys.getsizeof(struct_obj1) + self.assertEqual(exp_objlog_size, objlog.__sizeof__()) + + # modify objlog and verify the size + struct_obj2 = StructObject(s1='contrail-collector') + list_obj = [struct_obj1, struct_obj2] + objlog.object = None + objlog.list_obj1 = list_obj + exp_objlog_size = sys.getsizeof(list_obj) + exp_objlog_size += sys.getsizeof(struct_obj1) + exp_objlog_size += sys.getsizeof(struct_obj2) + self.assertEqual(exp_objlog_size, objlog.__sizeof__()) + # end test_sandesh_sizeof + # end class SandeshMsgTest if __name__ == '__main__':