Created
March 22, 2023 12:56
-
-
Save ahukkanen/b963b55332a2917c8fe3a37a13aa0984 to your computer and use it in GitHub Desktop.
Test script for the Decidim surveys memory leak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
namespace :decidim do | |
namespace :surveys do | |
task test_memory_leak: :environment do | |
unless Rails.env.test? | |
puts %(Please run this with "RAILS_ENV=test") | |
next | |
end | |
puts "Cleaning the test database..." | |
Rake::Task["db:schema:load"].invoke | |
puts "" | |
puts "== Starting memory leak test" | |
puts "Creating the necessary structure for the test..." | |
organization = Decidim::Organization.create!( | |
name: "Memory leak test", | |
host: "memoryleak.lvh.me", | |
external_domain_whitelist: ["decidim.org", "github.com"], | |
description: { en: "<p>Test for memory leak.</p>" }, | |
default_locale: "en", | |
available_locales: ["en"], | |
reference_prefix: "MEMLEAK", | |
available_authorizations: [], | |
users_registration_mode: :enabled, | |
tos_version: Time.current, | |
badges_enabled: true, | |
user_groups_enabled: true, | |
send_welcome_notification: true, | |
file_upload_settings: Decidim::OrganizationSettings.default(:upload) | |
) | |
participatory_space = Decidim::ParticipatoryProcess.create!( | |
title: { en: "Memory leak" }, | |
slug: "memleak", | |
subtitle: { en: "Test for memory leak" }, | |
short_description: { en: "<p>Test for memory leak.</p>" }, | |
description: { en: "<p>Test for memory leak.</p>" }, | |
organization:, | |
published_at: Time.current, | |
start_date: Date.current, | |
end_date: 2.months.from_now | |
) | |
component = Decidim::Component.create!( | |
name: { en: "Survey" }, | |
manifest_name: :surveys, | |
published_at: Time.current, | |
participatory_space: | |
) | |
survey = Decidim::Surveys::Survey.new(component:) | |
questionnaire = Decidim::Forms::Questionnaire.create!( | |
title: { en: "Test survey" }, | |
description: { en: "<p>#{"a" * 1_000_000}</p>" }, | |
tos: { en: "<p>You need to accept testing.</p>" }, | |
questionnaire_for: survey | |
) | |
question = Decidim::Forms::Question.create!( | |
questionnaire:, | |
position: 0, | |
body: { en: "Answer this question" }, | |
question_type: "short_answer" | |
) | |
puts "Creating 3000 answers (this may take a while, please wait)..." | |
3000.times do |num| | |
Decidim::Forms::Answer.create!( | |
questionnaire:, | |
question:, | |
body: "Answer from user #{num}", | |
session_token: "token-#{num}", | |
ip_hash: "xyz" | |
) | |
end | |
puts "Serializing the answer sets..." | |
all_answers = Decidim::Forms::QuestionnaireUserAnswers.for(questionnaire) | |
initial_memory = memory_usage | |
all_answers.each_with_index do |answer_set, idx| | |
puts "Memory usage (#{idx}): #{memory_usage}" if (idx % 100).zero? | |
Decidim::Forms::UserAnswersSerializer.new(answer_set).serialize | |
end | |
puts "Memory usage (end): #{memory_usage}" | |
puts "Diff: #{memory_usage - initial_memory}" | |
puts "Cleaning the test database..." | |
Rake::Task["db:schema:load"].invoke | |
end | |
def memory_usage | |
`ps -o rss #{Process.pid}`.lines.last.to_i | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment