This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: "Описание урока" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer