3月 19

服务器上固态硬盘使用情况追踪测试

2012年8月17日 在服务器上装了一块固态硬盘,当时做了个测试
http://www.simonzhang.net/?p=1298

测试磁盘写能力
time dd if=/dev/zero of=/cache/test.dbf bs=8k count=300000
a

测试磁盘读能力
time dd if=/cache/test.dbf of=/dev/null bs=8k
b

  因为服务器还在工作,并且有20G的左右的缓存数据在使用,能保持这个效率还是不错。

查看一下磁盘健康情况,使用smartmontools工具
# yum install -y smartmontools

常用命令:
健康状况
smartctl -H /dev/hdb
列出错误日志。如果日志量过大,应该尽快更换
smartctl -l error /dev/hdb
所有信息
smartctl -a /dev/hdb

信息里面有温度,加电时间,等详细信息。

附录加载部分
/dev/system/root/ext4 discard,errors=remount-ro,noatime 0 1

12月 05

go语言跑在Raspberry pi 上

  之前go语言在HTC的android手机上运行正常,所以在raspberry pi上也应该没有问题。所以测试一下,如果可以又多了一种开发方法。下载go1.0.3.

安装需要的包
$ sudo apt-get install mercurial bison gcc libc6-dev ed gawk make git

配置环境变量,将以下配置放在profile文件的最底部,为了方便直接用go源码编译,直接放在家目录里。
$ sudo vi /etc/profile
export GOARM=5
export GOROOT=/home/pi/go
export GOARCH=arm
export GOOS=linux
export PATH=$PATH;/home/pi/go/bin/

开始编译
$ cd go/src/
$ time ./all.bash

经过漫长的等待终于编译完了,耗时如下。
real 49m48.591s
user 44m33.760s
sys 2m7.540s

直接用之前的代码测试http://www.simonzhang.net/?p=1346。运行成功。

5月 27

从mysql向redis中加载数据测试

  有测试显示reids如果使用持久化测试后效率会下降,所以不使用持久化。现在来测试一下从mysql中捞取数据加载到redis中的速度。
  服务器使用8核2.6 cpu,内存8G,sas硬盘,Centos5.6 64位操作系统。python 2.6 redis2.4.13.
  使用测试代码如下,从mysql的photo表中捞取两列数据加载到redis中,这两列在表中都有索引,数据量28万。

#!/bin/env python
# -------------------------------------------------
# Filename:    
# Revision:    
# Date:        2012-05-27
# Author:      simonzhang
# Email:       simon-zzm@163.com
# -------------------------------------------------
import MySQLdb
import redis


def redis_run(sql_data):
    try:
        r = redis.Redis(host='192.168.1.100', password = '123456', port=6379, db=0)
    except redis.RedisError, e:
        print "Error %s" % e
    for i in sql_data:
        r.set(str(i[0]),i[1])
        

def mysql_run(sql):
    try:
        db = MySQLdb.connect(host='192.168.1.100', user='test', passwd ='123456', db='photo')
        cursor = db.cursor()   
    except MySQLdb.Error, e:
        print "Error %d:%s" % (e.args[0],e.args[1])
        exit(1)
    try:
        result_set = ''
        cursor.execute('%s' % sql)
        result_set=cursor.fetchall()
        cursor.close()
        db.close()
        return  result_set
    except MySQLdb.Error, e:
        print "Error %d:%s" % (e.args[0], e.args[1])
        cursor.close()
        db.close()

def main():
    _loop = 0
    _limit_start = 0
    _limit_span = 10000
    _count_result = 5
    while _count_result > 0:
        result_data = ''
        sql = "select id as pid, userid as uid from photo LIMIT %s,%s" % (_limit_start + _limit_span * _loop, _limit_span)
        result_data = mysql_run(sql)
        _count_result = len(result_data)
        redis_run(result_data)
        _loop += 1


if __name__ == '__main__':
    main()

进行测试,分别为每次捞取50万,10万,5万,1万,结果如下:

