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

Profile memory usage from processing HTML contents #2088

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/actions/munge_gemfile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

CONTENTS = <<~RUBY
source 'https://rubygems.org'
gemspec
RUBY

File.open('Gemfile', 'wb') { |f| f.puts CONTENTS }
38 changes: 38 additions & 0 deletions .github/workflows/memprof.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Memory Profile
on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
memory_profile:
if: "!contains(github.event.commits[0].message, '[ci skip]')"
runs-on: "ubuntu-latest"
steps:
- uses: actions/checkout@master
- uses: actions/setup-ruby@v1
with:
ruby-version: '2.7'
- name: Set up Dependencies Cache
uses: actions/cache@v1
with:
path: vendor/bundle
key: ubuntu-latest-gems
restore-keys: |
ubuntu-latest-gems-
- name: Install dependencies
run: |
bundle config path vendor/bundle
bundle install -j8
bundle exec rake gem:spec
bundle exec ruby .github/workflows/actions/munge_gemfile.rb
bundle install -j8
- name: Compile Nokogiri
run: bundle exec rake compile
- name: Memory Profile of parsing and serializing HTML Document
run: bundle exec rake memprof:html_doc
- name: Memory Profile of parsing and serializing HTML Document Fragment
run: bundle exec rake memprof:html_doc_frag
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ gem "hoe-debugging", "~>2.0", :group => [:development, :test]
gem "hoe-gemspec", "~>1.0", :group => [:development, :test]
gem "hoe-git", "~>1.6", :group => [:development, :test]
gem "hoe-markdown", "~>1.1", :group => [:development, :test]
gem "memory_profiler", "~>0.9", :group => [:development, :test]
gem "minitest", "~>5.8", :group => [:development, :test]
gem "racc", "~>1.5.0", :group => [:development, :test]
gem "rake", "~>13.0", :group => [:development, :test]
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ HOE = Hoe.spec 'nokogiri' do
["hoe-gemspec", "~> 1.0"],
["hoe-git", "~> 1.6"],
["hoe-markdown", "~> 1.1"],
["memory_profiler", "~> 0.9"],
["minitest", "~> 5.8"],
["racc", "~> 1.5.0"],
["rake", "~> 13.0"],
Expand All @@ -80,6 +81,7 @@ require_relative "tasks/debug"
require_relative "tasks/docker"
require_relative "tasks/docs-linkify"
require_relative "tasks/rubocop"
require_relative "tasks/memprof"
require_relative "tasks/set-version-to-timestamp"

# work around Hoe's inflexibility about the default tasks
Expand Down
34 changes: 34 additions & 0 deletions tasks/memprof.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

namespace :memprof do
require "memory_profiler"
module NokoMemProf
def self.profile abs_path
puts "\nTesting with Nokogiri v" + Nokogiri::VERSION.to_s
puts
contents = File.read abs_path
MemoryProfiler.report do
1000.times { yield contents }
end.pretty_print(scale_bytes: true, normalize_paths: true)
end
end


task :html_doc do
require "nokogiri"

abs_path = File.expand_path("../test/files/fixture.html", __dir__)
NokoMemProf.profile(abs_path) do |contents|
Nokogiri::HTML::Document.parse(contents).to_html
end
end

task :html_doc_frag do
require "nokogiri"

abs_path = File.expand_path("../test/files/fixture_body.html", __dir__)
NokoMemProf.profile(abs_path) do |contents|
Nokogiri::HTML::DocumentFragment.parse(contents).to_html
end
end
end