需要提高Python程序性能的提示吗

2024-09-28 01:25:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python创建一个模拟器工具,它将使用pyrad模块发送radius请求

但我面临一些性能问题,性能只有1500 tps。 请分享一些想法/建议,以提高模拟器的性能

class MyThread(threading.Thread):

    def __init__(self, auth_obj):
        threading.Thread.__init__(self)
        self.auth_obj = auth_obj

    def run(self):
        self.auth_obj.create_threads()


class Authenticator:
    def __init__(self):
        self.iterations = 0
        self.concurrency = 0
        self.secret_pwd = ""
        self.server_ip = ""

        self.read_xml()

        self.user_idx_list = range(self.concurrency)
        if self.concurrency > MAX_CONC_THREADS:
            self.concurrency = MAX_CONC_THREADS

        print("Main:Starting Time: {}".format((datetime.now()).strftime("%B %d, %H:%M:%S %Y")))

        # Using Threads
        thread_list = list()

        for index in range(self.iterations):
            thread = MyThread(self)
            thread_list.append(thread)
            thread.start()
            time.sleep(1)

            if index % PRINT_OUTPUT_SEC == 0:
                print_table_output()

        for cur_t in thread_list:
            cur_t.join()

        print_table_output()
        str_out = " Final Stats "
        print(str_out.center(63, '#'))
        input("Load Successfully Sent...")


    def send_conc_request(self, username_idx):
        TotalReq.increment()
        username = user_list[username_idx % LIST_SIZE]

        srv = Client(server=self.server_ip, secret=bytes(self.secret_pwd, encoding="ascii"),
                     dict=dict_obj)

        # create request
        req = srv.CreateAuthPacket(code=pyrad.packet.AccessRequest, User_Name=username,
                                   NAS_Identifier="Creator-VM", NAS_IP_Address="10.212.10.211")

        req["User-Password"] = req.PwCrypt("test")

        reply = srv.SendPacket(req)

        AuthReqSent.increment()

        if reply is None:
            AuthReqError.increment()
            return

        if reply.code == pyrad.packet.AccessAccept:
            AuthReqAccept.increment()
            return

        if reply.code == pyrad.packet.AccessTimeout:
            AuthReqTimeout.increment()
            return

        AuthReqReject.increment()
        return

    def create_threads(self):
        try:
            with ThreadPoolExecutor(max_workers=self.concurrency) as executor:
                executor.map(self.send_conc_request, self.user_idx_list, timeout=12)

        except Exception as exc:
            print("Unable to Create Threads Broken ThreadPool", exc)

Tags: selfauthobjifdefusernamereplythread
1条回答
网友
1楼 · 发布于 2024-09-28 01:25:48

首先,线程对cpu密集型进程帮助不大。我认为多重处理可能更有效。 为了解决您的问题,我建议使用“分而治之”的方法,如果您将代码分成更小的部分,您可以测量每个部分的性能,并查看任何部分代码的有效性。有很多工具可以帮助您,比如profiler、line_profiler或perf_tool(我是作者),它们天生就是用来解决这类问题的

相关问题 更多 >

    热门问题