Skip to content

Instantly share code, notes, and snippets.

@errordeveloper
Created December 12, 2012 11:08
Show Gist options
  • Save errordeveloper/4267000 to your computer and use it in GitHub Desktop.
Save errordeveloper/4267000 to your computer and use it in GitHub Desktop.
Fake loggers
ruby-1.9.3-p194
source :rubygems
gem 'eventmachine'
require 'eventmachine'
$stdout.sync = true;
module FakeLogger
class Nginx
def initialize(as = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7) AppleWebKit/534.48.3 (KHTML, like Gecko) Version/5.1 Safari/534.48.3", ip = '192.168.23.32')
@client = { :as => as, :ip => ip };
end
def req(method, host, path, code)
# TODO: the extra space before '%d' is needed when %d is 1-9.
uuid = '90b7f69f7bfb41228dd6fd904583e524b2f7a324';
r = "\"#{method} #{path} HTTP/1.1\" #{code} 0 \"#{host}\"";
t = "[#{Time.now.strftime("%d/%b/%Y:%H:%M:%S %z")}]"
puts "#{Time.now.strftime("%b %d %H:%M:%S")} front0 nginx: #{@client[:ip]} - - #{t} #{r} \"#{@client[:as]}\" \"-\" - #{path} #{uuid}";
end
def rex(many, speed = 1)
many.each do |r|
EM.add_periodic_timer(speed*r[:period]) {
req(r[:method], r[:host], r[:path], r[:code]);
}
end
end
end
end
EM.run {
srv = [];
srv[0] = FakeLogger::Nginx.new(:ip=>'10.10.0.5');
srv[1] = FakeLogger::Nginx.new(:ip=>'10.10.0.7');
rqx = [
{ :period => 0.2,
:method => 'GET',
:host => 'http://example.org',
:path => '/index.html',
:code => 200 },
{ :period => 0.05,
:method => 'GET',
:host => 'http://example.com',
:path => '/index.html',
:code => 503 },
{ :period => 0.3,
:method => 'POST',
:host => 'http://example.com',
:path => '/test',
:code => 401 },
{ :period => 0.4,
:method => 'PUT',
:host => 'http://example.org',
:path => '/takeit.cgi?with=param',
:code => 200 }
];
srv[0].rex(rqx, 0.03);
srv[1].rex(rqx, 0.14);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment