Permalink
Browse files

basic benchmarker and stats

  • Loading branch information...
1 parent c7d6dbb commit 17abb8ed24c3d4ff7f4366ca6bd5a62a1058a7d7 Nick Kallen committed Apr 16, 2009
Showing with 69 additions and 16 deletions.
  1. +18 −11 jb
  2. +12 −3 joke_cluster.rb
  3. +6 −2 joke_server.rb
  4. +33 −0 util/stats.rb
View
29 jb
@@ -4,27 +4,34 @@ require 'benchmark'
require 'rubygems'
require 'activesupport'
require 'socket'
+require 'optparse'
+require 'ostruct'
-count = 10000
-concurrency = 10
-count_per_worker = count / concurrency
+options = {
+ :concurrency => 1,
+ :count => 100
+}
+OptionParser.new do |opts|
+ opts.on('-n', "--number COUNT", Integer) { |count| options[:count] = count }
+ opts.on('-c', "--concurrency CONCURRENCY", Integer) { |concurrency| options[:concurrency] = concurrency }
+ opts.on('-p', "--port PORT", Integer) { |port| options[:port] = port }
+end.parse!
-host = "0.0.0.0"
-port = 10001
+count_per_worker = options[:count] / options[:concurrency]
benchmark = Benchmark.measure do
threads = []
- concurrency.times do
+ options[:concurrency].times do
threads << Thread.new do
- count_per_worker.times do
- socket = TCPSocket.new(host, port)
+ socket = TCPSocket.new("0.0.0.0", options[:port])
+ count_per_worker.times do |i|
socket.print("\n")
- response = socket.gets
+ socket.gets
end
end
end
threads.each(&:join)
end
-puts "#{benchmark.real}"
-puts "#{benchmark.real / count_per_worker}"
+mean = benchmark.real / count_per_worker
+puts "Requests per second:\t%2f [#/sec] (mean)" % mean
View
@@ -1,6 +1,15 @@
-count = 10
+#!/usr/bin/env ruby
-count.times do |count|
- port = 1000 + count
+options = {
+ :count => 10,
+ :base_port => 1000
+}
+OptionParser.new do |opts|
+ opts.on('-n', "--number COUNT", Integer) { |count| options[:count] = count }
+ opts.on('-p', "--port PORT", Integer) { |count| options[:port] = port }
+end.parse!
+
+options[:count].times do |count|
+ port = options[:base_port] + count
System.fork("./joke_server.rb -p#{port}")
end
View
@@ -2,11 +2,15 @@
require 'rubygems'
require 'eventmachine'
+require 'util/stats'
+
+$stats = Stats.new(['job_user', 'job_sys', 'job_real'], Logger.new(STDOUT))
module JokeServer
def receive_data(data)
- send_data("knock knock\n")
- # output w3c data with user, sys, and real
+ $stats.transaction do
+ $stats.measure('job') { send_data("knock knock\n") }
+ end
end
end
View
@@ -0,0 +1,33 @@
+require 'activesupport'
+
+class Stats
+ attr_accessor :transaction_id
+
+ def initialize(fields, logger)
+ @fields = fields
+ @values = {}
+ @logger = logger
+ @logger.info("# Fields: " + fields.join(" "))
+ end
+
+ def measure(field, &block)
+ measurement = Benchmark.measure(&block)
+ @values["#{field}_user"] = measurement.utime
+ @values["#{field}_sys"] = measurement.stime
+ @values["#{field}_real"] = measurement.real
+ end
+
+ def transaction
+ @transaction_id = "#{Process.pid}-#{Time.now.to_i}-#{rand(9999)}"
+ yield
+ print
+ @values = {}
+ end
+
+ private
+ def print
+ prefix = [Time.now.iso8601, @transaction_id].join(" ")
+ info = @fields.collect { |field| @values[field] || "-" }.join(" ")
+ @logger.info(prefix + " " + info)
+ end
+end

0 comments on commit 17abb8e

Please sign in to comment.