Skip to content
This repository has been archived by the owner on May 28, 2023. It is now read-only.

Commit

Permalink
working PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy B committed May 14, 2020
1 parent ef15dfa commit c5744a6
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 28 deletions.
6 changes: 0 additions & 6 deletions .travis.yml

This file was deleted.

48 changes: 48 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
PATH
remote: .
specs:
foot_traffic (0.1.0)
ferrum (~> 0.8)

GEM
remote: https://rubygems.org/
specs:
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
cliver (0.3.2)
concurrent-ruby (1.1.6)
diff-lcs (1.3)
ferrum (0.8)
addressable (~> 2.6)
cliver (~> 0.3)
concurrent-ruby (~> 1.1)
websocket-driver (>= 0.6, < 0.8)
public_suffix (4.0.5)
rake (12.3.3)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.2)
rspec-support (~> 3.9.3)
rspec-expectations (3.9.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.3)
websocket-driver (0.7.1)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.4)

PLATFORMS
ruby

DEPENDENCIES
foot_traffic!
rake (~> 12.0)
rspec (~> 3.0)

BUNDLED WITH
2.1.4
33 changes: 17 additions & 16 deletions foot_traffic.gemspec
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
require_relative 'lib/foot_traffic/version'
require_relative "lib/foot_traffic/version"

Gem::Specification.new do |spec|
spec.name = "foot_traffic"
spec.version = FootTraffic::VERSION
spec.authors = ["Andy B"]
spec.email = ["[email protected]"]
spec.name = "foot_traffic"
spec.version = FootTraffic::VERSION
spec.authors = ["Andy B"]
spec.email = ["[email protected]"]

spec.summary = %q{TODO: Write a short summary, because RubyGems requires one.}
spec.description = %q{TODO: Write a longer description or delete this line.}
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.license = "MIT"
spec.summary = "Control a fleet of Chromes from a Ruby script. Built on Ferrum."
spec.description = "Foot Traffic allows to simulate real web users for load testing, debugging, or feature discovery"
spec.homepage = "https://github.com/lewagon/foot_traffic"
spec.license = "MIT"
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")

spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
spec.metadata["source_code_uri"] = "https://github.com/lewagon/foot_traffic"
spec.metadata["changelog_uri"] = "https://github.com/lewagon/foot_traffic/CHANGELOG"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "ferrum", "~> 0.8"
spec.add_development_dependency "rspec", "~> 3.2"
end
4 changes: 2 additions & 2 deletions lib/foot_traffic.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "foot_traffic/version"
require "foot_traffic/session"

module FootTraffic
class Error < StandardError; end
# Your code goes here...
class ResourceOverloadError < StandardError; end
end
20 changes: 20 additions & 0 deletions lib/foot_traffic/refinements.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "ferrum"

module FootTraffic
refine Ferrum::Context do
def new_tab
create_page
end

def in_thread(&block)
Thread.new do
block.call(create_page)
rescue ThreadError, RuntimeError, Errno::EMFILE, Errno::ECONNRESET
raise ResourceOverloadError
end
end

alias_method :with_tab, :in_thread
alias_method :tab_thread, :in_thread
end
end
55 changes: 55 additions & 0 deletions lib/foot_traffic/session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require_relative "refinements"
require "ferrum"

module FootTraffic
class ThreadPool
def initialize
@threads = []
end

def <<(thread)
@threads << thread
end

def wait
@threads.map(&:join)
end
end

class Session
def self.start(options: {}, duration: nil, clones: 1, quit: false, &block)
new(options).start(duration: duration, clones: clones, quit: quit, &block)
end

def initialize(opts)
opts[:headless] ||= false
@browser ||= Ferrum::Browser.new(opts)
end

def start(duration: nil, clones: 1, quit: false, &block)
main = Thread.new {
threads = []
clones.times do
threads << Thread.new {
window = @browser.contexts.create
block.call(window, ThreadPool.new)
}
end
threads.map(&:join)
}

# A sleeping thread to keep Ferrum open for a given period of time
unless quit
wait = Thread.new {
duration.nil? ? sleep : sleep(duration)
}
wait.join
end

main.join
@browser.quit
rescue ThreadError, RuntimeError, Errno::EMFILE, Errno::ECONNRESET
raise ResourceOverloadError
end
end
end
4 changes: 0 additions & 4 deletions spec/foot_traffic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@
it "has a version number" do
expect(FootTraffic::VERSION).not_to be nil
end

it "does something useful" do
expect(false).to eq(true)
end
end

0 comments on commit c5744a6

Please sign in to comment.