Skip to content

Instantly share code, notes, and snippets.

@kigster
Created November 8, 2023 20:01
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 kigster/3f01d364e417c1502c49e8e58f433595 to your computer and use it in GitHub Desktop.
Save kigster/3f01d364e417c1502c49e8e58f433595 to your computer and use it in GitHub Desktop.
Memcached Multi-Get vs Single Get
# Gemfile
source "https://rubygems.org"
# Activate the gem you are reporting the issue against.
gem 'rails', '>= 7.0.4.3'
gem "dalli"
gem "rspec"
gem "rspec-its"
gem "rspec-rails"
gem "awesome_print"
# memcached.rb
#!/usr/bin/env ruby
# typed: false
# frozen_string_literal: true
require 'rubygems'
require 'rspec'
require 'rspec/its'
require 'action_controller/railtie'
require 'action_view/railtie'
ARRAY_SIZE = 10000
class TestApp < Rails::Application
class << self
attr_accessor :key_array, :array_size
end
self.array_size = ARRAY_SIZE
self.key_array = []
array_size.times.each { |index| self.key_array << index }
config.root = __dir__
config.session_store :cookie_store, key: "cookie_store_key"
config.hosts.clear
#secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
routes.draw do
get "/" => "test#index"
end
end
class ApplicationController < ActionController::Base
include Rails.application.routes.url_helpers
end
class TestController < ApplicationController
def index
cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, ['127.0.0.1:11212'])
write_time = Benchmark.measure do
TestApp.key_array.each do |key|
cache.fetch(key) { true }
end
end
missing_keys = []
fetch_multi_time = Benchmark.measure do
cache.fetch_multi(TestApp.key_array) do |key|
missing_keys << key
end
end
fetch_by_one_time = Benchmark.measure do
TestApp.key_array.each do |key|
cache.read(key) or warn("key #{key} is missing")
end
end
render json: {
array_size: TestApp.key_array.size,
write_time: write_time.real,
fetch_multi_time: fetch_multi_time.real,
fetch_by_one_time: fetch_by_one_time.real,
missing_keys: missing_keys.size
}
end
end
require "rspec/autorun"
require "rspec/rails"
require 'awesome_print'
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
end
RSpec.describe TestController, type: :request do
describe "#index" do
it 'should return json' do
get '/'
expect(response).to have_http_status(:ok)
ap JSON.parse(response.body)
end
end
end
# Running:
$ bundle install
# Bundle complete! 6 Gemfile dependencies, 69 gems now installed.
# Use `bundle info [gemname]` to see where a bundled gem is installed.
$ bundle exec memcached.rb 2>/dev/null
I, [2023-11-08T12:01:03.145175 #66687] INFO -- : Started GET "/" for 127.0.0.1 at 2023-11-08 12:01:03 -0800
D, [2023-11-08T12:01:03.171663 #66687] DEBUG -- : Dalli::Server#connect 127.0.0.1:11212
{
"array_size" => 10000,
"write_time" => 0.4958810000680387,
"fetch_multi_time" => 0.006276000058278441,
"fetch_by_one_time" => 0.4439320000819862,
"missing_keys" => 0
}
Finished in 0.9975 seconds (files took 0.58344 seconds to load)
1 example, 0 failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment