Skip to content

Instantly share code, notes, and snippets.

@IvanTorresEdge
Created May 10, 2012 05:45
Show Gist options
  • Select an option

  • Save IvanTorresEdge/2651275 to your computer and use it in GitHub Desktop.

Select an option

Save IvanTorresEdge/2651275 to your computer and use it in GitHub Desktop.
CacheFly's Purge Performance Test
#!/usr/bin/env ruby
require 'rubygems'
require 'aws-sdk'
require 'base64'
require 'benchmark'
AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''
CACHEFLY_TOKEN = ''
CACHEFLY_INVALIDATION_URI = 'api.cachefly.com/1.0/purge.purge.file'
CACHEFLY_DOMAIN = 'https://you-cf-subdomain.cachefly.net'
bucket_name = ''
source_files = ['./jquery-1.7.1.min.js', './jquery-1.7.2.min.js']
@filename = '/javascripts/vendor/jquery.min.js'
def request_invalidation
uri = URI.parse("http://#{CACHEFLY_TOKEN}@#{CACHEFLY_INVALIDATION_URI}")
res = Net::HTTP.post_form(uri, :files => Base64.encode64(@filename))
# puts res.body
end
def request_jquery(version = '1.7.1')
uri = URI.parse(CACHEFLY_DOMAIN + @filename)
req = Net::HTTP::Get.new(uri.path)
http = Net::HTTP::new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
res = nil
begin
res = http.request(req)
sleep 1
end until !res.nil? && res.body.match(/#{version}/)
end
AWS.config\
:access_key_id => AWS_ACCESS_KEY_ID,
:secret_access_key => AWS_SECRET_ACCESS_KEY
# Create the basic S3 object
s3 = AWS::S3.new
# Load up the 'bucket' we want to store things in
bucket = s3.buckets[bucket_name]
# If the bucket doesn't exist, create it
unless bucket.exists?
puts "Need to make bucket #{bucket_name}.."
s3.buckets.create(bucket_name)
end
# Grab a reference to an object in the bucket with the name we require
object = bucket.objects[@filename.gsub(/^\//, '')]
Benchmark.bm(15) do |x|
x.report 'jquery 1.7.1' do
object.write :file => source_files[0],
:acl => :public_read,
:content_type => 'application/javascript'
request_invalidation
request_jquery
end
x.report 'jquery 1.7.2' do
object.write :file => source_files[1],
:acl => :public_read,
:content_type => 'application/javascript'
request_invalidation
request_jquery '1.7.2'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment