<commit_before>require 'appsignal'
require 'benchmark'

class ::Appsignal::Event::ActiveRecordEvent
  def connection_config; {:adapter => 'mysql'}; end
end

namespace :benchmark do
  task :all => [:run_inactive, :run_active] do
  end

  task :run_inactive do
    puts 'Running with appsignal off'
    ENV['APPSIGNAL_PUSH_API_KEY'] = nil
    subscriber = ActiveSupport::Notifications.subscribe do |*args|
      # Add a subscriber so we can track the overhead of just appsignal
    end
    run_benchmark
    ActiveSupport::Notifications.unsubscribe(subscriber)
  end

  task :run_active do
    puts 'Running with appsignal on'
    ENV['APPSIGNAL_PUSH_API_KEY'] = 'something'
    run_benchmark
  end
end

def run_benchmark
  puts 'Initializing'
  Appsignal.config = Appsignal::Config.new('', 'production')
  Appsignal.start
  puts "Appsignal #{Appsignal.active? ? 'active' : 'not active'}"

  puts 'Running 10_000 normal transactions'
  puts(Benchmark.measure do
    10_000.times do |i|
      Appsignal::Transaction.create("transaction_#{i}", {})

      ActiveSupport::Notifications.instrument('sql.active_record', :sql => 'SELECT `users`.* FROM `users` WHERE `users`.`id` = ?')
      10.times do
        ActiveSupport::Notifications.instrument('sql.active_record', :sql => 'SELECT `todos`.* FROM `todos` WHERE `todos`.`id` = ?')
      end

      ActiveSupport::Notifications.instrument('render_template.action_view', :identifier => 'app/views/home/show.html.erb') do
        5.times do
          ActiveSupport::Notifications.instrument('render_partial.action_view', :identifier => 'app/views/home/_piece.html.erb') do
            3.times do
              ActiveSupport::Notifications.instrument('cache.read')
            end
          end
        end
      end

      ActiveSupport::Notifications.instrument(
        'process_action.action_controller',
        :controller => 'HomeController',
        :action     => 'show',
        :params     => {:id => 1}
      )

      Appsignal::Transaction.complete_current!
    end
  end)

  if Appsignal.active?
    puts "Running aggregator post_processed_queue! for #{Appsignal.agent.aggregator.queue.length} transactions"
    puts(Benchmark.measure do
      Appsignal.agent.aggregator.post_processed_queue!.to_json
    end)
  end
end<commit_msg>Show object counts in benchmark<commit_after>require 'appsignal'
require 'benchmark'

class ::Appsignal::Event::ActiveRecordEvent
  def connection_config; {:adapter => 'mysql'}; end
end

namespace :benchmark do
  task :all => [:run_inactive, :run_active] do
  end

  task :run_inactive do
    puts 'Running with appsignal off'
    ENV['APPSIGNAL_PUSH_API_KEY'] = nil
    subscriber = ActiveSupport::Notifications.subscribe do |*args|
      # Add a subscriber so we can track the overhead of just appsignal
    end
    run_benchmark
    ActiveSupport::Notifications.unsubscribe(subscriber)
  end

  task :run_active do
    puts 'Running with appsignal on'
    ENV['APPSIGNAL_PUSH_API_KEY'] = 'something'
    run_benchmark
  end
end

def run_benchmark
  total_objects = ObjectSpace.count_objects[:TOTAL]
  puts "Initializing, currently #{total_objects} objects"
  Appsignal.config = Appsignal::Config.new('', 'production')
  Appsignal.start
  puts "Appsignal #{Appsignal.active? ? 'active' : 'not active'}"

  puts 'Running 10_000 normal transactions'
  puts(Benchmark.measure do
    10_000.times do |i|
      Appsignal::Transaction.create("transaction_#{i}", {})

      ActiveSupport::Notifications.instrument('sql.active_record', :sql => 'SELECT `users`.* FROM `users` WHERE `users`.`id` = ?')
      10.times do
        ActiveSupport::Notifications.instrument('sql.active_record', :sql => 'SELECT `todos`.* FROM `todos` WHERE `todos`.`id` = ?')
      end

      ActiveSupport::Notifications.instrument('render_template.action_view', :identifier => 'app/views/home/show.html.erb') do
        5.times do
          ActiveSupport::Notifications.instrument('render_partial.action_view', :identifier => 'app/views/home/_piece.html.erb') do
            3.times do
              ActiveSupport::Notifications.instrument('cache.read')
            end
          end
        end
      end

      ActiveSupport::Notifications.instrument(
        'process_action.action_controller',
        :controller => 'HomeController',
        :action     => 'show',
        :params     => {:id => 1}
      )

      Appsignal::Transaction.complete_current!
    end
  end)

  if Appsignal.active?
    puts "Running aggregator post_processed_queue! for #{Appsignal.agent.aggregator.queue.length} transactions"
    puts(Benchmark.measure do
      Appsignal.agent.aggregator.post_processed_queue!.to_json
    end)
  end

  puts "Done, currently #{ObjectSpace.count_objects[:TOTAL] - total_objects} objects created"
end
{'fraction_non_alphanumeric': 0.11602094240837696, 'fraction_numerical': 0.0062827225130890054, 'mean_word_length': 3.561604584527221, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '566554', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1441, 'n_words': 319}