Class: Ritsudo::Benchmark

Inherits:
Object
  • Object
show all
Defined in:
lib/ritsudo/benchmark.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collector: Ritsudo::Collector.new) ⇒ Benchmark

Returns a new instance of Benchmark.



4
5
6
# File 'lib/ritsudo/benchmark.rb', line 4

def initialize(collector: Ritsudo::Collector.new)
  @collector = collector
end

Instance Attribute Details

#collectorObject (readonly)

Returns the value of attribute collector.



3
4
5
# File 'lib/ritsudo/benchmark.rb', line 3

def collector
  @collector
end

Instance Method Details

#collect(url, wait:, sub_process_timeout:, driver_options:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ritsudo/benchmark.rb', line 47

def collect(url, wait:, sub_process_timeout:, driver_options:)
  drive(driver_options) do |driver|
    driver.get(url)
    sleep(wait)
    sleep(1) until driver.driver.execute_script("return window.performance.timing.loadEventEnd")

    requests = collect_requests_wait_finish(driver, sub_process_timeout: sub_process_timeout)
    @collector.add(requests)

    dom_content_loaded_script = "return window.performance.timing.domContentLoadedEventEnd - window.performance.timing.requestStart"
    dom_content_loaded = driver.driver.execute_script(dom_content_loaded_script)
    @collector.add_misc("Misc", "DomContentLoaded", dom_content_loaded)

    loaded_script = "return window.performance.timing.loadEventEnd - window.performance.timing.requestStart"
    loaded = driver.driver.execute_script(loaded_script)
    @collector.add_misc("Misc", "Loaded", loaded)
  end
end

#collect_requests_wait_finish(driver, sub_process_timeout:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ritsudo/benchmark.rb', line 26

def collect_requests_wait_finish(driver, sub_process_timeout:)
  messages = driver.manage.logs.get('performance')
  requests = Ritsudo::Request.grouping(messages)
  Timeout.timeout(sub_process_timeout) do
    while requests.find(&:processing?)
      sleep(0.5)
      messages = driver.manage.logs.get('performance') + messages
      requests = Ritsudo::Request.grouping(messages)
    end
  end
  requests
rescue Timeout::Error
  requests
end

#do(url, count: 5, wait: 1, sub_process_timeout: 3, driver_options: {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ritsudo/benchmark.rb', line 8

def do(url, count: 5, wait: 1, sub_process_timeout: 3, driver_options: {})
  if driver_options[:cookies]
    puts <<-EOS 
WARNING: headless chrome can not set cookie before access. Ritsudo accesses root path before requested path for set cookies.

    EOS
  end

  default_driver_options = { timeout: 5, wait_time: 1 }
  puts "Ritsudo requests #{count} times: #{url}"
  count.times do
    print(".")
    collect(url, wait: wait, sub_process_timeout: sub_process_timeout,
            driver_options: default_driver_options.merge(driver_options))
  end
  puts ""
end

#drive(driver_options) {|driver| ... } ⇒ Object

Yields:

  • (driver)


41
42
43
44
45
# File 'lib/ritsudo/benchmark.rb', line 41

def drive(driver_options, &block)
  driver = Ritsudo::Driver.new(driver_options)
  yield(driver)
  driver.driver.close()
end