Skip to content

Commit

Permalink
Add C profiling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Weaver committed Feb 23, 2011
1 parent b013b58 commit d486a34
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 36 deletions.
16 changes: 10 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ task :exceptions do
end
end

task :valgrind do
exec("ruby #{File.dirname(__FILE__)}/test/profile/valgrind.rb")
end

task :benchmark do
exec("ruby #{File.dirname(__FILE__)}/test/profile/benchmark.rb")
end

task :profile do
exec("ruby #{File.dirname(__FILE__)}/test/profile/profile.rb")
task :rb_profile do
exec("ruby #{File.dirname(__FILE__)}/test/profile/rb_profiler.rb")
end

task :c_profile do
exec("ruby #{File.dirname(__FILE__)}/test/profile/c_profiler.rb")
end

task :valgrind do
exec("ruby #{File.dirname(__FILE__)}/test/profile/valgrind.rb")
end
14 changes: 14 additions & 0 deletions test/profile/c_profiler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "#{File.dirname(__FILE__)}/../setup"

$LOAD_PATH << "#{File.dirname(__FILE__)}/../../lib/"
require 'memcached'

profile = "/tmp/memcached_#{Memcached::VERSION}_c"

system("env CPUPROFILE_FREQUENCY=500 CPUPROFILE=#{profile}.out DYLD_INSERT_LIBRARIES=/opt/local/lib/libprofiler.dylib ruby -r#{File.dirname(__FILE__)}/exercise -e \"Worker.new('mixed', 200000).work\"")

ruby = `which ruby`.chomp

system("pprof --nodefraction=0.0000001 --text #{ruby} #{profile}.out")
system("pprof --nodefraction=0.0000001 --pdf #{ruby} #{profile}.out > #{profile}.pdf")
system("open #{profile}.pdf")
29 changes: 19 additions & 10 deletions test/profile/exercise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@
GC.copy_on_write_friendly = true if GC.respond_to?("copy_on_write_friendly=")

class Worker
def initialize(method_name = 'mixed', iterations = 10000, ignore_memory = false)
@method = method_name
@i = iterations.to_i
@ignore_memory = ignore_memory
def initialize(method_name, iterations, with_memory = 'false')
@method = method_name || 'mixed'
@i = (iterations || 10000).to_i
@with_memory = with_memory

puts "*** Running #{@method.inspect} test for #{@i} loops. ***"

@key1 = "key1-"*8
@key2 = "key2-"*8
@key1 = "key1--------------------------------"
@key2 = "key2--------------------------------"
@key3 = "key3--------------------------------"

@value = []
@marshalled = Marshal.dump(@value)

@opts = [
["#{UNIX_SOCKET_NAME}0", "#{UNIX_SOCKET_NAME}1"],
['localhost:43042', 'localhost:43043', 'localhost:43044'],
{
:buffer_requests => false,
:no_block => false,
:buffer_requests => true,
:no_block => true,
:noreply => true,
:namespace => "namespace"
}
]
Expand Down Expand Up @@ -81,7 +83,14 @@ def work
when "mixed"
@i.times do
@cache.set @key1, @value
@cache.set @key2, @value
@cache.get @key1
@cache.get @key3
@cache.get [@key1, @key2, @key3]
@cache.prepend @key1, @marshalled
@cache.prepend @key2, @marshalled
@cache.delete @key1
@cache.delete @key2
end
when "stats"
@i.times do
Expand Down Expand Up @@ -111,7 +120,7 @@ def work
raise "No such method"
end

unless @ignore_memory
if @with_memory == "true"
puts "*** Garbage collect. ***"
10.times do
GC.start
Expand Down
19 changes: 0 additions & 19 deletions test/profile/profile.rb

This file was deleted.

21 changes: 21 additions & 0 deletions test/profile/rb_profiler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

require "#{File.dirname(__FILE__)}/../setup"

$LOAD_PATH << "#{File.dirname(__FILE__)}/../../lib/"

ENV['CPUPROFILE_FREQUENCY'] = '500'
require 'memcached'
require 'rubygems'
require 'perftools'
require "#{HERE}/profile/exercise"

profile = "/tmp/memcached_#{Memcached::VERSION}_rb"
worker = Worker.new('mixed', 200000)

PerfTools::CpuProfiler.start("#{profile}.out") do
worker.work
end

system("pprof.rb --nodefraction=0.0000001 --text #{profile}.out")
system("pprof.rb --nodefraction=0.0000001 --edgefraction=0.0000001 --pdf #{profile}.out > #{profile}.pdf")
system("open #{profile}.pdf")
4 changes: 3 additions & 1 deletion test/profile/valgrind.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
exec("valgrind --tool=memcheck --leak-check=full --show-reachable=no --num-callers=15 --track-fds=yes --workaround-gcc296-bugs=yes --max-stackframe=7304328 --dsymutil=yes --track-origins=yes ruby -r#{File.dirname(__FILE__)}/test/profile/exercise.rb -e \"Worker.new(ENV['METHOD'], ENV['LOOPS'], ENV['IGNORE_MEMORY']).work\"")
require "#{File.dirname(__FILE__)}/../setup"

exec("valgrind --tool=memcheck --leak-check=full --show-reachable=no --num-callers=15 --track-fds=yes --workaround-gcc296-bugs=yes --max-stackframe=7304328 --dsymutil=yes --track-origins=yes ruby -r#{File.dirname(__FILE__)}/exercise.rb -e \"Worker.new('mixed', 10000, 'true').work\"")

0 comments on commit d486a34

Please sign in to comment.