Skip to content

@kayakjang /gist:1750926
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Memory usage for MongoMapper' where query
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.