5月 09

raspberry pi 测试pypy 切割图片效率

为了方便安装pypy的第三方库,首先安装pip。
$ curl -O http://python-distribute.org/distribute_setup.py
$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ sudo pypy distribute_setup.py
$ sudo pypy get-pip.py

首先找了一个3M(3072×2304)的图片,缩小为300×300的大小
脚本:

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


from PIL import Image


def main():
    get_data = Image.open('test.jpg')
    tmp = get_data.resize((300, 300),)
    tmp.save('test1.jpg', 'JPEG', quality=75)


if __name__ == '__main__':
    main()

在pypy上装PIL
$ sudo /usr/lib/pypy-upstream/bin/pip install PIL

运行脚本报错如下:

File “/usr/lib/pypy-upstream/site-packages/PIL/Image.py”, line 1290, in resize
self.load()
File “/usr/lib/pypy-upstream/site-packages/PIL/ImageFile.py”, line 189, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File “/usr/lib/pypy-upstream/site-packages/PIL/Image.py”, line 385, in _getdecoder
raise IOError(“decoder %s not available” % decoder_name)
IOError: decoder jpeg not available

不能调用系统库,之前在处理过这种问题http://www.simonzhang.net/?p=435
但是pypy比较复杂,兼容有问题。直接删除PIL,使用pillow。Pillow基础就是PIL只是兼容性强,更利于推广。
$ sudo /usr/lib/pypy-upstream/bin/pip uninstall PIL

$ sudo /usr/lib/pypy-upstream/bin/pip install pillow

注意必须使用
from PIL import Image

否则会报错
File “/usr/lib/pypy-upstream/site-packages/PIL/Image.py”, line 2020, in open
raise IOError(“cannot identify image file”)
IOError: cannot identify image file

在安装完pillow后没有产生PIL.pth文件,直接手动写一个。
$ sudo vim /usr/lib/pypy-upstream/site-packages/PIL.pth
内容是:PIL。

开始测试

time python cut_pic.py

real 0m2.841s
user 0m2.630s
sys 0m0.200s

time pypy cut_pic.py

real 0m5.144s
user 0m4.870s
sys 0m0.230s

图片产生大小如下
-rw-r–r– 1 pi pi 3588203 5月 8 16:02 test.jpg
-rw-r–r– 1 pi pi 21907 5月 8 16:10 test_pypy.jpg
-rw-r–r– 1 pi pi 21907 5月 8 16:09 test_python.jpg

直接使用python的效果更佳,不清楚原因。之后有版本升级了再做测试。

9月 12

golang 批量检查页面

  在学习go语言,所以当前的需求使用go来试验一下。需要:循环检查某些url并返值,并获取所用时间,如果返回内容长度超过100个字节,只显示前100个字节,不足100个字节显示全部。我将url写在url.txt文件中,方便以后增加。
  在centos 5.4下go1.0.2编译通过。虽然编译通过,但是个人感觉还是不如python方便,并且变异出来的东西都快3M了,让我想起了当年的delphi。如果这种监测实验脚本还是首选python。但是用python做有个问题,如果使用“while 1”循环使用urllib2获取,时间一长就失败过多。具体原因没有查,直接用crontab或脚本调用解决。

package main

import ("fmt"
        "os"
        "net/http"
        "io/ioutil"
        "time"
        )

func main() {
        var str string
        userFile := "url.txt"
        fin,err := os.Open(userFile)
        defer fin.Close()
        if err != nil {
                fmt.Println(userFile,err)
                return
        }
        buf := make([]byte, 1)
        //
        for{
            n,_:= fin.Read(buf)
            if 0==n {break}
            if string(buf)=="\n"{
               //开始通过url获得页面
               starttime:=time.Now().Unix()
               res, err := http.Get(str)
               endtime:=time.Now().Unix()
               usetime:=endtime-starttime
               if err != nil {
                   fmt.Println(err)
               } 
               context, err := ioutil.ReadAll(res.Body)
               if err != nil {
                   fmt.Println(err)
               }
               res.Body.Close()
               // 获得完毕打印
               if len(context) >100{
                     fmt.Println(string(context)[:100])
               } else {
                     fmt.Println(string(context))
               }
               fmt.Println("get url:", str, " use time :", usetime)
               str=""
            } else {
                str+=string(buf)
              }
        }
}
3月 16

squid 配置 和常用命令

【收集整理:张子萌 2009-11-2】

设置为缓荐服务器
cache_mem 30 M #内存使用大小。一般来说如果系统有内存,设置该值为(n/)3M。现在是3G 所以这里1G
cache_swap_low 90
cache_swap_high 95
max_open_disk_fds 0 #允许最大打开文件数量,0 无限制
minimum_object_size 0 KB #磁盘cache中最小的object的大小(低于这个值则不缓存)
maximum_object_size 20000 KB #磁盘cache中最大的object的大小(超过这个值则不缓存)
maximum_object_size_in_memory 4096 KB #装入内存缓存的文件大小,默认值是8K,超过8K的文件都不装入内存,可以在这里设成4M

