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%。
测试代码

7月 08

python mongodb 变量做集合(collection)名

【2011-7-8 张子萌】
当前需求:1)将某个目录下的html文件插入到mongo数据库中。
2)cellection名为html页的上级目录名。
3)document为文件名、html内容

测试目录位置/home/test

#!/usr/bin/python
#-*- coding:utf-8 -*-
import re
import os
import pymongo

find_file=re.compile(r”.html$”)
find_path=r”/home/test”
find_walk=os.walk(find_path)
conn = pymongo.Connection(“localhost”,27017)
db = conn.mytest
for path,dirs,files in find_walk:
for file in files:
if find_file.search(file):
collection_name=path.split(“/”)[2]
get_html=open(path+”/”+file,’r’)
exec(‘db.’+collection_name+’.save({“file_name”:file,”context”:get_html.read()})’)
get_html.close()

遇到一个问题,好像collection如只是数字(int)插入有问题,所以如果用数字名做collection名可以在前面加个字母。

3月 25

delphi简单代码收集

【收集人:张子萌 】

1. Form1中调用Form2窗口

Form2.ShowModal; //显示form2窗口

Form2.close; //关闭窗口

2. 获取title,可查看UTF-8编码

procedure TForm2.Button1Click(Sender: TObject); //获取title
var
webboby:string;
r:TRegExpr;
s1:string;
charseti:integer;
gethttp:Tidhttp;
begin
gethttp:=Tidhttp.Create(self);
webboby:=gethttp.Get(Edit1.Text);
charseti:=pos(‘charset=’,webboby);
if (copy(webboby,charseti+8,3)=’utf’) or (copy(webboby,charseti+8,3)=’UTF’) then
webboby:=UTF8Decode(webboby);
s1:=”;
r:= TRegExpr.Create;
try // ensure memory clean-up
r.Expression := ‘[^a]*‘;
if r.Exec(webboby) then
REPEAT
s1 :=r.Match [0];
Edit2.Text:=s1;
UNTIL not
r.ExecNext;
finally
r.Free;
end;
end;