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.



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 526

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.



525
526
527
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 525

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



525
526
527
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 525

def port
  @port
end

Instance Method Details

#ensure_databaseObject



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
602
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
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 577

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



672
673
674
675
676
677
678
679
680
681
682
683
684
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 672

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



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 542

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("schema")
  end

  yield if block_given?
end

#shutdownObject



664
665
666
667
668
669
670
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 664

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

#use_persistent_cache?Boolean

Returns:

  • (Boolean)


660
661
662
# File 'lib/groonga-query-log/command/run-regression-test.rb', line 660

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