public
Created

Memory usage for MongoMapper' where query

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
# load_test_books.rb
 
require 'rubygems'
require 'mongo_mapper'
 
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
MongoMapper.database = 'dpu-test'
 
class Book
include MongoMapper::Document
key :name, String
key :price, Integer
key :category, String
end
 
def load_test_books(nums = 100 * 100 * 10)
nums.times do |num|
Book.create(:price => rand(50) + 20, :name => "Ruby##{num+1}", :category => 'ruby')
end
end
 
# load 100,000 books
load_test_books
 
 
# query_where_all.rb
 
p Book.count
before_memory_usage = `ps -o rss= -p #{$$}`.to_i
Book.where(:category => 'ruby').all
after_memory_usage = `ps -o rss= -p #{$$}`.to_i
 
p 'memory usage before sum book price:'
p before_memory_usage
 
p 'memory usage after sum book price:'
p after_memory_usage
 
p after_memory_usage - before_memory_usage #in my computer printed 234.8m
 
 
# query_where.rb
 
p Book.count
before_memory_usage = `ps -o rss= -p #{$$}`.to_i
Book.where(:category => 'ruby')
after_memory_usage = `ps -o rss= -p #{$$}`.to_i
 
p 'memory usage before sum book price:'
p before_memory_usage
 
p 'memory usage after sum book price:'
p after_memory_usage
 
p after_memory_usage - before_memory_usage #in my computer only print 4kb
 
# paginate_where.rb
p Book.count
before_memory_usage = `ps -o rss= -p #{$$}`.to_i
total_price = 0
books = Book.where(:category => 'ruby')
pages = books.count / 100 + 1
1.upto pages do |page|
Book.where(:category => 'ruby').paginate(:page => page, :per_page => 100).each { |book| total_price += book.price }
end
 
after_memory_usage = `ps -o rss= -p #{$$}`.to_i
 
p 'memory usage before sum book price:'
p before_memory_usage
 
p 'memory usage after sum book price:'
p after_memory_usage
 
p after_memory_usage - before_memory_usage # print 28kb in my computer

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.