run_tests.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env ruby
  2. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  3. require 'rails'
  4. def run
  5. puts "# Zammad Performance Tests\n\n"
  6. run_tests!
  7. end
  8. def run_tests!
  9. puts 'Run test scenarios…'
  10. [].tap do |results|
  11. run_test_scenarios(results:)
  12. if results.any? { |r| r[:failed] }
  13. puts 'Tests failed.'
  14. exit 1
  15. end
  16. puts 'All tests were successful.'
  17. end
  18. end
  19. def run_test_scenarios(results:)
  20. agent = User.with_permissions('ticket.agent').first
  21. expect(title: 'assets:precompile', max_time: 120, max_sql_queries: nil, results:) do
  22. system('RAILS_ENV=production bundle exec rails assets:precompile > /dev/null 2>&1', exception: true)
  23. end
  24. max_index_queries = version_oldstable? ? 70 : 35
  25. expect(title: 'Ticket::Overviews.index', max_time: 1, max_sql_queries: max_index_queries, results:) do
  26. Ticket::Overviews.index(agent)
  27. end
  28. expect(title: 'Ticket::Overviews.all', max_time: 0.05, max_sql_queries: 5, results:) do
  29. Ticket::Overviews.all(current_user: agent)
  30. end
  31. max_overview_list_time = 20
  32. max_overview_list_queries = 25_000
  33. puts "Current ticket count: #{Ticket.count}"
  34. expect(title: 'Sessions::Backend::TicketOverviewList#push', max_time: max_overview_list_time, max_sql_queries: max_overview_list_queries, results:) do
  35. Sessions::Backend::TicketOverviewList.new(agent, {}).push
  36. end
  37. end
  38. def version_oldstable?
  39. Version.get.match?(%r{^6\.0\.})
  40. end
  41. def expect(title:, max_time:, max_sql_queries:, results:, &block)
  42. ActiveRecord::Base.connection.query_cache.clear
  43. Rails.cache.clear
  44. sql_queries = 0
  45. failed = false
  46. callback = ->(_name, _start, _finish, _id, payload) { sql_queries += 1 if !payload[:cached] }
  47. time = Benchmark.measure do
  48. ActiveSupport::Notifications.subscribed(callback, 'sql.active_record', &block)
  49. end
  50. puts " #{title}: #{time.real}s (#{sql_queries} queries)"
  51. if max_time && time.real > max_time
  52. puts " ERROR: took #{time.real}s, rather than expected maximum of #{max_time}s"
  53. failed = true
  54. end
  55. if max_sql_queries && sql_queries > max_sql_queries
  56. puts " ERROR: caused #{sql_queries} SQL queries, rather than expected maximum of #{max_sql_queries}"
  57. failed = true
  58. end
  59. results.push({ title:, max_time:, time: time.real, max_sql_queries:, sql_queries:, failed: })
  60. end
  61. run