A MongoDB Stress Test Written in Python
A MongoDB Stress Test Written in Python
$ Pip install pymongo
#! /Usr/bin/env python
# Coding = UTF-8
# Author: Ca0Gu0
From pymongo import MongoClient
Import datetime, time
Class MongCli (object ):
Def _ init _ (self, host = "127.0.0.1", port = 27017, user = 'pytest', passwd = 'pytest123 ', database = "pytest "):
Self. host = host
Self. port = port
Self. user = user
Self. passwd = passwd
Self. database = database
Client = Consumer client (self. host, self. port)
Client. the_database.authenticate (self. user, self. passwd, source = self. database)
Self. db = client [self. database]
Self. posts = self. db. posts
Def t (self, args1 = None, args2 = None ):
C = datetime. datetime. now ()
Print "% s | % s... | % s | Row: % s | % s "% (self. host, args1, args2, self. posts. count (), c. strftime ("% Y-% m-% d % H: % M: % S "))
Return c
Def write (self, number = 100 ):
Start = self. t (args1 = "start", args2 = "write ")
For I in range (number ):
Post = {"author": "Mike" + str (I ),
"Text": "My first blog post! "+ Str (I ),
"Tags": ["mongodb", "python", "pymongo"],
"Date": datetime. datetime. utcnow ()}
Post_id = self. posts. insert_one (post). inserted_id
End = self. t (args1 = "end", args2 = "write ")
Print "Total write runtime: % ss" % str (end-start). seconds)
Def read (self ):
Start = self. t (args1 = "start", args2 = "read ")
Output = open ("output.txt", 'w ')
For post in self. posts. find ():
Try:
Output. write (str (post) + "\ n ")
Except t Exception, e:
Print e
Output. close ()
End = self. t (args1 = "end", args2 = "read ")
Print "Total read runtime: % ss" % str (end-start). seconds)
Print "---------- Split --------"
If _ name _ = "_ main __":
F = MongCli (host = "127.0.0.1", port = 27017, user = 'admin', passwd = 'admin', database = "admin ")
F. write (1, 20000)
F. read ()
MongoDB 3.0 official version released and downloaded
CentOS compilation and installation of MongoDB
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
MongoDB beginners must read (both concepts and practices)
MongoDB Installation Guide for Ubunu 14.04
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]
Nagios monitoring MongoDB sharded cluster service practice
Build MongoDB Service Based on CentOS 6.5 Operating System
MongoDB details: click here
MongoDB: click here
This article permanently updates the link address: