Load Testing CPU Cores in C#

I wrote a small Console application in C# that will nuke your logical processors (by nuke, I mean to say that it will eat your CPU utilization, and you’ll see each core spool up to 100% usage):

using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace CPUHog
{
    class Program
    {

        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentThread();
        [DllImport("kernel32.dll")]
        static extern IntPtr SetThreadAffinityMask(IntPtr hThread, IntPtr dwThreadAffinityMask);

        static void Main(string[] args)
        {
            Console.WriteLine("Starting the CPU hog.");
            Console.WriteLine("Number of logical processors: {0}", Environment.ProcessorCount);
            for (int i = 0; i < Environment.ProcessorCount; i++)
            {
                SpoolThread(i);
            }
            Console.WriteLine("Press any key to exit the CPU hog.");
            Console.ReadLine();
            Environment.Exit(0);
        }

        // Spools a spinning thread on a given CPU core.
        static void SpoolThread(int core)
        {
            new Thread(() =>
            {
                Thread.BeginThreadAffinity();
                Console.WriteLine("Nuking logical processor {0}.", core);
                SetThreadAffinityMask(GetCurrentThread(), new IntPtr(1 << (int)(core)));
                // The while loop below is what actually does the nuking.
                while (true) { }
            }).Start();
        }
    }
}

To view its effects, use Task Manager (CTRL + SHIFT + ESCAPE) -> Performance tab -> Click on CPU -> Right click the graph on the right-hand side (in the CPU view) -> Change graph to -> Logical processors. You’ll see something like this:

See the CPUHog in action
Showcasing the CPUHog in action.

Alexandru

"To avoid criticism, say nothing, do nothing, be nothing." - Aristotle

"It is wise to direct your anger towards problems - not people; to focus your energies on answers - not excuses." - William Arthur Ward

"Science does not know its debt to imagination." - Ralph Waldo Emerson

"Money was never a big motivation for me, except as a way to keep score. The real excitement is playing the game." - Donald Trump

"All our dreams can come true, if we have the courage to pursue them." - Walt Disney

"Mitch flashes back to a basketball game held in the Brandeis University gymnasium in 1979. The team is doing well and chants, 'We're number one!' Morrie stands and shouts, 'What's wrong with being number two?' The students fall silent." - Tuesdays with Morrie

I'm not entirely sure what makes me successful in general programming or development, but to any newcomers to this blood-sport, my best guess would be that success in programming comes from some strange combination of interest, persistence, patience, instincts (for example, someone might tell you that something can't be done, or that it can't be done a certain way, but you just know that can't be true, or you look at a piece of code and know something doesn't seem right with it at first glance, but you can't quite put your finger on it until you think it through some more), fearlessness of tinkering, and an ability to take advice because you should be humble. Its okay to be wrong or to have a bad approach, realize it, and try to find a better one, and even better to be wrong and find a better approach to solve something than to have had a bad approach to begin with. I hope that whatever fragments of information I sprinkle across here help those who hit the same roadblocks.

Leave a Reply

Your email address will not be published. Required fields are marked *