寻求使用python基准测试程序最大化CPU利用率。

发布于 2025-01-18 05:20:45 字数 3889 浏览 3 评论 0原文

我一直致力于用 python 开发一个综合基准测试程序,以便对班级项目的各种系统中的 CPU 进行压力测试。我的方法基于梅森素性测试(受到 prime95 的启发)。该程序旨在测试用户定义的工作集上数字的梅森素性。到目前为止,我已经在Python中实现了这个,但是一旦实现了concurrent.futures模块以便并行运行任务以最大化CPU利用率,我遇到了障碍。在测试我的程序时,我遇到了两个问题。

  1. CPU 利用率仍然只有 ~35%
  2. 当测试更大的工作集时,程序在开始迭代每个素数之前会停滞几分钟 - 我假设这与并发的设置有关。

我希望有人能够提供一些关于如何通过该程序最大限度地利用系统资源并解决较大集的问题的见解。

代码如下:

sys = platform.uname()
c = wmi.WMI()
winSys = c.Win32_ComputerSystem()[0]

mode1 = "Integer Mode"
mode2 = "Floating-Point Mode"

def lehmer(p: int) -> bool:

    s = 4
    M = (1 << p) - 1
    for i in range(p - 2):
        s = ((s * s) - 2) % M
    return s == 0





#Initial printout of system information and menu screen schowing benchamrking options
print("_________________________________________________________________________________")
print("------------------------------System Information---------------------------------")
print(f"\tOS: {sys.system} {sys.release} ") #
print(f"\tMachine Name: {sys.node}")
print(f"\tVersion: {sys.version}")
print(f"\tCPU: {sys.processor}")
print("\tNumber of Cores: " + str(psutil.cpu_count()))
print(f"\tRAM: {psutil.virtual_memory()}")
print("---------------------------------------------------------------------------------")


modeSelect = 0;
print("Welcome to ParaBench! Please select what benchmarking mode you would like to use." + '\n')
modeSelect = int(input("[1] -> " + mode1 + '\n' + "[2] -> " + mode2
    + "\n[9] -> Exit\n_________________________________________________________________________________\n"))


#User selects Integer benchmarking mode
if modeSelect == 1:
    print("User Selected " + mode1)

    #Printout of selection for order of magnitude
    print("[1] -> First 1x10^2 Primes\n" +  "[2] -> First 1x10^3 Primes\n"
        +"[3] -> First 1x10^4 Primes\n" + "[4] -> First 1x10^5 Primes\n" + "[5] -> First 1x10^6 Primes\n")
    mersenneOrder = int(input("Please Select an option\n"))


    if mersenneOrder == 1:
        print("Starting Benchmark...")
        with ThreadPoolExecutor(15) as executor:
            timeStart = perf_counter()

            for result in executor.map(lehmer,range(2,100)):
                print(result)
            timeStop = perf_counter()

            print("1E2 Benchmark Complete in ",timeStop-timeStart)


    if mersenneOrder == 2:
        print("Starting Benchmark...")
        with ThreadPoolExecutor(15) as executor:
            timeStart = perf_counter()

            for result in executor.map(lehmer,range(2,1000)):
                print(result)
            timeStop = perf_counter()
            print("1E3 Benchmark Complete!!", timeStop-timeStart)


    if mersenneOrder == 3:
        print("Starting Benchmark...")
        with ThreadPoolExecutor() as executor:
            timeStart = perf_counter()

            for result in executor.map(lehmer,range(2,10000)):
                print(result)
            timeStop = perf_counter()
            print("1E4 Benchmark Complete!!", timeStop-timeStart)


    if mersenneOrder == 4:
        print("Starting Benchmark...")
        with ThreadPoolExecutor(15) as executor:
            timeStart = perf_counter()

            for result in executor.map(lehmer,range(2,100000)):
                print(result)
            timeStop = perf_counter()
            print("1E5 Benchmark Complete!!", timeStop-timeStart)


    if mersenneOrder == 5:
        print("Starting Benchmark...")
        with ThreadPoolExecutor(15) as executor:
            timeStart = perf_counter()

            for result in executor.map(lehmer,range(2,1000000)):
                print(result)
            timeStop = perf_counter()
            print("1E6 Benchmark Complete!!", timeStop-timeStart)

        #Single-threaded test (DEPRECATED)
        #for x in range(2,1000000):
        #    if lehmer(x):
        #        print(x)

