Skip to content

Instantly share code, notes, and snippets.

@thewoolleyman
Created March 13, 2010 00:41
Show Gist options
  • Save thewoolleyman/330977 to your computer and use it in GitHub Desktop.
Save thewoolleyman/330977 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'test/unit'
require 'mongo'
def rating(query)
collection_size = query.collection.count
return 100 if collection_size <= 1
explanation = query.explain
return 0 if collection_size > 1 and explanation['startKey'].empty?
convert(explanation['nscanned']/collection_size) + 50*(explanation['n']/explanation['nscanned'])
end
def convert(ratio)
((ratio * -100) + 100) / 2
end
class QueryTest < Test::Unit::TestCase
include Mongo
def setup
@col = create_collection
end
def test_indexed_read_on_a
query_to_test = @col.find({'a' => 3})
assert_operator 50, :<=, rating(query_to_test), "Low query rating (<50)"
assert_operator 100, :<=, rating(query_to_test), "Less than outstanding query rating (<100)"
end
def create_collection(name = 'sample', db_name = 'aDB')
db = Connection.new.db(db_name)
db.drop_collection(name) if db.collection_names.include? name
col = db[name]
100.times { |i| col.insert({:a => i, :b => 10 + i}) }
col.create_index('a')
col
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment