Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test with Valgrind using the ruby_memcheck gem #162

Merged
merged 3 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/liquid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,19 @@ jobs:
env:
LIQUID_C_PEDANTIC: 'true'
- run: bundle exec rubocop

valgrind:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.0'
- run: sudo apt-get install -y valgrind
- uses: actions/cache@v1
with:
path: vendor/bundle
key: ${{ runner.os }}-gems-${{ hashFiles('Gemfile') }}
restore-keys: ${{ runner.os }}-gems-
- run: bundle install --jobs=3 --retry=3 --path=vendor/bundle
- run: bundle exec rake test:valgrind
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ group :test do
gem 'rubocop-shopify', '~> 1.0.6', require: false
gem 'spy', '0.4.1'
gem 'benchmark-ips'
gem 'ruby_memcheck'
end

group :development do
Expand Down
96 changes: 3 additions & 93 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,106 +4,16 @@ require 'rake/testtask'
require 'bundler/gem_tasks'
require 'rake/extensiontask'
require 'benchmark'
require 'ruby_memcheck'

ENV['DEBUG'] ||= 'true'
ext_task = Rake::ExtensionTask.new("liquid_c")

# For MacOS, generate debug information that ruby can read
dsymutil = RbConfig::CONFIG['dsymutil']
unless dsymutil.to_s.empty?
ext_lib_path = "lib/#{ext_task.binary}"
dsym_path = "#{ext_lib_path}.dSYM"

file dsym_path => [ext_lib_path] do
sh dsymutil, ext_lib_path
end
Rake::Task['compile'].enhance([dsym_path])
end
RubyMemcheck.config(binary_name: "liquid_c")

task default: [:test, :rubocop]

task test: ['test:unit', 'test:integration:all']

namespace :test do
Rake::TestTask.new(unit: :compile) do |t|
t.libs << 'lib' << 'test'
t.test_files = FileList['test/unit/**/*_test.rb']
end

desc 'run test suite with default parser'
Rake::TestTask.new(integration: :compile) do |t|
t.libs << 'lib'
t.test_files = ['test/integration_test.rb']
end

namespace :integration do
integration_test_with_env = lambda do |env_vars|
proc do
old_env_values = ENV.to_hash.slice(*env_vars.keys)
task = Rake::Task['test:integration']
begin
env_vars.each { |key, value| ENV[key] = value }
task.invoke
ensure
old_env_values.each { |key, value| ENV[key] = value }
task.reenable
end
end
end

task :lax, &integration_test_with_env.call('LIQUID_PARSER_MODE' => 'lax')

task :strict, &integration_test_with_env.call('LIQUID_PARSER_MODE' => 'strict')

task :without_vm, &integration_test_with_env.call('LIQUID_C_DISABLE_VM' => 'true')

task all: [:lax, :strict, :without_vm]
end
end

task :rubocop do
require 'rubocop/rake_task'
RuboCop::RakeTask.new
end

namespace :benchmark do
desc "Run the liquid benchmark with lax parsing"
task :run do
ruby "./performance.rb c benchmark lax"
end

desc "Run the liquid benchmark with strict parsing"
task :strict do
ruby "./performance.rb c benchmark strict"
end
end

namespace :c_profile do
%i(run compile render).each do |task_name|
task(task_name) do
ruby "./performance/c_profile.rb #{task_name}"
end
end
end

namespace :profile do
desc "Run the liquid profile/performance coverage"
task :run do
ruby "./performance.rb c profile lax"
end

desc "Run the liquid profile/performance coverage with strict parsing"
task :strict do
ruby "./performance.rb c profile strict"
end
end

namespace :compare do
%w(lax warn strict).each do |type|
desc "Compare Liquid to Liquid-C in #{type} mode"
task type.to_sym do
ruby "./performance.rb bare benchmark #{type}"
ruby "./performance.rb c benchmark #{type}"
end
end
task valgrind: ['test:unit:valgrind', 'test:integration:valgrind:all']
end
15 changes: 15 additions & 0 deletions rakelib/compile.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

ext_task = Rake::ExtensionTask.new("liquid_c")

# For MacOS, generate debug information that ruby can read
dsymutil = RbConfig::CONFIG['dsymutil']
unless dsymutil.to_s.empty?
ext_lib_path = "lib/#{ext_task.binary}"
dsym_path = "#{ext_lib_path}.dSYM"

file dsym_path => [ext_lib_path] do
sh dsymutil, ext_lib_path
end
Rake::Task['compile'].enhance([dsym_path])
end
43 changes: 43 additions & 0 deletions rakelib/integration_test.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

namespace :test do
integration_test_config = lambda do |t|
t.libs << 'lib'
t.test_files = ['test/integration_test.rb']
end

desc 'run test suite with default parser'
Rake::TestTask.new(integration: :compile, &integration_test_config)

namespace :integration do
define_env_integration_tests = lambda do |task_name|
rake_task = Rake::Task[task_name]

[
[:lax, { 'LIQUID_PARSER_MODE' => 'lax' }],
[:strict, { 'LIQUID_PARSER_MODE' => 'strict' }],
[:without_vm, { 'LIQUID_C_DISABLE_VM' => 'true' }],
].each do |name, env_vars|
task(name) do
old_env_values = ENV.to_hash.slice(*env_vars.keys)
begin
env_vars.each { |key, value| ENV[key] = value }
rake_task.invoke
ensure
old_env_values.each { |key, value| ENV[key] = value }
rake_task.reenable
end
end
end

task(all: [:lax, :strict, :without_vm])
end

define_env_integration_tests.call('test:integration')

RubyMemcheck::TestTask.new(valgrind: :compile, &integration_test_config)
namespace :valgrind do
define_env_integration_tests.call('test:integration:valgrind')
end
end
end
43 changes: 43 additions & 0 deletions rakelib/performance.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

namespace :benchmark do
desc "Run the liquid benchmark with lax parsing"
task :run do
ruby "./performance.rb c benchmark lax"
end

desc "Run the liquid benchmark with strict parsing"
task :strict do
ruby "./performance.rb c benchmark strict"
end
end

namespace :c_profile do
%i(run compile render).each do |task_name|
task(task_name) do
ruby "./performance/c_profile.rb #{task_name}"
end
end
end

namespace :profile do
desc "Run the liquid profile/performance coverage"
task :run do
ruby "./performance.rb c profile lax"
end

desc "Run the liquid profile/performance coverage with strict parsing"
task :strict do
ruby "./performance.rb c profile strict"
end
end

namespace :compare do
%w(lax warn strict).each do |type|
desc "Compare Liquid to Liquid-C in #{type} mode"
task type.to_sym do
ruby "./performance.rb bare benchmark #{type}"
ruby "./performance.rb c benchmark #{type}"
end
end
end
6 changes: 6 additions & 0 deletions rakelib/rubocop.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

task :rubocop do
require 'rubocop/rake_task'
RuboCop::RakeTask.new
end
14 changes: 14 additions & 0 deletions rakelib/unit_test.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

namespace :test do
unit_test_config = lambda do |t|
t.libs << 'lib' << 'test'
t.test_files = FileList['test/unit/**/*_test.rb']
end

Rake::TestTask.new(unit: :compile, &unit_test_config)

namespace :unit do
RubyMemcheck::TestTask.new(valgrind: :compile, &unit_test_config)
end
end
2 changes: 2 additions & 0 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

at_exit { GC.start }

require_relative 'liquid_test_helper'

test_files = Dir[File.join(LIQUID_TEST_DIR, 'integration/**/*_test.rb')]
Expand Down
3 changes: 3 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

at_exit { GC.start }

require 'minitest/autorun'
require 'liquid/c'

Expand Down