1+ #!/usr/bin/python
2+ import sys
3+ import time
4+ from sample_test import *
5+ from perf_test import *
6+
7+ INT8_MIN = - (2 ** 7 )
8+ UINT8_MAX = (2 ** 8 ) - 1
9+ INT16_MIN = - (2 ** 15 )
10+ UINT16_MAX = (2 ** 16 ) - 1
11+ INT32_MIN = - (2 ** 31 )
12+ UINT32_MAX = (2 ** 32 ) - 1
13+ INT64_MIN = - (2 ** 63 )
14+ UINT64_MAX = (2 ** 64 ) - 1
15+ #FLOAT_MAX = 3.40282e+038
16+ DOUBLE_MAX = 1.79769e+308
17+
18+ def test_create_sample_struct (key ):
19+ obj_val = rb_create_sample_struct ()
20+ obj_val ['int8_val' ] = key
21+ obj_val ['uint8_val' ] = 128
22+ obj_val ['str_val' ] = "test_string"
23+ obj_val ['str_arr_val' ].append ("test_string_1" )
24+ obj_val ['str_arr_val' ].append ("test_string_2" )
25+ obj_val ['str_arr_val' ].append ("test_string_3" )
26+ obj_val ['str_dict_val' ]["test_key1" ] = "test_string_1"
27+ obj_val ['str_dict_val' ]["test_key2" ] = "test_string_2"
28+ obj_val ['str_dict_val' ]["test_key3" ] = "test_string_3"
29+ return obj_val
30+
31+ def test_sample_struct ():
32+ obj_val = test_create_sample_struct (32 )
33+ file = "sample_struct.bin"
34+ rb_dump_sample_struct (obj_val , file )
35+ (obj_out , rc ) = rb_load_sample_struct (file )
36+ if not rc :
37+ return False
38+ return rb_eq_sample_struct (obj_val , obj_out )
39+
40+ def test_sample_object ():
41+ obj_val = rb_create_sample_object ()
42+ obj_val ['obj' ] = test_create_sample_struct (8 )
43+ item1 = test_create_sample_struct (16 )
44+ item2 = test_create_sample_struct (32 )
45+ item3 = test_create_sample_struct (64 )
46+ obj_val ['arr' ].append (item1 )
47+ obj_val ['arr' ].append (item2 )
48+ obj_val ['arr' ].append (item3 )
49+ obj_val ['dict' ]['test_string_1' ] = item1
50+ obj_val ['dict' ]['test_string_2' ] = item2
51+ obj_val ['dict' ]['test_string_3' ] = item3
52+ file = "sample_object.bin"
53+ rb_dump_sample_object (obj_val , file )
54+ (obj_out , rc ) = rb_load_sample_object (file )
55+ if not rc :
56+ return False
57+ return rb_eq_sample_object (obj_val , obj_out )
58+
59+ def init_list (obj ):
60+ for i in xrange (10 ):
61+ obj .append (i )
62+ def init_dict (obj ):
63+ for i in xrange (10 ):
64+ obj ['key%d' % i ] = 'val%d' % i
65+
66+ def run_perf_test (buf , obj ):
67+ buf ['pos' ] = buf ['start' ]
68+ if not rb_encode_perf_object (obj , buf ):
69+ print "rb_encode fail"
70+ return
71+ buf ['pos' ] = buf ['start' ]
72+ (objout , rc ) = rb_decode_perf_object (buf , 0 )
73+ if not rc :
74+ print "rb_decode fail"
75+ if not rb_eq_perf_object (obj , objout ):
76+ print "not eq fail"
77+
78+ def init_perf_object_base (obj ):
79+ obj ["bool_val" ] = True
80+ obj ["int8_val" ] = INT8_MIN
81+ obj ["uint8_val" ] = UINT8_MAX
82+ obj ["int16_val" ] = INT16_MIN
83+ obj ["uint16_val" ] = UINT16_MAX
84+ obj ["int32_val" ] = INT32_MIN
85+ obj ["uint32_val" ] = UINT32_MAX
86+ obj ["int64_val" ] = INT64_MIN
87+ obj ["uint64_val" ] = UINT64_MAX
88+ # float has precision issue (IEEE 754)
89+ obj ["float_val" ] = 1024.5
90+ obj ["double_val" ] = DOUBLE_MAX
91+ init_dict (obj ["dict_val" ])
92+
93+ def perf_test (count ):
94+ obj = rb_create_perf_object ()
95+ init_perf_object_base (obj )
96+ obj ["str_val" ] = "test_string"
97+ init_list (obj ["vec_val" ])
98+ buf = rb_create_buf (rb_sizeof_perf_object (obj ))
99+ start = time .time ()
100+ for i in xrange (count ):
101+ run_perf_test (buf , obj )
102+ end = time .time ()
103+ return end - start
104+
105+ def optional_field_test ():
106+ obj = rb_create_perf_object ()
107+ init_perf_object_base (obj )
108+ obj ['skip_str_val' ] = True
109+ obj ['skip_vec_val' ] = True
110+
111+ file = "optional_field_test.bin"
112+
113+ rb_dump_perf_object (obj , file )
114+
115+ (obj_out , rc ) = rb_load_perf_object (file )
116+ if not rc :
117+ return False
118+
119+ if obj_out ['rb_has_str_val' ]:
120+ print "error: str_val should be skipped" ;
121+ if obj_out ['rb_has_vec_val' ]:
122+ print "error: vec_val should be skipped"
123+ return rb_eq_perf_object (obj , obj_out )
124+
125+ def test_main (argv ):
126+ if 2 == len (argv ):
127+ count = int (argv [1 ])
128+ if count < 1 :
129+ print 'invalid argument'
130+ return
131+ us = perf_test (count )
132+ qps = int (count / us )
133+ print 'QPS: %d' % qps
134+ return
135+ if optional_field_test () and test_sample_struct () and test_sample_object ():
136+ print 'unit test passed'
137+ if __name__ == "__main__" :
138+ test_main (sys .argv )
0 commit comments