Skip to content

Instantly share code, notes, and snippets.

View adamlwatson's full-sized avatar

Adam Watson adamlwatson

View GitHub Profile
@adamlwatson
adamlwatson / Pre-requisites
Created November 15, 2011 18:19
Testing throughput bottlenecks in Goliath from Mongoid gem
0. gem install goliath (should be v0.9.4 as of writing this)
1. gem install mongoid (should be v2.3.3 as of writing this)
2. create and save test.rb file (see below)
3. % ruby test.rb -sv
4. % ab -n500 -c10 http://localhost:9000/status
5. note the req/s results (somewhere around 80-90 req/s on my machine)
6. edit the test.rb file, remove the 'require mongoid' line
7. repeat steps 3 and 4.
8. note the req/s results (somewhere aaround 550 req/s on my machine)
@adamlwatson
adamlwatson / gist:1371133
Created November 16, 2011 19:49
Workaround for multiple regex patterns against a variable parameter segment in a url mapping in Goliath
class RackRoutes < Goliath::API
get '/item/:value/description' do
if params[:value].match /^[a-zA-Z][\w\-]+$/
...do awesome stuff with a parameter that is alpha-based...
elsif params[:value].match /\d+/
...do awesome stuff with a parameter that is numeric-based...
else
run Proc.new { |env| [404, {}, "" ] }
end
@adamlwatson
adamlwatson / gist:1371170
Created November 16, 2011 20:02
Creating a connection pool in Goliath
config['mongo'] = EM::Synchrony::ConnectionPool.new(size:10) do
begin
conn = EM::Mongo::Connection.new('localhost', 27017, 1, {:reconnect_in => 1})
conn.db('mongo_db_name')
rescue Exception=>err
abort "An error occurred while creating the mongodb connection pool: #{err}"
end
end
@adamlwatson
adamlwatson / gist:1371577
Created November 16, 2011 21:56
Mongoid connection pooling in Goliath
require 'em-synchrony/em-mongo'
require 'mongoid'
mongoid_conn = Mongo::Connection.new 'localhost', 27017, :pool_size => 10
Mongoid.configure do |config|
begin
config.master = mongoid_conn.db('dbname')
rescue Exception=>err
abort "An error occurred while creating the mongoid connection pool: #{err}"
end
@adamlwatson
adamlwatson / gist:1384414
Created November 22, 2011 00:03
em-http callbacks not being given time to fire when inside an async callback that is repeatedly triggered?
require 'amqp'
require 'em-http-request'
EM.run do
connection = AMQP.connect(:host => '127.0.0.1')
channel = AMQP::Channel.new(connection)
queue = channel.queue("em.load.test", :auto_delete => true)
exchange = channel.default_exchange
def handle_message(header, payload, *args)
@adamlwatson
adamlwatson / for_james.rb
Last active May 7, 2016 14:16
Calculate percentage of change over time to linearly ramp a value to 0.
def linearRampToZeroOverTime( value, seconds )
value = value.to_f
update_ticks = 60
step_delta = value / seconds.to_f
tick_delta = step_delta / update_ticks.to_f
( 1..seconds ).each do | sec |
( 1..update_ticks ).each do | tick |
last_value = value
value = value - tick_delta
percentage_of_change = value / last_value
@adamlwatson
adamlwatson / test.py
Last active January 27, 2017 22:22
Problem with parsing of request with complex types
import pretend
from zeep import Client
from zeep.transports import Transport
client = Client('http://api.affiliatewindow.com/v6/AffiliateService?wsdl')
response = pretend.stub(
status_code=200,
headers=[],
content="""
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.affiliatewindow.com/"><SOAP-ENV:Body><ns1:getTransactionListResponse><ns1:getTransactionListReturn><ns1:Transaction><ns1:iId>00000000</ns1:iId><ns1:sStatus>confirmed</ns1:sStatus><ns1:sType>normal</ns1:sType><ns1:sIp>x.x.x.x</ns1:sIp><ns1:bPaid>false</ns1:bPaid><ns1:iPaymentId>0</ns1:iPaymentId><ns1:iMerchantId>1111</ns1:iMerchantId><ns1:mSaleAmount><ns1:dAmount>0.00</ns1:dAmount><ns1:sCurrency>GBP</ns1:sCurrency></ns1:mSaleAmount><ns1:mCommissionAmount><ns1:dAmount>0.00</ns1:dAmount><ns1:sCurrency>GBP</ns1:sCurrency></ns1:mCommissionAmount><ns1:dClickDate>2017-01-01T00:00:00+00:00</ns1:dClickDate><ns1:dTransactionDate>2017-01-01T00:00:00+00:00</ns1
# this scrubs emoji sequences from a string - i think it covers all of them
def strip_emoji ( str )
str = str.force_encoding('utf-8').encode
clean_text = ""
# emoticons 1F601 - 1F64F
regex = /[\u{1f600}-\u{1f64f}]/
clean_text = str.gsub regex, ''