Atlas  0.7.0
Networking protocol for the Worldforge system.
test_size.py
1 #test size of messages produced by various codecs
2 
3 #Copyright 2001 by Aloril
4 
5 #This library is free software; you can redistribute it and/or
6 #modify it under the terms of the GNU Lesser General Public
7 #License as published by the Free Software Foundation; either
8 #version 2.1 of the License, or (at your option) any later version.
9 
10 #This library is distributed in the hope that it will be useful,
11 #but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 #Lesser General Public License for more details.
14 
15 #You should have received a copy of the GNU Lesser General Public
16 #License along with this library; if not, write to the Free Software
17 #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 print("Not yet ported to rewritten Atlas-Python!")
20 import string, time
21 import os,stat,sys
22 from types import *
23 import random
24 import atlas, codec, file
25 import xml
26 
27 #temnporary so testing is easier:
28 import binary1
29 import importlib
30 importlib.reload(binary1)
31 import binary2
32 importlib.reload(binary2)
33 importlib.reload(codec)
34 #import Binary
35 #reload(Binary)
36 #from Binary import Binary
37 
38 def deb(): import pdb; pdb.pm()
39 
40 def disp_file_stats(msg_count, file_name, description):
41  size = os.stat(file_name)[stat.ST_SIZE]
42  return disp_stats(msg_count,size,description)
43 
44 def disp_stats(count,total_len,description):
45  print("%s:\n Total length: %6i, Length/msg: %6.1f" % \
46  (description,total_len,1.0*total_len/count))
47  return description,total_len,1.0*total_len/count
48 
49 def calculate_stats(msgs, codec):
50  msg_stats = []
51  encode = codec.encode
52  description = codec.id
53  count = len(msgs)
54  total_len = 0
55  result_str_list =[]
56  for msg in msgs:
57  str = encode(msg)
58  if type(str)==StringType:
59  total_len = total_len + len(str)
60  result_str_list.append(str)
61  else:
62  total_len = total_len + str
63  msg_stats.append(disp_stats(count,total_len,description))
64  result_str = string.join(result_str_list,"")
65  open("test_size.log","w").write(result_str)
66  os.system("gzip -9 test_size.log")
67  msg_stats.append(disp_file_stats(len(msgs),"test_size.log.gz","gzip -9 compressed file"))
68  os.system("gunzip test_size.log.gz")
69  os.system("bzip2 -9 test_size.log")
70  msg_stats.append(disp_file_stats(len(msgs),"test_size.log.bz2","bzip2 -9 compressed file"))
71  os.system("bunzip2 test_size.log.bz2")
72  return msg_stats
73 
74 
75 #see http://bilbo.escapesystems.com/~aloril/atlas/logs/
76 def all_encoding_stats(file_name):
77  """output with default file_name:
78 Test file: cyphesis_atlas_XML_2000-03-27_no_obj.log
79 Msg count: 228
80  uncompressed gzip -9 bzip2 -9
81 XML 954.97 47.93 33.95
82 XML2_test 727.00 45.92 34.03
83 Packed 384.41 36.79 30.84
84 Bach_beta 478.61 39.11 31.35
85 Binary1_beta 380.54 38.58 32.26
86 Binary2_test 236.12 35.22 30.78
87 
88 
89 Test file: CyphesisClient_fromServerViewpoint2_no_obj.log
90 Msg count: 716
91  uncompressed gzip -9 bzip2 -9
92 XML 832.77 36.76 23.82
93 XML2_test 632.11 35.03 23.94
94 Packed 284.41 28.88 23.20
95 Bach_beta 373.68 31.91 23.28
96 Binary1_beta 277.63 30.53 24.04
97 Binary2_test 156.22 28.31 23.62
98 
99 
100 Test file: cyphesis_atlas_Packed_2001-07-13.log
101 Msg count: 4768
102  uncompressed gzip -9 bzip2 -9
103 XML 1250.59 27.39 14.17
104 XML2_test 910.63 23.97 13.20
105 Packed 405.12 17.34 12.67
106 Bach_beta 544.18 18.72 12.21
107 Binary1_beta 441.45 22.03 14.34
108 Binary2_test 260.30 19.02 13.23
109  """
110  print()
111  print()
112  print("Test file:",file_name)
113  global all_msg
114  xml_codec = codec.get_XML()
115  #all_msg = xml_codec.decode(open(file_name).read())
116  all_msg = file.read_file(file_name)
117  print("Msg count:",len(all_msg))
118 
119  all_stats = []
120  #XML size
121  all_stats.append(calculate_stats(all_msg,xml_codec))
122 
123  #XML2 size
124  all_stats.append(calculate_stats(all_msg,codec.get_XML2_test()))
125 
126  #Packed size
127  all_stats.append(calculate_stats(all_msg,codec.get_Packed()))
128 
129  #Bach size
130  all_stats.append(calculate_stats(all_msg,codec.get_Bach_beta()))
131 
132  #Binary1_beta size
133  all_stats.append(calculate_stats(all_msg,codec.get_Binary1_beta()))
134 
135  #Binary2_test size
136  all_stats.append(calculate_stats(all_msg,codec.get_Binary2_test()))
137 
138 ## for name in binary2.attribute_type_dict.keys():
139 ## print name
140 ## binary2.discard_name = name
141 ## all_stats.append(calculate_stats(all_msg,codec.get_Binary2_test()))
142 ## all_stats[-1][0] = list(all_stats[-1][0])
143 ## all_stats[-1][0][0] = name
144 ## all_stats.sort(lambda e1,e2:cmp(e1[2][2],e2[2][2]))
145 
146  print()
147  filter_types = ("uncompressed", "gzip -9", "bzip2 -9")
148  print(" %10s %10s %10s" % filter_types)
149  for stat in all_stats:
150  print("%-13s %10.2f %10.2f %10.2f" % (
151  stat[0][0], stat[0][2], stat[1][2], stat[2][2]))
152 
153 def all_tests():
154  all_encoding_stats("cyphesis_atlas_XML_2000-03-27_no_obj.log")
155  all_encoding_stats("CyphesisClient_fromServerViewpoint2_no_obj.log")
156  all_encoding_stats("cyphesis_atlas_Packed_2001-07-13.log")
157 
158 
159 msg = xml.string2object("""
160 <map>
161  <string name="abstract_type">operation</string>
162  <list name="parents">
163  <string>error</string>
164  </list>
165  <int name="serialno">2</int>
166  <int name="refno">1</int>
167  <map name="time">
168  <float name="seconds">19035676005.9</float>
169  <string name="time_string">0612-01-01 07:46:45.9</string>
170  </map>
171  <list name="args">
172  <map>
173  <string name="message">Account id already exist</string>
174  </map>
175  <map>
176  <string name="abstract_type">operation</string>
177  <list name="parents">
178  <string>create</string>
179  </list>
180  <int name="serialno">1</int>
181  <map name="time">
182  <float name="seconds">19035676005.9</float>
183  <string name="time_string">0612-01-01 07:46:45.9</string>
184  </map>
185  <list name="args">
186  <map>
187  <string name="password">lZYVYjmU</string>
188  <string name="id">admin</string>
189  <list name="parents">
190  <string>player</string>
191  </list>
192  </map>
193  </list>
194  </map>
195  </list>
196 </map>
197 """)
198 
199 all_tests()
200 #p = packed.get_parser()
201 #msg2 = p("[@id=17$name=Fred +28the +2b great+29#weight=1.5(args=@1@2@3)]")
202 
203 def check_time(codec, all_msgs):
204  print(codec.id)
205  encode = codec.encode
206  str_all_msgs = []
207  t0 = time.time()
208  for obj in all_msgs:
209  str_all_msgs.append(encode(obj))
210  t1 = time.time()
211  print(t1-t0)
212  decode = codec.decode
213  if codec.id[:3]=="XML":
214  decode("<atlas>")
215  t0 = time.time()
216  for str in str_all_msgs: decode(str)
217  t1 = time.time()
218  print(t1-t0)
219 
220 def check_all_times():
221  for c in (codec.get_XML(),
222  codec.get_XML2_test(),
223  codec.get_Packed(),
224  codec.get_Bach_beta(),
225  codec.get_Binary1_beta()):
226  check_time(c, all_msg)
227