图画类的书看起来比较方便,基本上一到两天就看完了。都是些基础问题,对于初学开发的比较适合,对于有3年以上经验的开发者我觉得就没有必要了。
Monthly Archives: 五月 2013
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) }
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%。
测试代码
学习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}
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也是,最好也加上。