Increasing CPU usage to simulate heavy computer use

Grayda

Member
Joined
May 20, 2010
Messages
16
Programming Experience
10+
I'm building a VB.NET 2010 app that tracks battery usage. Run the app, start logging, then let the battery go flat. At the end, you get a nice graph that shows you how long the battery took to run down. Compare it to previous results and you can see how battery life is doing.

But this is supposed to be an unattended app, so it's not feasible for me to sit down and use word, browse the Internet, listen to music, play games etc. for four hours (especially at work!), so I'm looking for a way to artificially increase CPU usage to chew through the battery faster, and simulate how a battery would probably be used throughout a work day.

I've managed to inflate the memory usage using:

Code:
Dim a as long
Dim ptr(0 To 20000) As IntPtr
For a = 1 To 20000
ptr(a) = System.Runtime.InteropServices.Marshal.AllocHGlobal(65535)
Debug.WriteLine("Memory block #" & a & " allocated")
Next
And deflate it by:

Code:
For a = 1 To 20000
System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr(a))
Debug.WriteLine("Memory block #" & a & " freed")
Next
But that doesn't really seem to have any effect on battery time, as that memory isn't being used, just allocated.

How would you suggest I increase CPU usage in VB.NET? I've tried creating new threads with constant loops, but I get the funny feeling that VB.NET optimizes these kind of things to prevent memory and CPU hogging apps from taking over
 

Joshdbr

Well-known member
Joined
Apr 7, 2011
Messages
211
Location
Montana, USA
Programming Experience
5-10
Not sure what exactly you are looking for, but I know from experience that this will crank up the CPU.

Code:
Do Until Me Is Nothing
    Dim proc() As Process = Process.GetProcesses
Loop
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,333
Location
Norway
Programming Experience
10+
It is not necessary to perform any code, a tight loop will utilize the CPU fully, for example do this on a secondary thread:
Private Sub CPUload()
    Do : Loop '100% load
End Sub

You'll need to run one thread for each CPU core. Example:
For i = 1 To Environment.ProcessorCount            
    Dim t As New Threading.Thread(AddressOf CPUload)
    t.IsBackground = True
    t.Start()
Next

If you also want to simulate a steady load to reflect normal-like usage you can do as explained here: c# - Simulate steady CPU load and spikes - Stack Overflow
Private percentLoad As Integer = 30

Private Sub CPUload()
    Dim loadms = 1000 * percentLoad \ 100, sleepms = 1000 - loadms
    Dim watch = Stopwatch.StartNew
    Do
        If watch.ElapsedMilliseconds >= loadms Then
            Threading.Thread.Sleep(sleepms)
            watch.Restart()
        End If
    Loop
End Sub
 

Grayda

Member
Joined
May 20, 2010
Messages
16
Programming Experience
10+
Just gave that a try, and now my app is using 200mb of memory and sitting nicely at 30% usage. Thanks for your help John!
 
Top Bottom