cache_swap_low 90 #最小允许使用swap 90%
cache_swap_high 95 #最多允许使用swap 95%
cache_dir ufs /tmp1 10000 16 256 #磁盘缓存的类型和目录,大小,一二级目录的设置,这里磁盘缓存大小是10G
#ufs是一种文件存储方式 因为os一般都是从内存获取数据 那么内存的东西必须最后写的硬盘上
#sync 同步的时候用的ufs 那么squid也是一样
#ufs一般是同时写入内存和硬盘
#注意:size是按照M为单位的也就这个目录中最大存储容量的上限

acl QUERY urlpath_regex -i cgi-bin .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe #-i为不区分大小写
cache deny QUERY #设置不想缓存的目录或者文件类型

cache_mgr webmaster@test.com #管理员邮箱

###相关日志记录,可以设为none成不记录日志####
cache_store_log /var/log/squid/store.log
cache_log /var/log/squid/cache.log
access_log /var/log/squid/access.log combined

http_port 80 # squid监听的端口,客户访问网站的端口
httpd_accel_host ip_address # WEB服务器的IP地址
httpd_accel_single_host on # 设置转发为缓冲的请求到一台单独的机器
httpd_accel_port 81 # WEB服务器的端口
httpd_accel_uses_host_header off # 完成单台WEB服务器的反向代理功能
httpd_accel_with_proxy off # 停用代理
cache_peer www.XXXXX.com parent 80 0 no-query originserver name=www #定义不同的父节点,将节点设为no-query以及originserver说明这些节点是实际服务器
cache_peer_domain www.XXXXX.com #设定不同域名转发到不同的cache_peer上,如果没有这项.不同域名的域名可能被分发到同一台服务器上.
hierarchy_stoplist cgi-bin ? #用来强制某些特定的对象不被缓存,主要是处于安全的目的。

refresh_pattern . 0 20% 4320 override-expire override-lastmod reload-into-ims ignore-reload #更新cache规则
client_lifetime 120 minute #最大客户连接时间 120分钟
cache_mgr sky@test.com #指定当缓冲出现问题时向缓冲管理者发送告警信息的地址信息
request_entities off #禁止非http的标准请求,防止攻击
acl buggy_server url_regex ^http://…. http:// #只允许http的请求
# 设定可以访问的域名别名
acl HostA dstdomain www.XXXX.com
acl HostB dstdomain bbs.XXXX.com
# 加速器采用http协议在8000端口监听
acl acceleratedProtocol protocol HTTP
acl acceleratedPort port 8000
acl all src 0.0.0.0/0.0.0.0 #允许所有IP访问
acl manager proto http #manager url协议为http
acl localhost src 127.0.0.1/255.255.255.255 #允午本机IP
acl to_localhost dst 127.0.0.1 #允午目的地址为本机IP
acl Safe_ports port 80 # 允许安全更新的端口为80
acl CONNECT method CONNECT #请求方法以CONNECT
http_access allow all #允许所有人使用该代理.因为这里是代理加速web服务器
http_reply_access allow all #允许所有客户端使用该代理
acl OverConnLimit maxconn 16 #限制每个IP最大允许16个连接,防止攻击
http_access deny OverConnLimit
icp_access deny all #禁止从邻居服务器缓冲内发送和接收ICP请求.
miss_access allow all #允许直接更新请求
ident_lookup_access deny all #禁止lookup检查DNS

half_closed_clients off #半关闭状态的TCP连接,设为off,则一旦从客户端返回“no more data to read”的信息,squid就立即关闭该连接
refresh_pattern .htm 0 25% 8 #设置htm文件过期时间为10分钟

一些关于squid调试的命令:
1,初始化squid.conf 里配置的 cache 目录
#squid/sbin/squid -z
如果有错误提示,请检查 cache目录的权限。

2. squid.conf 排错,即验证 squid.conf 的语法和配置。
#squid/sbin/squid -k parse
如果squid.conf 有语法或配置错误,这里会返回提示你,如果没有返回,恭喜,可以尝试启动squid。

3.在前台启动squid,并输出启动过程。
#squid/sbin/squid -N -d1
如果有到 ready to server reques,启动成功。
然后 ctrl + c,停止squid,并以后台运行的方式启动。

4.启动squid在后台运行。
#squid/sbin/squid -s
可以通过ps -ef|grep squid 来查看系统进程。

5.停止 squid
#squid/sbin/squid -k shutdown

6.重引导修改过的 squid.conf
#squid/sbin/squid -k reconfigure
修改squid.conf配置后,先检查配置文件是否正确,然后再执行此指令,即可让squid按新quid.conf 来运行。

7.把squid添加到系统启动项 编辑
/etc/rc.d/rc.local 添加如下行:
/usr/local/squid/sbin/squid -s

8.查看你的日志文档。
#more /usr/local/squid/var/logs/access.log | grep TCP_MEM_HIT
该指令可以看到在squid运行过程中,有那些文件被squid缓存到内存中,并返回给访问用户。
#more /usr/local/squid/var/logs/access.log | grep TCP_HIT
该指令可以看到在squid运行过程中,有那些文件被squid缓存到cache目录中