Skip to content

Instantly share code, notes, and snippets.

@ricardochimal
Created July 17, 2009 04:52
Show Gist options
  • Save ricardochimal/148892 to your computer and use it in GitHub Desktop.
Save ricardochimal/148892 to your computer and use it in GitHub Desktop.
class AccessLog < CouchRest::ExtendedDocument
use_database db
property :method
property :host
property :uri
property :ip
property :user_agent
timestamps!
save_callback :before, :format_ip
def format_ip
self.ip = self.ip.split(",").first.strip
end
def safe_ip
octects = self.ip.split('.')
octects[3] = 'xxx'
octects.join('.')
end
def self.create_from_request(request)
log = AccessLog.new
log.ip = request.env['REMOTE_ADDR']
log.method = request.env['REQUEST_METHOD']
log.host = request.env['HTTP_HOST']
log.uri = request.env['REQUEST_URI']
log.user_agent = request.env['HTTP_USER_AGENT']
log.save
log
end
end
require 'couchrest'
def db
@db ||= CouchRest.database!(ENV['COUCHDB_URL'])
end
$LOAD_PATH.unshift << File.dirname(__FILE__)
require 'access_log'
require 'counting'
# write 1000 records to couch.io
$ heroku rake bench:write
(in /mnt/home/slugs/9554_68bace7_cd29/mnt)
11.9237477779388
# read + destroy 1000 records from couch.io
$ heroku rake bench:destroyplus
(in /mnt/home/slugs/9554_68bace7_cd29/mnt)
38.8893201351166
require 'rubygems'
require File.dirname(__FILE__) + '/main'
run Sinatra::Application
class Counting < CouchRest::ExtendedDocument
use_database db
property :num
timestamps!
end
require 'sinatra'
require File.dirname(__FILE__) + '/lib/all'
before do
AccessLog.create_from_request(request)
end
get '/' do
"Hello, I've been seen #{AccessLog.count} times. <a href=\"/latest\">View Latest Hits</a>"
end
get '/latest' do
@logs = AccessLog.all(:limit => 20)
erb :latest
end
task :environment do
require File.dirname(__FILE__) + '/lib/all'
end
require 'benchmark'
namespace :bench do
task :write => :environment do
puts Benchmark.realtime {
1000.times { |x| c = Counting.new; c.num = x; c.save; }
}
end
task :destroyplus => :environment do
puts Benchmark.realtime {
1000.times { |x| Counting.first.destroy }
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment