Best Test-prof_ruby code snippet using FactoryProf.flamegraph
factory_prof.rb
Source:factory_prof.rb  
1# frozen_string_literal: true2require "test_prof/factory_prof/printers/simple"3require "test_prof/factory_prof/printers/flamegraph"4require "test_prof/factory_prof/factory_builders/factory_bot"5require "test_prof/factory_prof/factory_builders/fabrication"6module TestProf7  # FactoryProf collects "factory stacks" that can be used to build8  # flamegraphs or detect most popular factories9  module FactoryProf10    FACTORY_BUILDERS = [FactoryBuilders::FactoryBot,11                        FactoryBuilders::Fabrication].freeze12    # FactoryProf configuration13    class Configuration14      attr_accessor :mode15      def initialize16        @mode = ENV["FPROF"] == "flamegraph" ? :flamegraph : :simple17      end18      # Whether we want to generate flamegraphs19      def flamegraph?20        @mode == :flamegraph21      end22    end23    class Result # :nodoc:24      attr_reader :stacks, :raw_stats25      def initialize(stacks, raw_stats)26        @stacks = stacks27        @raw_stats = raw_stats28      end29      # Returns sorted stats30      def stats31        @stats ||= @raw_stats.values32          .sort_by { |el| -el[:total_count] }33      end34      def total_count35        @total_count ||= @raw_stats.values.sum { |v| v[:total_count] }36      end37      def total_time38        @total_time ||= @raw_stats.values.sum { |v| v[:total_time] }39      end40      private41      def sorted_stats(key)42        @raw_stats.values43          .map { |el| [el[:name], el[key]] }44          .sort_by { |el| -el[1] }45      end46    end47    class << self48      include TestProf::Logging49      def config50        @config ||= Configuration.new51      end52      def configure53        yield config54      end55      # Patch factory lib, init vars56      def init57        @running = false58        log :info, "FactoryProf enabled (#{config.mode} mode)"59        FACTORY_BUILDERS.each(&:patch)60      end61      # Inits FactoryProf and setups at exit hook,62      # then runs63      def run64        init65        printer = config.flamegraph? ? Printers::Flamegraph : Printers::Simple66        at_exit { printer.dump(result) }67        start68      end69      def start70        reset!71        @running = true72      end73      def stop74        @running = false75      end76      def result77        Result.new(@stacks, @stats)78      end79      def track(factory)80        return yield unless running?81        @depth += 182        @current_stack << factory if config.flamegraph?83        @stats[factory][:total_count] += 184        @stats[factory][:top_level_count] += 1 if @depth == 185        t1 = TestProf.now86        begin87          yield88        ensure89          t2 = TestProf.now90          elapsed = t2 - t191          @stats[factory][:total_time] += elapsed92          @stats[factory][:top_level_time] += elapsed if @depth == 193          @depth -= 194          flush_stack if @depth.zero?95        end96      end97      private98      def reset!99        @stacks = [] if config.flamegraph?100        @depth = 0101        @stats = Hash.new do |h, k|102          h[k] = {103            name: k,104            total_count: 0,105            top_level_count: 0,106            total_time: 0.0,107            top_level_time: 0.0108          }109        end110        flush_stack111      end112      def flush_stack113        return unless config.flamegraph?114        @stacks << @current_stack unless @current_stack.nil? || @current_stack.empty?115        @current_stack = []116      end117      def running?118        @running == true119      end120    end121  end122end123TestProf.activate("FPROF") do124  TestProf::FactoryProf.run125end...flamegraph
Using AI Code Generation
1    User.create(name: 'John', age: 30)2    User.create(name: 'John', age: 30)3    User.create(name: 'John', age: 30)4    User.create(name: 'John', age: 30)5    User.create(name: 'John', age: 30)6    User.create(name: 'John', age: 30)7    User.create(name: 'John', age: 30)Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
