# frozen_string_literal: true require "benchmark" require "difftastic" require "lipgloss" require "yaml" require "reactionview/template/handlers/herb/herb" require_relative "../../test/engine/action_view/action_view_test_helper" module Bench module BenchmarkHelper include Engine::ActionViewTestHelper LABEL_WIDTH = 50 RENDER_ITERATIONS = 5000 COMPILE_ITERATIONS = 500 WARMUP_ITERATIONS = 100 def bold(str) = "\e[1m#{str}\e[0m" def dimmed(str) = "\e[2m#{str}\e[0m" def green(str) = "\e[32m#{str}\e[0m" def red(str) = "\e[31m#{str}\e[0m" class ActionViewHelperCounter < Herb::Visitor attr_reader :count def initialize super @count = 0 end def visit_html_element_node(node) @count += 1 if node.element_source && node.element_source != "HTML" super end end def count_action_view_helpers(ast) counter = ActionViewHelperCounter.new counter.visit(ast) counter.count end def fresh_action_view_context lookup_context = ::ActionView::LookupContext.new([]) view = ::ActionView::Base.with_empty_template_cache.new(lookup_context, {}, nil) view.class.include(Turbo::FramesHelper) if defined?(Turbo::FramesHelper) view.class.include(Engine::ActionViewTestHelper::SimpleUrlFor) view end def bar(ratio, width: 40) filled = [(ratio * width).round, width].min "#{green("█" * filled)}#{dimmed("░" * (width - filled))}" end def per_iteration_label(total_seconds, iterations, unit) format("(%