Class: GroongaQueryLog::Command::RunRegressionTest::GroongaServer

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/groonga-query-log/command/run-regression-test.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#puts

Constructor Details

#initialize(groonga, groonga_options, groonga_env, database_path, options) ⇒ GroongaServer

Returns a new instance of GroongaServer.



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 548

def initialize(groonga,
               groonga_options,
               groonga_env,
               database_path,
               options)
  @input_directory = options[:input_directory] || Pathname.new(".")
  @working_directory = options[:working_directory] || Pathname.new(".")
  @groonga = groonga
  @groonga_options = groonga_options
  @groonga_env = groonga_env
  @database_path = @working_directory + database_path
  @host = "127.0.0.1"
  @port = find_unused_port
  @options = options
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



547
548
549
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 547

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



547
548
549
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 547

def port
  @port
end

Instance Method Details

#ensure_databaseObject



603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 603

def ensure_database
  if @options[:recreate_database]
    FileUtils.rm_rf(@database_path.dirname.to_s)
  end
  return if @database_path.exist?

  FileUtils.mkdir_p(@database_path.dirname.to_s)
  create_db_command = [@groonga, "-n", @database_path.to_s, "quit"]
  unless system(*create_db_command)
    create_db_command_line = create_db_command.join(" ")
    raise "Failed to run: #{create_db_command_line}"
  end

  load_files.each do |load_file|
    filter_command = nil
    case load_file.extname
    when ".rb"
      env = {
        "GROONGA_LOG_PATH" => log_path.to_s,
      }
      command = [
        RbConfig.ruby,
        load_file.to_s,
        @database_path.to_s,
      ]
    when ".zst"
      env = {}
      command = [
        @groonga,
        "--log-path", log_path.to_s,
        @database_path.to_s,
      ]
      filter_command = [
        "zstdcat",
        load_file.to_s,
      ]
    else
      env = {}
      command = [
        @groonga,
        "--log-path", log_path.to_s,
        "--file", load_file.to_s,
        @database_path.to_s,
      ]
    end
    command_line = command.join(" ")
    if filter_command
      filter_command_line = filter_command.join(" ")
      command_line = "#{filter_command_line} | #{command_line}"
    end
    puts("Running...: #{command_line}")
    status = nil
    if filter_command
      IO.pipe do |input, output|
        filter_pid = spawn(*filter_command, out: output)
        output.close
        pid = spawn(env, *command, in: input)
        input.close
        begin
          pid, status = Process.waitpid2(pid)
          filter_pid, _filter_status = Process.waitpid2(filter_pid)
        rescue Interrupt
          Process.kill(:TERM, pid)
          Process.kill(:TERM, filter_pid)
          pid, status = Process.waitpid2(pid)
          filter_pid, _filter_status = Process.waitpid2(filter_pid)
        end
      end
    else
      pid = spawn(env, *command)
      begin
        pid, status = Process.waitpid2(pid)
      rescue Interrupt
        Process.kill(:TERM, pid)
        pid, status = Process.waitpid2(pid)
      end
    end
    unless status.success?
      raise "Failed to run: #{command_line}"
    end
  end
end

#n_leaked_objectsObject



698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 698

def n_leaked_objects
  n = 0
  File.open(log_path, encoding: "UTF-8") do |log|
    log.each_line do |line|
      next unless line.valid_encoding?
      case line
      when /grn_fin \((\d+)\)/
        n += Integer($1, 10)
      end
    end
  end
  n
end

#runObject



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 564

def run
  return unless @options[:run_queries]

  spawn_args = []
  spawn_args << @groonga_env if @groonga_env
  spawn_args << @groonga
  spawn_args.concat(@groonga_options)
  spawn_args.concat(["--bind-address", @host])
  spawn_args.concat(["--port", @port.to_s])
  spawn_args.concat(["--protocol", "http"])
  spawn_args.concat(["--log-path", log_path.to_s])
  if @options[:output_query_log]
    spawn_args.concat(["--query-log-path", query_log_path.to_s])
  end
  spawn_args << "-s"
  spawn_args << @database_path.to_s
  @pid = spawn(*spawn_args)

  n_retries = 10
  begin
    send_command("status")
  rescue SystemCallError
    sleep(1)
    n_retries -= 1
    raise if n_retries.zero?
    retry
  end

  if @options[:warm_up]
    send_command("dump?dump_records=no")
    warm_up_commands = @options[:warm_up_commands] || []
    warm_up_commands.each do |command|
      send_command(command)
    end
  end

  yield if block_given?
end

#shutdownObject



690
691
692
693
694
695
696
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 690

def shutdown
  begin
    send_command("shutdown")
  rescue SystemCallError
  end
  Process.waitpid(@pid)
end

#use_persistent_cache?Boolean

Returns:

  • (Boolean)


686
687
688
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 686

def use_persistent_cache?
  @groonga_options.include?("--cache-base-path")
end