I have been working on developing a synthetic benchmarking program in python in order to stress-test the CPU in various systems for a class project. I have based my approach on Mersenne primality tests (inspired by prime95). The program is intended to test the Mersenne primality of numbers over a working set, defined by the user. I have so far implemented this in python, however once implementing the concurrent.futures module in order to run the task in parallel to maximize CPU utilization, I hit a snag. When testing my program I ran into 2 issues.

  1. CPU utilization is still only ~35%
  2. When testing larger working sets, the program stalls for several minutes before it starts iterating through each prime - I am assuming this has something to do with concurrent.futures's setup.

I was hoping someone could provide some insight into how to maximize system resource usage with this program and iron out the issues with larger sets.

Code below:

sys = platform.uname()
c = wmi.WMI()
winSys = c.Win32_ComputerSystem()[0]

mode1 = "Integer Mode"
mode2 = "Floating-Point Mode"

def lehmer(p: int) -> bool:

    s = 4
    M = (1 << p) - 1
    for i in range(p - 2):
        s = ((s * s) - 2) % M
    return s == 0





#Initial printout of system information and menu screen schowing benchamrking options
print("_________________________________________________________________________________")
print("------------------------------System Information---------------------------------")
print(f"\tOS: {sys.system} {sys.release} ") #
print(f"\tMachine Name: {sys.node}")
print(f"\tVersion: {sys.version}")
print(f"\tCPU: {sys.processor}")
print("\tNumber of Cores: " + str(psutil.cpu_count()))
print(f"\tRAM: {psutil.virtual_memory()}")
print("---------------------------------------------------------------------------------")


modeSelect = 0;
print("Welcome to ParaBench! Please select what benchmarking mode you would like to use." + '\n')
modeSelect = int(input("[1] -> " + mode1 + '\n' + "[2] -> " + mode2
    + "\n[9] -> Exit\n_________________________________________________________________________________\n"))


#User selects Integer benchmarking mode
if modeSelect == 1:
    print("User Selected " + mode1)

    #Printout of selection for order of magnitude
    print("[1] -> First 1x10^2 Primes\n" +  "[2] -> First 1x10^3 Primes\n"
        +"[3] -> First 1x10^4 Primes\n" + "[4] -> First 1x10^5 Primes\n" + "[5] -> First 1x10^6 Primes\n")
    mersenneOrder = int(input("Please Select an option\n"))


    if mersenneOrder == 1:
        print("Starting Benchmark...")
        with ThreadPoolExecutor(15) as executor:
            timeStart = perf_counter()

            for result in executor.map(lehmer,range(2,100)):
                print(result)
            timeStop = perf_counter()

            print("1E2 Benchmark Complete in ",timeStop-timeStart)


    if mersenneOrder == 2:
        print("Starting Benchmark...")
        with ThreadPoolExecutor(15) as executor:
            timeStart = perf_counter()

            for result in executor.map(lehmer,range(2,1000)):
                print(result)
            timeStop = perf_counter()
            print("1E3 Benchmark Complete!!", timeStop-timeStart)


    if mersenneOrder == 3:
        print("Starting Benchmark...")
        with ThreadPoolExecutor() as executor:
            timeStart = perf_counter()

            for result in executor.map(lehmer,range(2,10000)):
                print(result)
            timeStop = perf_counter()
            print("1E4 Benchmark Complete!!", timeStop-timeStart)


    if mersenneOrder == 4:
        print("Starting Benchmark...")
        with ThreadPoolExecutor(15) as executor:
            timeStart = perf_counter()

            for result in executor.map(lehmer,range(2,100000)):
                print(result)
            timeStop = perf_counter()
            print("1E5 Benchmark Complete!!", timeStop-timeStart)


    if mersenneOrder == 5:
        print("Starting Benchmark...")
        with ThreadPoolExecutor(15) as executor:
            timeStart = perf_counter()

            for result in executor.map(lehmer,range(2,1000000)):
                print(result)
            timeStop = perf_counter()
            print("1E6 Benchmark Complete!!", timeStop-timeStart)

        #Single-threaded test (DEPRECATED)
        #for x in range(2,1000000):
        #    if lehmer(x):
        #        print(x)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文