5月 29

post数据到golang服务和python的pypy服务后数据入库库效率测试

  在测试go语言和pypy接收post参数后,数据入库的效率。服务器使用阿里云的基础服务器,512内存单核CentOS6.3 64位服务器。测试还有上传文件部分。此处只列出了部分代码。
  python使用tornado在pypy1.9环境,python代码没有列出。
  go语言代码如下。数据库配置部分写成可以全局调用的。

package main

import (
	"fmt"
        "database/sql"
        _ "github.com/go-sql-driver/mysql"
	"net/http"
)

func mydb()*sql.DB {
    //配置数据库连接地址
    db, e:= sql.Open("mysql", "test:1111111@tcp(192.168.1.130:3306)/test?charset=utf8")
    checkErr(e)
    fmt.Println(db)
    return db
}

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}

func IndexHandler(w http.ResponseWriter, r *http.Request) {
        //用于获取插入数据的表id
        var get_id string
	if r.Method == "POST" {
            r.ParseForm()
	    //获取获得的参数
	    get_time:=r.FormValue("time")
            get_mac:=r.FormValue("mac")
            get_md5:=r.FormValue("md5")
	    //插入数据
            stm, e := mydb().Prepare("INSERT INTO `video` set mac=?, md5=?, createDate=?")
            checkErr(e)
            result,e:=stm.Exec(get_mac, get_md5, get_time)
            checkErr(e)
	    //获取插入后数据的id
            id,e:= result.LastInsertId()
            checkErr(e)
	    //返回给客户
            get_id = fmt.Sprint(id)
            context := "Insert id:" + get_id
	    w.Write([]byte(context))
        }
}

func main() {
     http.HandleFunc("/", IndexHandler)
     http.ListenAndServe(":8899", nil)
}

  测试结果:单进程情况下,go语言的速度比pypy的稍慢,基本持平相差不到5%。但是go使用内存量占到45%,cpu使用8%左右。pypy使用内存20%,cpu使用在5%左右。如果多进程pypy可以启动4个,go只能启动两个。这样pypy比go的处理速去应该快一倍多。java代码也有测试,使用tomcat7,因为逻辑更复杂,所以不能参加对比,粗略估计go会比java快40%。
测试代码

5月 28

学习torndb

  用tornado3.0.1所以开始使用torndb,对torndb进行学习。
  链接到数据库查看torndb提供的方法如下:
close、database、execute、execute_lastrowid、execute_rowcount、
executemany、executemany_lastrowid、executemany_rowcount、get、
host、iter、max_idle_time、query、reconnect、socket

提供的方法

execute                            执行语句不需要返回值的操作。
execute_lastrowid           执行后获得表id,一般用于插入后获取返回值。
executemany                  可以执行批量插入。返回值为第一次请求的表id。
executemany_rowcount 批量执行。返回值为第一次请求的表id。
get                                   执行后获取一行数据,返回dict。
iter                                   执行查询后,返回迭代的字段和数据。
query                               执行后获取多行数据,返回是List。

close                               关闭
max_idle_time                最大连接时间
reconnect                       关闭后再连接

torndb可以自动commit不用手动提交,对几个主要的方法进行各测试。

建表
CREATE TABLE `ceshi` (
`id` int(1) NULL AUTO_INCREMENT ,
`num` int(1) NULL ,
PRIMARY KEY (`id`)
)
;

>>> import torndb
>>> db = torndb.Connection(“192.168.1.103″,”ceshi”,”ceshi”, “1111111”, 24*3600)
>>> get_id1 = db.execute_lastrowid(“insert ceshi(num) values(‘1’)”)
>>> print get_id1
1
>>> args1 = [(‘2’),(‘3’),(‘4’)]
>>> get1 = db.executemany(“insert ceshi(num) values(%s)”, args1)
>>> print get1
2
>>> args2 = [(‘5’),(‘6’),(‘6’)]
>>> get2 = db.executemany(“insert ceshi(num) values(%s)”, args2)
>>> print get2
5
>>> rows = db.iter(“select * from ceshi”)
>>> for i in rows:
… print i

{‘num’: 1L, ‘id’: 1L}
{‘num’: 2L, ‘id’: 2L}
{‘num’: 3L, ‘id’: 3L}
{‘num’: 4L, ‘id’: 4L}
{‘num’: 5L, ‘id’: 5L}
{‘num’: 6L, ‘id’: 6L}
{‘num’: 6L, ‘id’: 7L}