Test MongoDB replica Set data synchronization latency with Python

Source: Internet
Author: User
Tags mongoclient

This article mainly introduces how Python connects MongoDB replica set and read/write separation configuration, MongoDB replica set data synchronization delay test.


First, python Connection MongoDB Replica set

1. connecting replica sets

Direct use in the new version of the drive mongoclient Connection

As follows:

From Pymongo Import mongoclientconn = Mongoclient ([' 192.168.3.11:27017 ', ' 192.168.3.12:27017 ', ' 192.168.3.13:27017 '])

2. Read and write separation configuration

From Pymongo Import readpreferencedb = conn.get_database (' hnrtest ', Read_preference=readpreference.secondary_ PREFERRED)

Replica set readpreference have a 5 Options:

PRIMARY : Default options, from primary node read data

PRIMARY _preferred : Precedence from primary node read if there is no primary nodes, then available from the cluster secondary node reads

SECONDARY : from secondary node read data

secondary_preferred : Priority from Secondary node reads if there are no available Secondary node, from the Primary node Read

NEAREST : Reads data from the nodes available in the cluster

second, the test MongoDB Delay Script

1. principle

insert a batch of data into the cluster, and then move from Secondary The node reads until the total number of data is read equal to the total number of insertions.

2. Span style= "Font-family:simsun;" lang= "ZH-CN" xml:lang= "ZH-CN" > script contents

#!/usr/bin/python# -*- coding: utf-8 -*-import pymongofrom pymongo import  MongoClientfrom pymongo import ReadPreferenceimport timeimport argparse#  Defines the execution-time-length adorner function Def warps (*args):     def deco (func):         def _deco (*args, **kwargs):             #  Record start time             start  = time.clock ()             #  callback primitive function             func (*args, **kwargs)              #  Record End Time              end = time.clock ()              #  Calculation Execution Duration             delat =  end - start            #  conversion into MS output             print ("Delay:%sms"  %  (int ( delat*1000))         return _deco    return  deco#  connecting replica set conn = mongoclient ([' 192.168.3.11:27017 ',  ' 192.168.3.12:27017 ',  ' 192.168.3.13:27017 ']) #  read/write Separation db = conn.get_database (' hnrtest ',  read_preference= readpreference.secondary_preferred) #  Create Insert Data function Def data_insert (num):    try:         for i in range (1, num):             db.student.insert ({"Name":  "Student" +str (i),  "age":   (i % 100),  "City": " Fuzhou "})     except Exception as e:         print ("Insert data:",  e) #  Create a query data function that references the adorner function @warps () def data_select (num):     try:        count = db.student.find ( ). Count ()         while count != num - 1:             count = db.student.find (). Count ()     except Exception as e:         print ("Select data:",  e) #  Create delete data function Def data_delete ():     try:         db.student.remove ({})     except  Exception as e:        print ("Delete data:",  e) #   Create calculation delay time function Def data_Delay (num):     data_insert (num)     data_select (num) if __name__  ==  ' __main__ ':    #  defines the amount of data that the script needs to pass in, with a default value of 1000, passing in parameters by-n      parser = argparse. Argumentparser (description= ' insert data to mongodb number ')      Parser.add_argument ('-n ',  action= ' store ',  dest= ' num ', type=int, required=false,  default=1000)     given_args = parser.parse_args ()     num  = given_args.num    data_delete ()     data_delay (num)

This article is from the "Linux Road" blog, make sure to keep this source http://hnr520.blog.51cto.com/4484939/1874506

Test MongoDB replica Set data synchronization latency with Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.