Python multithreading has been written in the past finally come in handy, in fact, not yet started testing, but next week will use this script test, although boss let me do in C + +:
# coding=utf-8import Randomimport stringimport threadingimport timefrom requests Import Postclass MultiThread (threading . Thread): Def __init__ (self, URL, qlock): Threading. Thread.__init__ (self) self.url = URL Self.qlock = Qlock def run (self): send_requests (Self.url, SEL F.qlock) def send_requests (URL, qlock): "" ":p Aram Url:http://xxx:p Aram Qlock: Thread Lock: Return:" "" qlock.a Cquire () nums = ' 0123456789 ' Try:json = {' Longitude ': '. Join (Random.sample (nums + string.digits , 3)) + '. ' + '. Join (Random.sample (nums + string.digits)), ' Latitude ': '. Join (Random.sample (Nums + string . digits, 2)) + '. ' + '. Join (Random.sample (nums + string.digits,), ' Subtime ': Time.strftime ('%y-%m-%d%h:%m :%s ', Time.localtime ()), ' MachineID ': '. Join (Random.sample (string.ascii_letters + string.digits, 6)} R = Post (URL, Json=json, timeout=1) Print R.status_code # print R.Text Finally:qlock.release () def run_thread (URL, concurrency): "" ":p Aram Url:http://xxx:p Aram Concur Rency: Number of concurrent: return: "" "Lock = Threading. Lock () threads = [] for Cncu in range (1, concurrency): t = multithread (URL, lock) T.daemon = True T.start () threads.append (t) for T in Threads:t.join () if __name__ = = ' __main__ ': url = ' http://xxx ' For I in range (0, 1000000): Run_thread (URL, 10)
I don't really understand if it would be better to do stress testing in C + +? While Python's multithreading is known to be fake (Gil Lock), no matter how many threads are opened, there are actually only 1 threads running ... C + + multithreading is certainly better performance, but I personally always feel that doing stress testing does not seem to use C + +?
The personal understanding of these two languages is this: Python is an interpreted language, so each run requires an interpreter side to explain the run (I remember Python in order to solve this problem---improve performance, so will generate some configuration files, if the code is not changed, then the last run of the process to execute--- This seems to be the case = =), and the executable file generated by C + + through the compiler may need to invoke some dynamic-link libraries (DLLs) at run time, but it does not need to be compiled once per run, so performance is excellent.
This piece is entirely their own knowledge blind area (learning depth or not enough), if there is a very understanding of the students, but also hope to be generous teaching means!
Python Learning-multi-threaded Send request test server pressure