Skip to content

Instantly share code, notes, and snippets.

View alekseyl's full-sized avatar
👨‍💻

Aleksey Leshchuk alekseyl

👨‍💻
View GitHub Profile
@alekseyl
alekseyl / compare_cpu.rb
Last active May 11, 2025 12:37
CPU profiling method comparing eager_load VS preload aproaches
def compare_cpu_general( scope, base_collection_size, nesting)
ids = scope.ids
ActiveRecord::Base.logger.silence do
Benchmark.bmbm do |benchmark|
benchmark.report("eager_load") do
50.times do
# sample(base_collection_size + 1) and limit(base_collection_size)
# added to imitate real limit for eager_load, so it would be obligated to use 2 queries instead of one
# for more read here: https://medium.com/p/5ce11870de62
scope.where(id: ids.sample(base_collection_size + 1)).limit(base_collection_size).eager_load(nesting).load
@alekseyl
alekseyl / compare_ram.rb
Created May 4, 2025 14:11
Memory profiling method comparing eager_load VS preload aproaches
require 'memory_profiler'
require 'terminal-table'
def compare_ram(scope, base_collection_size, nesting)
ids = scope.ids
ids_sample = ids.sample(base_collection_size) # it should be the same id for both tests, since we comparing memory consumption
# just to keep from memory profiler objects oblivion, and get retain numbers correctly
el, pr = []
preload = MemoryProfiler.report do
@alekseyl
alekseyl / nested_select_profiling.rb
Last active January 12, 2025 19:58
nested_select_profiling
# This is a simple gist for performance and RAM usage testing for a nested_select gem
#
# Objects relations: course -> topics -> lessons ( all relations are has_many )
#
# -------------------------------------------------------------------------
# Schemas:
#
# create_table "lessons", force: :cascade do |t|
# t.string "title", null: false, comment: "Тема урока"
# t.string "description", null: false, comment: "Описание урока"
@alekseyl
alekseyl / update_numbers.csv
Created December 20, 2021 12:50
Delegated uniqueness UPDATE
operation base HOT base non-HOT non-HOT+uuid index non-HOT+text index
UPDATE 11ms 22ms 45ms 64ms
@alekseyl
alekseyl / insert_numbers.csv
Last active December 20, 2021 13:01
delegated uniqueness INSERT
operation base configuration +uuid index +text index uuid trigger text trigger
INSERT 3.5ms 4.7ms 5.5ms 7.9ms 8.4ms
class CommentsController < ApplicationController
# I assumed the usual naming for models and foreign columns
def users_comments
# I don't like where( post_id: Post.all ) but if table comments
# contains deleted posts comments or even NULLs than we need to keep it this way
@user_comments = Comment.where( post_id: Post.all )
.where(author_id: Author.where( username: params[:username]) )
end
end
@alekseyl
alekseyl / me_redis_configure.rb
Last active May 18, 2018 08:25
MeRedis confiugure examples
Redis.include( MeRedis )
# zip key crumbs user, card, card_preview, zips integer crumbs to base62,
# for keys starting with card_preview prefix compress values with Zlib
# for keys starting with user or card compress values with ActiveRecordJSONCompressor
Redis.configure(
hash_max_ziplist_entries: 256,
zip_crumbs: %i[user card card_preview], # -> { user: :u, card: :c, card_preview: :c0 }
integers_to_base62: true,
compress_namespaces: {
:card_preview => MeRedis::ZipValues::ZlibCompressor,
@alekseyl
alekseyl / json_compressor.rb
Last active May 17, 2018 15:34
JSON compressor example for me-redis gem
module RSmazCompressor
def self.compress(value); RSmaz.compress(value) end
def self.decompress(value); RSmaz.decompress(value) end
end
module ZLibJSONCompressor
def self.compress(value); Zlib.deflate(value.to_json) end
def self.decompress(value); JSON.load( Zlib.inflate( value ) )end
end
@alekseyl
alekseyl / calc_performance
Last active May 18, 2018 09:00
compare redis performance for ruby hmethods VS methods
def calc_me_redis_perfomance_hash_zipping( zip_max = 128, sample_size = 10000 )
Redis.include(MeRedis)
redis = Redis.new
Redis.configure do |c|
c.zip_crumbs = :user
c.integers_to_base62 = true
c.hash_max_ziplist_entries = zip_max
end
@alekseyl
alekseyl / comparision
Last active May 28, 2018 09:04
comparision of redis memory optimization, for https://medium.com/@leshchuk/zipem-all-61076c7da4c
# Columns:
# redis - clear memory usage of 100K values in Mb,
# gz - gz compressed value, smaz = smaz compressed value,
# key zip = zipping key crumbs to cingle char,
# me_ - memory_efficient_ hash optimization
#integers, html pieces, plain english text pieces
+----------+--------+--------+--------+---------+--------+----------+------------+
| type | redis | gz | smaz | key zip | me_* | kz+me+gz | kz+me+smaz |
+----------+--------+--------+--------+---------+--------+----------+------------+