Solution 2: optimize the code
To do well, you must first sharpen your tools. To optimize the code, you must first find the bottleneck of the Code. The most common method is to add log or print, and delete the code after debugging is complete, which is troublesome. Python also provides many profile tools: profile, cProfile, hotshot, and pystats. However, these tools provide poor readability, the function or row takes the most time at a glance. Python line_profiler provides such a function, you can intuitively see which occupies the most time, can be said to be "quick accurate",: http://pythonhosted.org/line_profiler/
After line_profiler is installed, there will be a kernprof. py in the C: Python27Libsite-packages directory, adding @ profile to functions that may have bottlenecks, as shown in the following example:
@profile def create_msg2(self,H,msg): li = msg.keys() msg_type=li[0] ULR_avps=[] ULR=HDRItem() ULR.cmd=self.dia.dictCOMMANDname2code(self.dia.MSG_TERM[msg_type]) if msg_type[-1]=='A': msg=msg[msg_type] self.dia.setAVPs_by_dic(msg_type,msg,ULR_avps) ULR.appId=H.appId ULR.EndToEnd=H.EndToEnd ULR.HopByHop=H.HopByHop msg=self.dia.createRes(ULR,ULR_avps) else: self.dia.setAVPs(msg_type,msg,ULR_avps) ULR.appId=self.dia.APPID self.dia.initializeHops(ULR) msg=self.dia.createReq(ULR,ULR_avps) return msg
Run this file: kernprof. py-l-v D: projectmpsrcprotocolslibdiametermt. py. the following result is displayed. From this figure, we can intuitively see that the setAVPS method takes 96.6% of the time and further locates this function, add the @ proflie modifier again (you can add a Profile to multiple functions at a time), and you can see the time-consuming ratio of each line of code in the setAVPS function.
From the step-by-step analysis, we can see that in the open-source protocol library, in the setAVPS method, avp attributes are searched from a 3000 loop, and each AVP needs to be cyclically 3000 times, our initial solution was to delete a lot of things we couldn't use in our performance tests, but it only increased to about 150, and pypy could reach 600. It is far from the demand. Therefore, we changed AVP to the dictionary mode, and we can quickly find AVP attributes based on their names.
In addition to code optimization, the number of encoding avp threads is also increased. The subsequent sections will introduce multithreading and multi-process effects on performance. To be continued ....