Skip to content

Commit bbda0e5

Browse files
author
Charlie Savage
committed
Add in code to create example reports.
1 parent 67e1264 commit bbda0e5

File tree

4 files changed

+107
-538
lines changed

4 files changed

+107
-538
lines changed

LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright (C) 2005 - 2019 Shugo Maeda <shugo@ruby-lang.org>, Charlie Savage <cfis@savagexi.com> and
2+
Stefan Kaes <skaes@railsepxress.de>
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions
7+
are met:
8+
9+
1. Redistributions of source code must retain the above copyright
10+
notice, this list of conditions and the following disclaimer.
11+
2. Redistributions in binary form must reproduce the above copyright
12+
notice, this list of conditions and the following disclaimer in the
13+
documentation and/or other materials provided with the distribution.
14+
15+
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25+
SUCH DAMAGE.

create_examples.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# A silly little test program that finds prime numbers. It
2+
# is intentionally badly designed to show off the use
3+
# of ruby-prof.
4+
#
5+
# Source from http://people.cs.uchicago.edu/~bomb154/154/maclabs/profilers-lab/
6+
7+
require 'ruby-prof'
8+
require 'fileutils'
9+
10+
def make_random_array(length, maxnum)
11+
result = Array.new(length)
12+
result.each_index do |i|
13+
result[i] = rand(maxnum)
14+
end
15+
16+
result
17+
end
18+
19+
def is_prime(x)
20+
y = 2
21+
y.upto(x-1) do |i|
22+
return false if (x % i) == 0
23+
end
24+
true
25+
end
26+
27+
def find_primes(arr)
28+
result = arr.select do |value|
29+
is_prime(value)
30+
end
31+
result
32+
end
33+
34+
def find_largest(primes)
35+
largest = primes.first
36+
37+
# Intentionally use upto for example purposes
38+
# (upto is also called from is_prime)
39+
0.upto(primes.length-1) do |i|
40+
prime = primes[i]
41+
if prime > largest
42+
largest = prime
43+
end
44+
end
45+
largest
46+
end
47+
48+
def run_primes(length=10, maxnum=1000)
49+
# Create random numbers
50+
random_array = make_random_array(length, maxnum)
51+
52+
# Find the primes
53+
primes = find_primes(random_array)
54+
55+
# Find the largest primes
56+
find_largest(primes)
57+
end
58+
59+
# Generate example reports
60+
def generate_reports(result)
61+
path = File.join('examples')
62+
path = File.expand_path(path)
63+
FileUtils.makedirs(path)
64+
printer = RubyProf::MultiPrinter.new(result, [:flat, :flat_with_lines, :graph, :graph_html, :call_info, :tree, :stack, :dot])
65+
printer.print(:path => path, :profile => 'primes')
66+
end
67+
68+
def run
69+
start = Process.times
70+
result = RubyProf.profile do
71+
run_primes(10000)
72+
end
73+
74+
generate_reports(result)
75+
end
76+
77+
run

index.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
<li><a href="#performance">Performance</a></li>
2020
<li><a href="#profiling-rails">Profiling Rails</a></li>
2121
<li><a href="#version-1.0">Version 1.0</a></li>
22-
<li><a href="#license">License</a></li>
22+
<li><a href="#api-documentation">API Documentation</a></li>
23+
<li><a href="#license"></a><a href="#license">License</a></li>
2324
<li><a href="#development">Development</a></li>
2425
</ul>
2526
</nav>
@@ -526,8 +527,10 @@ <h2><a id="version-1.0">Version 1.0</a></h2>
526527
specified the link scheme. This features was more confusing then
527528
helpful.</li>
528529
</ul>
530+
<h2><a id="api-documentation">API Documentation</a></h2>
531+
<p>API documenation for each class is <a href="doc/index.html">available</a>.</p>
529532
<h2><a id="license">License</a></h2>
530-
<p>See LICENSE for license information.</p>
533+
<p>See <a href="./LICENSE">LICENSE</a> for license information.</p>
531534
<h2><a id="development">Development</a></h2>
532535
<p>Code is located at <a href="https://github.com/ruby-prof/ruby-prof">github.com/ruby-prof/ruby-prof</a></p>
533536
<ul>

0 commit comments

Comments
 (0)