50万
real 0m26.239s
user 0m16.816s
sys 0m5.745s

10万
real 0m24.019s
user 0m15.670s
sys 0m4.932s

5万
real 0m26.061s
user 0m15.789s
sys 0m4.674s

1万
real 0m28.705s
user 0m15.778s
sys 0m4.913s

结论:每次捞取10万效率会比较理想,对于操作系统的压力不大,所以硬件方面不用考虑。
这里两列保存的都是id,加入用户id和照片id长度都是9位,一组数据是18位。一亿组数据也就需要2G内存。
通过计算28万需要24秒,如果有1亿的数据,全部倒入要2个半小时。所以内存存储不是问题。不知道用固态硬盘是否能快,我没有就不知道了。所以要做三件事,一做好集群,将数据及时同步到其他机房,自己写个程序同步定时同步,如果用主从,主机重启了为空,这个就很麻烦了,二使用redis的数据持久化,肯定比从mysql中直接捞快,三天天烧香希望不要宕机。

4月 07

android的monkey测试简单记录

  需要对android的一个软软件做个疲劳测试,使用android SDK自带的工具最为方便。所就开始做monkey测试,调用和回播的脚本是从网上搜集而来,自己做了小修改。原始脚本的网页url忘记记录,所以不能提供参考网连接。下面开始对我所做的简单测试做个记录。
  首先是在系统上安装java运行环境,这个比较简单。下载android SDK包,直接解压后就可以使用非常方便。
  进入android-sdk-windows/tools目录,在其下面写两个脚本。
录脚本的启动脚本名monkey_recorder.py脚本内容如下:

#!/usr/bin/env monkeyrunner
# Copyright 2010, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder

device = mr.waitForConnection()
recorder.start(device)

回放脚本名为monkey_playback.py脚本内容如下:

#!/usr/bin/env monkeyrunner
# Copyright 2010, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
from com.android.monkeyrunner import MonkeyRunner

# The format of the file we are parsing is very carfeully constructed.
# Each line corresponds to a single command.  The line is split into 2
# parts with a | character.  Text to the left of the pipe denotes
# which command to run.  The text to the right of the pipe is a python
# dictionary (it can be evaled into existence) that specifies the
# arguments for the command.  In most cases, this directly maps to the
# keyword argument dictionary that could be passed to the underlying
# command. 

# Lookup table to map command strings to functions that implement that
# command.
CMD_MAP = {
    'TOUCH': lambda dev, arg: dev.touch(**arg),
    'DRAG': lambda dev, arg: dev.drag(**arg),
    'PRESS': lambda dev, arg: dev.press(**arg),
    'TYPE': lambda dev, arg: dev.type(**arg),
    'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg)
    }

# Process a single file for the specified device.
def process_file(fp, device):
    for line in fp:
        (cmd, rest) = line.split('|')
        try:
            # Parse the pydict
            rest = eval(rest)
        except:
            print 'unable to parse options'
            continue

        if cmd not in CMD_MAP:
            print 'unknown command: ' + cmd
            continue
        CMD_MAP[cmd](device, rest)
        
def go(file,num):
    n = num
    device = MonkeyRunner.waitForConnection()
    for i in range(int(n)):
        print 'now %s'%i
        fp = open(file, 'r')
        process_file(fp, device)
        fp.close();

def main():
    file = sys.argv[1]
    try :
        number = sys.argv[2]
    except :
        number = 1
    go(file,number)
 
if __name__ == '__main__':
    main()

  到此环境和脚本就算部署完了。下面开始记录脚本使用。
  首先将手机和电脑用usb线连好,使用“cmd”调出命令行,然后进入“android-sdk-windows/tools”目录。输入命令:
monkeyrunner.bat monkey_recorder.py
如果没有报错,并且出现以下界面,就可以开始录制脚本了。

  “wait”为每步操作间的等待时间。“Press a Button“发送,MENU,HOME,or SEARCH 按钮。“Type Something”为输入文字。“Fling”为模拟滑动操作。“Export Actions”为保存刚才录制的脚本。最后一个为刷新。   
  保存的脚本可以直接用文本编辑器手动编辑。假如我们录制的脚本为text.txt。下一步就是回放脚本,使用命令为:
