Commit cb9aac31 authored by Ohad Dahan's avatar Ohad Dahan

Added init method and benchmarks

parent f918e16d
require "nativepluck/version"
module Nativepluck
@nativepluck_init = false
class << self
attr_accessor :nativepluck_type_map_for_results, :nativepluck_type_map_for_queries
attr_accessor :original_type_map_for_results, :original_type_map_for_queries
attr_accessor :nativepluck_init
end
def self.included(klass)
klass.extend(ClassMethods)
init_nativepluck
end
def self.init_nativepluck
return if @nativepluck_init
@nativepluck_type_map_for_results = PG::BasicTypeMapForResults.new ActiveRecord::Base.connection.raw_connection
@nativepluck_type_map_for_queries = PG::BasicTypeMapForQueries.new ActiveRecord::Base.connection.raw_connection
@original_type_map_for_results = ActiveRecord::Base.connection.raw_connection.type_map_for_results
@original_type_map_for_queries = ActiveRecord::Base.connection.raw_connection.type_map_for_queries
@nativepluck_init = true
end
def self.set_pg_native_casters
init_nativepluck
ActiveRecord::Base.connection.raw_connection.type_map_for_results = Nativepluck.nativepluck_type_map_for_results
ActiveRecord::Base.connection.raw_connection.type_map_for_queries = Nativepluck.nativepluck_type_map_for_queries
end
def self.return_original_casters
init_nativepluck
ActiveRecord::Base.connection.raw_connection.type_map_for_results = Nativepluck.original_type_map_for_results
ActiveRecord::Base.connection.raw_connection.type_map_for_queries = Nativepluck.original_type_map_for_queries
end
......
require 'memory_profiler'
require 'benchmark/ips'
Dir.glob("#{Rails.root}/test/benchmarks/*benchmarks.rb").each do |benchmark_file|
require_relative benchmark_file
end ; nil
require 'memory_profiler'
require 'benchmark/ips'
require 'pg'
module BenchMarks
module Memory
def self.benchmark(write_to_file: false, row_limit: nil)
limit = row_limit || Genericmodel.count
results = nil
reports = {pluck: {}, nativepluck: {}, nativepluck_raw_to_sql: {}, nativepluck_raw_string: {}}
Genericmodel.first.attribute_names.each do |attribute_name|
##################################################################################################
Genericmodel.pluck(attribute_name)
reports[:pluck][attribute_name] = MemoryProfiler.report do
results = Genericmodel.limit(limit).pluck(attribute_name)
end
##################################################################################################
Genericmodel.nativepluck(attribute_name)
reports[:nativepluck][attribute_name] = MemoryProfiler.report do
results = Genericmodel.nativepluck(attribute_name, limit: limit)
end
##################################################################################################
Nativepluck.nativepluck_raw(Genericmodel.limit(limit).select(attribute_name))
reports[:nativepluck_raw_to_sql][attribute_name] = MemoryProfiler.report do
results = Nativepluck.nativepluck_raw(Genericmodel.limit(limit).select(attribute_name))
end
##################################################################################################
sql = Genericmodel.limit(limit).select(attribute_name).to_sql
Nativepluck.nativepluck_raw(sql)
reports[:nativepluck_raw_string][attribute_name] = MemoryProfiler.report do
results = Nativepluck.nativepluck_raw(sql)
end
##################################################################################################
end
##################################################################################################
return unless write_to_file
text_report = {}
reports.each_pair do |type, results_per_attribute|
results_per_attribute.each_pair do |attribute_name, result|
text_report[attribute_name] ||= []
text_report[attribute_name] << [type, attribute_name, result.total_allocated, result.total_retained]
end
end ; nil
File.open('memory_report.csv','w') do |fh|
fh.puts("Number of rows : #{limit}")
fh.puts('Method, Attribute Name, Total Allocated, Total Retained')
text_report.each_value do |results|
results.each {|i| fh.puts(i.join(',')) }
end
end
##################################################################################################
end
##################################################################################################
end
end
require 'memory_profiler'
require 'benchmark/ips'
require 'pg'
module BenchMarks
module RunTime
def self.benchmark
loggers = [ActiveRecord::Base.logger, ActiveModelSerializers.logger]
ActiveRecord::Base.logger ,ActiveModelSerializers.logger = nil , Logger.new(nil)
limit = Genericmodel.count
Genericmodel.attribute_names.each do |attribute_name|
sql_raw = Genericmodel.limit(limit).select(attribute_name).to_sql
Benchmark.ips do |x|
x.config(time: 5, warmup: 1)
x.report("pluck | #{limit} | #{attribute_name}") { Genericmodel.limit(limit).pluck(attribute_name) }
x.report("nativepluck | #{limit} | #{attribute_name}") { Genericmodel.nativepluck(attribute_name, limit: limit) }
x.report("nativepluck_raw :to_sql | #{limit} | #{attribute_name}") { Nativepluck.nativepluck_raw(Genericmodel.limit(limit).select(attribute_name)) }
x.report("nativepluck_raw SQL_RAW | #{limit} | #{attribute_name}") { Nativepluck.nativepluck_raw(sql_raw) }
x.compare!
end
end
ActiveRecord::Base.logger , ActiveModelSerializers.logger = loggers ; nil
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment