As a tech I am often in the position where a computer’s performance just doesn’t match the shiny sticker on the outside of the box boasting unlimited power hereafter! So just how capable is the CPU in your machine and how many operations can it handle at ~100% CPU saturation on all cores?

 

Multithreaded CPU Stress Test using Mersenne Primes Multithreaded CPU Stress Test using Mersenne Primes

Stressing your CPU

In this article I wrote a short C++ CPU Stress Test Program to benchmark this using the Mersenne Primes algorithm.

You can download the source files for this article here

**NOTE**

After install Windows Driver Toolkit I now have to manually set some of the linker paths – you may well need to set these yourself depending on your VC environment

 

CPU Stress Test Linker Options CPU Stress Test Linker Options

And be sure to include NTDLL as an additional library

CPU Stress Test linker NTDLL CPU Stress Test linker NTDLL

 

ABOUT THE CODE

The program firstly determines how many cores the machine under test has by calling NtQuerySystemInformation and populating the BasicInformation object. We want to stress each core if possible so we need to determine this from machine to machine.

// want the number of cores
NtQuerySystemInformation(SystemBasicInformation, &BasicInformation, sizeof(SYSTEM_BASIC_INFORMATION), NULL);

Each thread searches for Mersenne Primes between n (2-61 here)

 BOOL CpuBenchmark::IsPrime(ULONG64 n)  // function determines if the number n is prime.
{
	for (ULONG64 i = 2; i * i < n; i++)

		if (n % i == 0)
			return false;
	return true;
}


ULONG64 CpuBenchmark::Power2(ULONG64 n)		//function returns 2 raised to power of n
{
	ULONG64 square = 1;
	for (ULONG64 i = 0; i < n; i++)
	{
		square *= 2;
	}
	return square;

}

VOID CpuBenchmark::MersennePrimes()
{
	
	ULONG64 i;
	ULONG64 n;

	for (n = 2; n <= 61; n++)      
	{                              
		if (IsPrime(n) == 1)      
		{
			i = Power2(n) - 1;
			if (IsPrime(i) == 1) {
				// dont care just want to stress the CPU
			}
		}

	}
}

The programs then run a separate thread starting a new instance of the test class hoping to take advantage of each core

// make sure we've got at least as many threads as cores
for (int i = 0; i < this->BasicInformation.NumberOfProcessors; i++)
{
threads.push_back(thread(&CpuBenchmark::MersennePrimes, CpuBenchmark()));
}

The programs then waits for all threads to complete

// wait for them all to finish
for (auto& th : threads)
th.join();

And return the ellapsed time back to the caller

auto stop = std::chrono::steady_clock::now();
ULONG64 millisecondsDuration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count();

return millisecondsDuration;

Conclusion

On my 8 core Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz this took about 20 seconds to complete.

You can download the source files for this article here
http://malcolmswaine.com/cpustresstestmersenneprimes-2/

Written by admin