前言:

Go测试环境:

windows10虚拟机vmware安装的centos6.9

内存2g,处理器数量2个,每个处理器核心数量2个

Go版本1.7.3

beego版本1.9.0

MongoDB版本3.4.7


Python测试环境:

windows10虚拟机vmware安装的centos7.0

内存2g,处理器数量2个,每个处理器核心数量2个

Python版本2.7.5

MongoDB版本3.4.9

测试代码:

go语言代码如下:

package main

import (
    "fmt"
    "labix.org/v2/mgo"
    "labix.org/v2/mgo/bson"
    "strconv"
    "time"
)

//产品信息
type Product struct {
    Id             string
    Name           string
    Pro_type       string
    Specifications string
    Total          string
    Used           string
    Info
    Resource
}

//附加资源信息
type Resource struct {
    Id    string
    Total string
    Used  string
}

//设备配置信息
type Info struct {
    Cpu  string
    Mem  string
    Disk string
}

type ProAll struct {
    Products []Product
}

func main() {

    //连接MongoDB数据库
    session, err := mgo.Dial("")
    if err != nil {
        panic(err)
    }
    defer session.Close()

    // Optional. Switch the session to a monotonic behavior.
    session.SetMode(mgo.Monotonic, true)
    //选择对应的数据库和集合
    c := session.DB("test").C("resource")

    var i int
    var s string
    var count int = 100000

    //插入10W条数据
    tInsert := time.Now() // get current time
    for i = 0; i < count; i++ {

        s = strconv.Itoa(i)
        err = c.Insert(&Product{s, "IPS", "asset", "L3", "350", "150", Info{"4", "8", "80"}, Resource{"100", "100", "20"}})
        if err != nil {
            panic(err)
        }

        //        fmt.Println("Product id:", i)
    }
    elapsed := time.Since(tInsert)
    fmt.Println("Insert elapsed: ", elapsed)

    //查询10W条数据
    tFind := time.Now() // get current time
    result := Product{}
    var productsAll ProAll //存放结果
    iter := c.Find(nil).Iter()
    for iter.Next(&result) {
        productsAll.Products = append(productsAll.Products, result)
    }
    elapsed = time.Since(tFind)
    fmt.Println("Find elapsed: ", elapsed)

    //修改10W条数据
    //更新数据
    tUpdate := time.Now() // get current time
    for i = 0; i < count; i++ {
        s = strconv.Itoa(i)
        err = c.Update(bson.M{"id": s}, bson.M{"$set": bson.M{"name": "waf", "ProType": "test", "specifications": "L5", "total": "100", "used": "10"}})
        if err != nil {
            panic(err)
        }
    }
    elapsed = time.Since(tUpdate)
    fmt.Println("Update elapsed: ", elapsed)

    //删除10W条数据(逐条)
    tDelete := time.Now() // get current time
    for i = 0; i < count; i++ {
        s = strconv.Itoa(i)
        //删除数据
        _, err = c.RemoveAll(bson.M{"id": s})
        if err != nil {
            panic(err)
        }
    }
    elapsed = time.Since(tDelete)
    fmt.Println("Delete elapsed: ", elapsed)
    fmt.Println("Delete complete ")
    /*
        //删除10W条数据(全部)
        tDelete := time.Now() // get current time
        //删除数据
        _, err = c.RemoveAll(bson.M{})
        if err != nil {
            panic(err)
        }
        elapsed = time.Since(tDelete)
        fmt.Println("Delete elapsed: ", elapsed)
        fmt.Println("Delete complete ")
    */
}


Python代码如下:

!/usr/bin/env python
# -*- coding:utf-8 -*-

import time

from pymongo import MongoClient

conn = MongoClient('127.0.0.1', 27017)
db = conn.test  #连接mydb数据库,没有则自动创建
my_set = db.resource

count = 100000
dict_pros = {'Id':'0001', 'Name':'VFW', 'Pro_type':'L2','Specifications':'talent', 'Total':'200', 'Used':'100',\

             'Resource_id':'VPN','Resource_total':'100','Resource_used':'50','Cpu':'2','Mem':'16','Disk':'80'}


#插入数据
print 'Insert data'
start = time.time()
for num in range(0,count):
    #print num
    dict_pros['Id'] = str(num)
    dict2 = dict_pros.copy()
    #print dict2.items()
    my_set.insert(dict2)
end = time.time()
print end-start

#查询全部
print 'Find data'
start = time.time()
for i in my_set.find():
    #print(i)
    b = i
end = time.time()
print end-start

#更新数据
print 'Update data'
start = time.time()
for num in range(0,count):
    #print num
    s = str(num)
    my_set.update({"Id":s},{'$set':{"Pro_type":"L6", "Name":"WAF"}})
end = time.time()
print end-start

#删除数据
print 'Delete data'
start = time.time()
for num in range(0,count):
    #print num
    s = str(num)
    my_set.remove({"Id": s})
end = time.time()
print end-start
注:以下测试数据

测试1 操作10条数据

操作方式\操作语言操作选项Go语言操作MongodbPython语言操作Mongodb



插入数据(逐条插入)

测试1(用时)2.140517ms5.378ms
测试2(用时)1.542948ms6.022ms
测试3(用时)1.806694ms7.603ms
测试平均值

1.8301ms

6.3343ms



查看数据(逐条查看)


测试1(用时)0.340315ms0.747ms
测试2(用时)0.28613ms0.791ms
测试3(用时)0.259212ms0.813ms
测试平均值

0.3132ms

0.769ms



更新数据(逐条更新)

测试1(用时)2.307188ms3.584ms
测试2(用时)2.038681ms3.62ms
测试3(用时)1.746155ms3.641ms
测试平均值

2.1729ms

3.602ms



删除数据(逐条删除)

测试1(用时)1.764252ms3.231ms
测试2(用时)1.470281ms3.257ms
测试3(用时)1.603262ms3.818ms
测试平均值

1.6172665ms

3.244ms


测试2 操作100条数据

操作方式\操作语言操作选项Go语言操作MongodbPython语言操作Mongodb



插入数据(逐条插入)

测试1(用时)12.253904ms53.663ms
测试2(用时)13.564496ms73.445ms
测试3(用时)13.863377ms31.856ms
测试平均值

13.2273ms

52.988ms



查看数据(逐条查看)


测试1(用时)1.013526ms3.751ms
测试2(用时)0.948686ms4.870ms
测试3(用时)0.999393ms4.812ms
测试平均值

0.9872ms

4.4777ms



更新数据(逐条更新)

测试1(用时)17.917997ms27.548ms
测试2(用时)18.722336ms37.705ms
测试3(用时)18.205441ms27.004ms
测试平均值

18.2819ms

30.7523ms



删除数据(逐条删除)

测试1(用时)15.574377ms32.952ms
测试2(用时)15.145433ms28.667ms
测试3(用时)16.658532ms26.138ms
测试平均值

15.7928ms

29.2523ms

测试3 操作1k条数据

操作方式\操作语言操作选项Go语言操作MongodbPython语言操作Mongodb



插入数据(逐条插入)

测试1(用时)128.656804ms367.175ms
测试2(用时)123.476824ms291.976ms
测试3(用时)122.358405ms247.361ms
测试平均值

124.8307ms

302.1707ms



查看数据(逐条查看)


测试1(用时)9.95505ms106.139ms
测试2(用时)9.641698ms156.25ms
测试3(用时)15.468753ms34.816ms
测试平均值

11.6885ms

99.0683ms



更新数据(逐条更新)

测试1(用时)356.757078ms500.113ms
测试2(用时)333.284348ms504.497ms
测试3(用时)339.227792ms505.964ms
测试平均值

343.0897ms

503.5247ms



删除数据(逐条删除)

测试1(用时)332.65338ms570.044ms
测试2(用时)349.066367ms500.387ms
测试3(用时)318.377296ms507.826ms
测试平均值

333.3657ms

526.0857ms

测试4 操作1w条数据

操作方式\操作语言操作选项Go语言操作MongodbPython语言操作Mongodb



插入数据(逐条插入)

测试1(用时)1.208164373s2.540460s
测试2(用时)1.278558761s2.750484s
测试3(用时)1.21364248s2.261721s
测试平均值

1.2335s

2.5176s



查看数据(逐条查看)


测试1(用时)0.095756s0.438170s
测试2(用时)0.074885s0.067101s
测试3(用时)0.095283s0.078621s
测试平均值

0.0886s

0.1946s



更新数据(逐条更新)

测试1(用时)19.123441215s23.6131s
测试2(用时)19.2433542s22.1318s
测试3(用时)19.643619901s21.3948s
测试平均值

19.3368s

22.3799s



删除数据(逐条删除)

测试1(用时)17.413472439s22.6515s
测试2(用时)17.924175571s21.4528s
测试3(用时)19.169451398s21.5346s
测试平均值

18.1690s

21.8796s

测试5操作10w条数据

操作方式\操作语言操作选项Go语言操作MongodbPython语言操作Mongodb

插入数据(逐条插入)

测试1(用时)12.573846404s24.0779s

查看数据(逐条查看)

测试1(用时)0.924193s9.6382s

更新数据(逐条更新)

测试1(用时) 1870.7085s

删除数据(逐条删除)

测试1(用时) 1891.2897s

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