Skip to content

Instantly share code, notes, and snippets.

@shaiguitar
Last active May 8, 2017 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaiguitar/c4ca88f516f95e15bc81cd0ac2a8176f to your computer and use it in GitHub Desktop.
Save shaiguitar/c4ca88f516f95e15bc81cd0ac2a8176f to your computer and use it in GitHub Desktop.
Fog caching example
require 'fog'
require 'fog/aws'
ec2 = Fog::Compute.new :provider => 'AWS', :region => 'us-east-1'
Fog::Cache.namespace_prefix = "benchmark-test"
begin
servers = Fog::Cache.load(Fog::Compute::AWS::Server, ec2)
puts "found cache."
# 1a) return if we found them in the cache. in this case we haven't.
puts servers.size
rescue Fog::Cache::CacheNotFound => e
puts "could not find cache. loading from api."
# 2) not found in cache. get resources from the connection.
servers = ec2.servers.all
# 3) dump resources for next time
servers.each { |r| r.cache.dump }
end
# Fog::Cache.expire_cache!(Fog::Compute::AWS::Server, ec2)
require 'fog'
require 'fog/aws'
ec2 = Fog::Compute.new :provider => 'AWS', :region => 'us-east-1'
servers = ec2.servers
puts servers.size

Run w/out caching:

shai@lappy ~   % time ruby fog-example.rb
1058
ruby fog-example.rb  3.66s user 0.21s system 20% cpu 18.895 total

With caching

shai@lappy ~   % time ruby fog-example-cache.rb
could not find cache. loading from api.
ruby fog-example-cache.rb  5.58s user 0.35s system 29% cpu 20.423 total

shai@lappy ~   % time ruby fog-example-cache.rb
found cache.
1058
ruby fog-example-cache.rb  3.11s user 0.17s system 80% cpu 4.087 total

Result - 500% increase.

An increase of about ~500% in speed, from 20 secs to 4 secs on an account with ~1k instances being fetched from the api.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment