Class: RHMP::Benchmark

Inherits:
Object show all
Defined in:
lib/benchmark.rb

Class Method Summary collapse

Class Method Details

.benchmark(iterations = 5) ⇒ Object

A synthetic, quick and dirty benchmark that gives you a bit of information about the system you’re running the script on.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/benchmark.rb', line 7

def self.benchmark(iterations = 5)

  starttime = Time.now
  puts "Benchmark started at #{Time.now.to_s}."
  puts

  times_to_run = iterations * 5000
  puts "Running Benchmark #1 (Composing random strings) #{times_to_run} times..."
  times_to_run.times do
    srand
    a = String.new
    a << rand(99)
    a = "#{a}#{rand(99)}"
    a = nil
  end

  times_to_run = iterations * 2500
  puts "Running Benchmark #2 (Performing math calculations) #{times_to_run} times..."
  times_to_run.times do
    a = 100 * 40 / 30 * 12 / 9
    a += 1
    a += a
    a = a ^ 9 * rand(10)
    b = Math.sqrt(a)
    a = b + a
    b = Math.atan(b) + a
  end

  times_to_run = iterations * 2
  puts "Running Benchmark #3 (File read and array operations) #{times_to_run} times..."
  f = File.new("/usr/share/dict/words","r")
  a = f.readlines
  f.close
  times_to_run.times do
    a.reverse!
    a.sort!
    a.uniq!
  end

  times_to_run = iterations * 2
  puts "Running Benchmark #4 (File write operations) #{times_to_run} times..."
  times_to_run.times do
    f = File.new("tmpbenchmark.txt","w")
    a.each do |l|
      f.puts l
    end
  end
  f.close
  File.unlink("tmpbenchmark.txt")
  
  endtime = Time.now

  puts
  puts "Benchmarks ended at #{endtime}, taking #{endtime - starttime} seconds to run."
  puts

end