Skip to content

Commit

Permalink
Add basic memory usage profile (#1137)
Browse files Browse the repository at this point in the history
This commit:

- adds a rake task to profile basic memory usage; and
- configures Travis to perform a memory usage profile on CI builds.
  • Loading branch information
ashmaroli authored and pyrmont committed Jun 1, 2019
1 parent a3d4091 commit 579e634
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
7 changes: 6 additions & 1 deletion .travis.yml
Expand Up @@ -8,10 +8,15 @@ rvm:
- "2.3"
- "2.4"
- "2.5"
- "2.6"
- &latest_ruby "2.6"
cache:
bundler: true
bundler_args: --without development
script:
- bundle exec rake
- bundle exec rubocop
matrix:
include:
- rvm : *latest_ruby
name : Profile Memory Allocation
script: bundle exec rake profile_memory
5 changes: 5 additions & 0 deletions Gemfile
Expand Up @@ -15,6 +15,11 @@ gem 'rubocop', '~> 0.49.1'
# don't try to install redcarpet under jruby
gem 'redcarpet', :platforms => :ruby

# Profiling
if RUBY_VERSION >= '2.3.0'
gem 'memory_profiler', :require => false
end

group :development do
gem 'pry'

Expand Down
31 changes: 31 additions & 0 deletions Rakefile
Expand Up @@ -39,3 +39,34 @@ task :default => :spec
Dir.glob(Pathname.new(__FILE__).dirname.join('tasks/*.rake')).each do |f|
load f
end

task :profile_memory do
lib = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require 'memory_profiler'

source = File.expand_path('lib/rouge/lexer.rb', __dir__)
sample = File.read(source, encoding: 'utf-8')
report = MemoryProfiler.report do
require 'rouge'
formatter = Rouge::Formatters::HTML.new
lexer = Rouge::Lexers::Ruby.new
formatter.format(lexer.lex(source))
end
print_options = { :scale_bytes => true }

if ENV['CI']
report.pretty_print(print_options)
else
results_file = File.expand_path('rouge-memory.tmp')
t_allocated = report.scale_bytes(report.total_allocated_memsize)
t_retained = report.scale_bytes(report.total_retained_memsize)
puts
puts "Total allocated: #{t_allocated} (#{report.total_allocated} objects)"
puts "Total retained: #{t_retained} (#{report.total_retained} objects)"
report.pretty_print(print_options.merge(to_file: results_file))
puts
puts "Detailed report saved to file: #{results_file}"
end
end

0 comments on commit 579e634

Please sign in to comment.