Created
January 28, 2013 00:10
-
-
Save isergey/4651567 to your computer and use it in GitHub Desktop.
mongo
This file contains 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
#encoding: utf-8 | |
from time import time | |
import datetime | |
import random | |
from pymongo import MongoClient | |
from bson.code import Code | |
from couchdb.client import Server, Database | |
def main(): | |
connection = MongoClient() | |
db = connection.stat | |
collection = db.pages | |
s = time() | |
# ids = range(1, 50) | |
# month = range(1, 13) | |
# day = range(1,26) | |
# hour = range(0, 24) | |
# minute = range(0, 60) | |
# second = range(0, 60) | |
# print random.choice(ids) | |
# | |
# groups = ['users', 'admin', 'manager', 'anon'] | |
# print random.choice(groups) | |
# | |
# now = datetime.datetime.utcnow() | |
# print (now.replace(day=1)) | |
# | |
# insert = [] | |
# for i in xrange(0, 1000000): | |
# date = datetime.datetime.utcnow() | |
# date = date.replace(month=random.choice(month), day=random.choice(day), hour=random.choice(hour), minute=random.choice(minute), second=random.choice(second)) | |
# | |
# item = { | |
# 'groups': [ | |
# random.choice(groups), | |
# random.choice(groups), | |
# ], | |
# 'id': random.choice(ids), | |
# 'date': date | |
# } | |
# insert.append(item) | |
# if i % 1000 == 0: | |
# print i | |
# collection.insert(insert) | |
# insert = [] | |
map = Code( | |
""" | |
function () { | |
// if (this.id == '1'){ | |
this.groups.forEach(function(x){ | |
emit(x, 1); | |
}); | |
//} | |
} | |
""" | |
) | |
reduce = Code( | |
""" | |
function (key, values) { | |
var total = 0; | |
for (var i = 0; i < values.length; i++) { | |
total += values[i]; | |
} | |
return total; | |
} | |
""" | |
) | |
# result = collection.map_reduce(map, reduce, "myresults") | |
result = collection.aggregate([ | |
{'$match': {'groups': "anon"}}, | |
{'$group': 1} | |
] | |
) | |
print result | |
#collection.create_index([("id", 1)]) | |
d = datetime.datetime(2013, 11, 12) | |
#print collection.find({'date': {"$gte": d}, 'groups': 'admin'}).count() | |
# print collection.find({'groups': 'admin'}).count() | |
print time() - s | |
def couch(): | |
server = Server() | |
db = server['python-tests'] | |
map_function = """ | |
function (doc) { | |
emit(doc.id, 1); | |
} | |
""" | |
reduce_fun = """ | |
function (keys, values, reduce) { | |
var total = 0; | |
for (var i = 0; i < values.length; i++) { | |
total += values[i]; | |
} | |
return total; | |
} | |
""" | |
# results = db.query(map_function, reduce_fun) | |
# print len(results) | |
s = time() | |
# ids = range(1, 50) | |
# month = range(1, 13) | |
# day = range(1,26) | |
# hour = range(0, 24) | |
# minute = range(0, 60) | |
# second = range(0, 60) | |
# print random.choice(ids) | |
# | |
# groups = ['users', 'admin', 'manager', 'anon'] | |
# print random.choice(groups) | |
# | |
# now = datetime.datetime.utcnow() | |
# print (now.replace(day=1)) | |
# insert = [] | |
# for i in xrange(0, 1000000): | |
# date = datetime.datetime.utcnow() | |
# date = date.replace(month=random.choice(month), day=random.choice(day), hour=random.choice(hour), minute=random.choice(minute), second=random.choice(second)) | |
# | |
# item = { | |
# 'groups': [ | |
# random.choice(groups), | |
# random.choice(groups), | |
# ], | |
# 'id': random.choice(ids), | |
# #'date': date | |
# } | |
# insert.append(item) | |
# if i % 1000 == 0: | |
# print i | |
# db.update(insert) | |
# insert = [] | |
print time() - s | |
main() | |
#couch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment