5月 30

golang获取unix time和随机数

比较简单,不做解释。

package main

import(
    "fmt"
    "time"
    "math/rand"
)

func main(){
    //获取unix time
    var a string 
    a = fmt.Sprint(time.Now().UnixNano())
    //获取随机数
    var b string
    ra := rand.New(rand.NewSource(time.Now().UnixNano()))
    b= fmt.Sprint(ra.Intn(10))
    //打印结果
    fmt.Println(a)
    fmt.Println(b)
}

golang获取unix time和随机数

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}

5月 17

iptables 允许苹果推送服务

  业务上要给苹果设备做推送,所以服务器要连接苹果的推送服务器。因为服务器启动了iptables防火墙所以请求被拦截了,需要开通。

苹果服务器使用2195端口,我的防火墙对于出的流量是没有限制的。所以iptables添加
#apple push message
iptables -A INPUT -p tcp –sport 2195 -j ACCEPT

  用telnet测试连接成功,然后程序测试成功。
# telnet gateway.sandbox.push.apple.com 2195

  有的是说2196也是,最好也加上。

5月 11

CentOS 上安装 pypy,做简单测试

以前没有关注过pypy,但是看到rasperry pi上pypy效果不错,我在服务器上也测试一下。
我的操作系统是CentOS是6.4,64位。直径使用源码安装失败,在网上搜索了一下问题较多并且很麻烦。
还是直接用rpm安装比较好,通过搜索,直接下载对应操作系统的pypy,当前CentOS6的pypy只有1.9版本,并不是最新的2.0,先测试一下。链接如下:
http://pkgs.org/search/?keyword=pypy

下载完rpm包开始安装,大家注意顺序。
# rpm -ivh pypy-libs-1.9-1.el6.x86_64.rpm
# rpm -ivh pypy-1.9-1.el6.x86_64.rpm
# rpm -ivh pypy-devel-1.9-1.el6.x86_64.rpm

还是用之前文中的代码进行测试(http://www.simonzhang.net/?p=1844)。
以后测试可能还经常用到这段代码,所以整理了一下格式,代码非原创。

#-*- coding:utf-8 -*-
# -------------------------------
# Filename:    test.py
# Revision:    1.0
# Date:        2013-05-08 
# Author:      simonzhang 
# Web:         www.simonzhang.net 
# Email:       simon-zzm@163.com 
# -------------------------------


def check(num):
     a = list(str(num))
     b = a[::-1]
     if a == b:
         return True
     return False


def main():
    all = xrange(1,10**7)
    for i in all:
        if check(i):
            if check(i**2):
                print i,i**2


if __name__ == '__main__':
    main()

python 测试结果
real 0m40.657s
user 0m40.622s
sys 0m0.019s

pypy 测试结果
real 0m9.833s
user 0m9.803s
sys 0m0.027s

测试效果,pypy比python快差不多4倍。使用pip直接安装tornado,测试了最简单的导入,查看版本是正常的。
代码:test