I was recently looking at simulating memory usage across a number of VMs where the utilisation pattern, time duration of the memory load and idle time between workloads would be random.
I was looking at using the Sysinternals TestLimt utility to achieve this by invoking a leak and touch memory to simulate resource usage.
As I wanted to generate random values I will be using the Get-Random cmdlet to use a value specified in a minimum and maximum range.
One of the requirements of this script is to simulate memory resource usage in a continuous loop, this is achieved by providing the While statement for a condition that is always true.
While ($True) {
I will then generate a random values for the duration to run the TestLimit utility and the percentage of memory usage to touch where a minimum and maximum value is specified to the Get-Random cmdlet.
$Date = Get-Date $Minutes = Get-Random -Minimum 20 -Maximum 120 $Duration = $Date.AddMinutes($Minutes) $Utilisation = Get-Random -Minimum 20 -Maximum 60
As I specifying memory utilisation as a percentage, I will need to calculate this from the total physical memory configured on the host and truncate to round towards zero. The TestLimit utility requires that memory objects are specified in MB, so the value will be converted following the percentage calculation.
$ComputerSystem = Get-WmiObject Win32_ComputerSystem $Memory = [math]::truncate($ComputerSystem.TotalPhysicalMemory /100 * $Utilisasation
Now that I have the values I wish to pass as arguments to the TestLimit utility for the amount of memory to touch and leak, I will start the process, where TestLimit will leak and touch memory pages (-d) with a total amount of memory generated from the above calculation (-c), where the working directory of the utility is ‘C:\TestLimit’.
Start-Process testlimit64.exe -ArgumentList "-d -c $Memory" -workingdirectory "C:\TestLimit"
As I want to run the process only for a period of time specified until the current date is greater or equal to the random duration value generated, the powershell script will be paused using the Start-Sleep cmdlet to check the current date every 60 seconds and to compare that to the duration.
Do { Start-Sleep -Seconds 60 } Until ((Get-Date) -ge $Duration
Once the current date is greater or equal to the duration string the process testlimit64 will be terminated and the script will then be paused for a random period of time generated from the minimum and maximum value. Once the script is resumed, the script block will be invoked again as we want this process to run in a continous loop.
Get-Process | Where-Object {$_.Name -eq "testlimit64"} | Stop-Process $Sleep = Get-Random -Minimum 180 -Maximum 720 Start-Sleep -Seconds $Sleep }
In this instance the powershell script is invoked by a scheduled task at computer startup. The full script can be downloaded from:
https://app.box.com/s/lq0b6r9srjs5nmm55cta
The TestLimit utility can be downloaded from:
http://live.sysinternals.com/WindowsInternals/testlimit64.exe