monkeyrunner.bat monkey_playback.py test.txt 100
  最后的数字是将这个脚本循环100次。
  我只需要到此即可了。如果需要抓图片\照相\判断等高级功能,将来需要时再学习。

4月 01

C、go、python、java、php和node.js 简单循环累加运算速度测试

【张子萌 www.simonzhang.net 2012-4-1】
【张子萌 www.simonzhang.net 2012-8-8 修改增加go语言】

之前简单做了一下node.js和python的“hello ***”的页面测试,也做了循环的测试,本次主要是增加了java的语言,go语言。主要是想看一下主流四种脚本的速度java、python、php、c语言。均使用for循环进行简单的累加测试。个人技能有限所以只做了简单测试做参考。

实验环境使用linux 64位服务器,操作系统为contos 5.4,php版本5.1.6,python版本为2.6.6,node.js版本为0.4.12,java版本为1.6.0_03,gcc 版本 4.1.2 2008070,go语言为1.0.2。
一、脚本编写
php脚本
# cat test.php

python脚本
# cat test.py

#!/bin/env python
#-*- coding:utf-8 -*-
def main():
    j = 0;
    for i in xrange(10000000):
        j=j+i
    print j

if __name__=="__main__":
    main()

node.js脚本
# cat test.js

var j=0;
for (i = 0; i < 10000000; i++ ) {
   j=j+i
}
    console.log(j);

Java代码:
# cat Test.java

public class Test {
public static void main(String[] args) {
          long n = 0;
          for (int i = 0; i < 10000; i++) {
               n=n+i;
          }
          System.out.println(n);
     }
}

C语言,使用gcc编译,编译两种结果,一种是直接编译的,一种是优化编译的。

#include 
#include 
main()
{
    long i,j=0;
    for (i=0 ; i<10000000 ; i++)
       j=j+i;
    printf("%ld\n",j);
}

go语言代码

package main

import "fmt"

func main() {
    var sum,i int64
    sum=0
    for i=0;i<10000000;i++{
        sum+=i
    }
    fmt.Printf("%d",sum)
}

二、运行结果
使用time命令对程序运行时间进行统计

以下是循环一千万次的累加测试结果。

参数 C语言直接编译 C语言优化编译 go Node.js Python PHP Java
Real 0.024s 0.001s 0.011s 0.420s 1.055s 1.429s 0.087
User 0.023s 0.000s 0.011s 0.401s 1.046s 1.423s 0.067
sys 0.001s 0.001s 0.000s 0.019s 0.009s 0.007s 0.015

以下是循环一万次的累加测试结果

参数 C语言直接编译 C语言优化编译 go Node.js Python PHP Java
Real 0.001s 0.001s 0.004s 0.090s 0.027s 0.014s 0.087
User 0.000s 0.001s 0.003s 0.080s 0.022s 0.007s 0.041
sys 0.001s 0.000s 0.002s 0.010s 0.006s 0.007s 0.017

三、结论
从简单的测试来看,c语言不是一般的快,大数据计算情况下node.js速度最快,java次之,python和php比慢。但是如果是少量计算时php效果还是很不错。但是实际应用中,还需要调用各种函数和各方面的资源,并不能以一个空框架下的for来判断。go的速度与C相当,但是编译速度还是比C要慢一点,以上的代码C编译完只有6K多,go编译完有1.3M。每种语言都会有自己擅长的一方面,速度快与慢,还与编写的技巧性有关。学好每一步,认认真真踏实的做就好了。

注:C、go和java被编译后会对代码进行优化。各自不同的编译器优化的侧重也不一样,所以这个测试有些失实。比如即使循环数再大java时间也不会有改变,估计是在编译时已经将for里的值计算完毕,运行时直接取结果。有兴趣的可以学习编译原理。