Skip to content

Instantly share code, notes, and snippets.

@actionjack
Created October 9, 2015 09:06
Show Gist options
  • Save actionjack/4328658c56b9381a44d6 to your computer and use it in GitHub Desktop.
Save actionjack/4328658c56b9381a44d6 to your computer and use it in GitHub Desktop.
Ruby Rakefile script to simulate a CPU load on a system. Took 1 minute to run on the AWS t1.micro instance for comparison.
#!/bin/bash
# If you have n processors you might want to add a bash loop to create that many processes:
# e.g. for 4 processors
for i in 1 2 3 4; do
rake -f simulate-cpu-load.rake simulate:cpuload &
done
wait
#!/usr/bin/env rake
####################################################################################################
# @author David Kirwan https://github.com/davidkirwan
# @description Ruby Rake script to generate a simulated CPU load on a system
#
# @usage rake -f simulate-cpu-load.rake simulate:cpuload
#
# @date 04-04-2013
####################################################################################################
### => Import libs
require 'rubygems'
require 'fileutils'
require 'logger'
require 'date'
### => Import custom libs
scriptPath = File.expand_path(File.dirname(__FILE__))
### => Variables
# => Date, used in some of the directory/filename generation
date = DateTime.now
theDate = date.year.to_s() + "-" + "%02d" % date.month.to_s() + "-" + "%02d" % date.day.to_s()
# Make the logs/ directory if it does not already exist
unless File.directory?("#{scriptPath}/logs") then FileUtils.mkdir("#{scriptPath}/logs") end
# Create the logger instance
@log = Logger.new("#{scriptPath}/logs/#{theDate}.log")
# Set the logging output level
@log.level = Logger::DEBUG
#@log.level = Logger::WARN
#@log.level = Logger::INFO
#################################################################################
task :default => 'menu'
task :menu do
@log.debug "Printing Welcome"
welcome = <<-WELCOME
rake simulate:cpuload # Simulates a CPU load on a system
WELCOME
puts welcome
end
#################################################################################
namespace :simulate do
desc "Simulate a CPU load"
task :cpuload do
@log.debug "Executing simulate:load task"
puts "Executing simulate:load task"
tasktimer = Time.now
100.times do |i|
100000.downto(1) do |j|
Math.sqrt(j) * i / 0.2
end
end
@log.debug "Duration: #{Time.now - tasktimer} seconds"
puts "Duration: #{Time.now - tasktimer} seconds"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment