10月 29

splinter自动化测试工具学习

splinter自动化测试工具学习。模拟浏览器操作。

官方标文档:http://splinter.cobrateam.info/docs/

测试环境如下:
windows 8 64位操作系统安装python2.7.8

window按顺序安装下面的包,也可以直接用pip安装。
https://bootstrap.pypa.io/ez_setup.py
https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz
https://pypi.python.org/packages/source/s/selenium/selenium-2.44.0.tar.gz
https://pypi.python.org/packages/source/s/splinter/splinter-0.6.0.tar.gz
下面库是后台使用也可以在后面安装
https://pypi.python.org/packages/source/z/zope.testbrowser/zope.testbrowser-4.0.4.zip

学习部分,简化实例
实例化
from splinter import Browser
browser = Browser()

基础部分:
访问部分 browser.visit(‘www.xxx.com’)
填表部分 browser.fill(‘hello’)
单击按钮 browser.find_by_name(‘button’).click()
获取返回值(用if判断) browser.is_text_present(‘OK’)
退出部分 browser.quit()
选择不同浏览器 browser = Browser(‘firefox’)
重新导入 browser.reload()
后退 browser.back()
前进 browser.forward()
获取title browser.title
获取页面 browser.html
获取当前url browser.url
模拟浏览器头 browser = Browser(user_agent=”Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)”)

常用部分:
查找相关元素,可以使用火狐的F12(大家应该懂)。
browser.find_by_css(‘h1’)
browser.find_by_xpath(‘//h1’)
browser.find_by_tag(‘h1’)
browser.find_by_name(‘name’)
browser.find_by_id(‘firstheader’)
browser.find_by_value(‘query’)

获取该元素的开始 first_found = browser.find_by_name(‘name’).first
获取该元素的结尾 last_found = browser.find_by_name(‘name’).last
获取该元素的索引 second_found = browser.find_by_name(‘name’)[1]

查找相关连接
links_found = browser.find_link_by_text(‘Link for Example.com’)
links_found = browser.find_link_by_partial_text(‘for Example’)
links_found = browser.find_link_by_href(‘http://example.com’)
links_found = browser.find_link_by_partial_href(‘example’)
获取某个元素的值 browser.find_by_css(‘h1’).first.value

查找div
divs = browser.find_by_tag(“div”)
within_elements = divs.first.find_by_name(“name”)

鼠标移动到某资源 browser.find_by_tag(‘h1’).mouse_over()
鼠标移开某资源 browser.find_by_tag(‘h1’).mouse_out()
双击某元素 browser.find_by_tag(‘h1’).double_click()
鼠标右键某资源 browser.find_by_tag(‘h1’).right_click()
点击相关连接
browser.click_link_by_href(‘http://www.the_site.com/my_link’)
browser.click_link_by_partial_href(‘my_link’)
browser.click_link_by_text(‘my link’)
browser.click_link_by_partial_text(‘part of link text’)

选择 browser.choose(‘some-radio’, ‘radio-value’)
选择项目 browser.check(‘some-check’)
取消选项 browser.uncheck(‘some-check’)
选择下拉菜单 browser.select(‘uf’, ‘rj’)

针对ajax部分
对某个焦点 browser.is_text_present(‘splinter’, wait_time=10)
browser.is_text_not_present(‘text not present’, wait_time=10)
browser.is_text_not_present(‘text not present’, wait_time=10)
直接执行脚本

browser.execute_script('document.getElementById("body").innerHTML="

Hello world!

"')

cookie部分
添加cookie browser.cookies.add({‘whatever’: ‘and ever’})
获取所有cookie browser.cookies.all()
删除cookie值 browser.cookies.delete(‘mwahahahaha’)
删除所有cookie browser.cookies.delete() #deletes all cookies

浏览器状态 browser.status_code.code
获取异常部分 HttpResponseError, e:

后台执行部分
http://splinter.cobrateam.info/docs/drivers/zope.testbrowser.html

实例代码,模拟51job刷新部分(比较复杂,基本模拟了主流的点击操作)。

#!/bin/python
#-*- coding:utf-8 -*-
# Filename:    grab-centreon.py
# Revision:    
# Date:        2014-10-29
# Author:      simonzhang
# web:         www.simonzhang.net
# Email:       simon-zzm@163.com
### END INIT INFO
from splinter import Browser
from time import sleep

url = 'www.51job.com'
name = 'kkkkkkk'
passwd = '9999999'
mybr = Browser()
 
# 按照id发送账号密码
mybr.visit("http://%s" % url)
sleep(2)
mybr.find_by_id('username').fill(name)    
mybr.find_by_id('userpwd').fill(passwd)
# 按照tag顺序点击
mybr.find_by_tag('input').click()
sleep(15)
mybr.find_by_id('window_fans_close').click()
sleep(3)
# 按照css查找按钮,点击开刷新页面
aa = mybr.find_by_css('a.refresh')
aa.click()
sleep(3)
# 直接执行刷新脚本,此部分需要根据自己编号进行修改
mybr.execute_script("Save_RefreshResume('000000','0','http://my.51job.com');")
# 直接找到刷新关闭页面
mybr.execute_script("javascript:Refresh_success.close();Refresh_success_LastUpdate(mystr[1]);")
# 按照连接找到位置进行点击
mybr.click_link_by_href('http://my.51job.com/my/My_SignOut.php')
sleep(2)
# 关闭浏览器
mybr.quit()
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

6月 25

如果我做双色球报表系统性能会如何

  双色球有人中了5.7亿,各种怀疑的声音也此起彼伏。有人说停售到开奖直播中间有一段时间是在算出现概率。我对是否在算并不感兴趣,我倒是想知道,如果在算要用什么样子的服务器

,会有多大压力。实际值2012072期销售349310588元,共174655294注。我用一台很老的dell 2850 来试验,双核3.2主频的单cpu,4G内存,ide接口硬盘。 Centos 5.4 64位系统,mysql5。

  写个脚本产生随机数来模拟彩票投注。根据投注号产生MD5码,取MD5的前两位作为分表依据。
  需要速度,所以开始用C来作,因学艺不精,C写的东西速度很慢,并且也没有找到原因,我将c的代码放在最后,如有朋友能帮忙解决烦请告知。最后还是用python来做。通过初步试验,

产生100万数据并入库需要3分多钟,如果产生1.8亿需要12个多小时。最后决定只产生一张表的数据,一张表数据100万。取MD5前两位,如果每张表都是100万,总共能产生2.56亿注,这个值

可以满足测试要求。

  产生表的SQL如下:

CREATE TABLE `dt_00` ( \
             `Id` int(11) NOT NULL AUTO_INCREMENT, \
             `CreateUnixTime` varchar(13) NOT NULL, \
             `RedBlue` varchar(21) DEFAULT NULL, \
             `HashValues` varchar(32) DEFAULT NULL, \
             `SaleId` int(11) DEFAULT NULL, \
             PRIMARY KEY (`Id`), \
             KEY `i_RedBlue` (`RedBlue`), \
             KEY `i_HashValues` (`HashValues`) \
             ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  产生投注的脚本如下,因为开始考虑到要放到256张表里,所以就没有做成批量入库,而是做成生成SQL语句,5000条提交一次。:

#!/bin/env python
# ----------------------------------------
# Filename:    rand.py
# Revision:    1.0
# Date:        2012-06-24
# Web:        http://www.simonzhang.net
# Author:      simonzhang
# Email:       simon-zzm@163.com
# ----------------------------------------
import random
import time
import MySQLdb
import hashlib


def mysql_run(sql):
    try:
        db = MySQLdb.connect(host = '192.168.1.41', user = 'double', passwd = 'ball', db = 'doubleball')
        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 create_sql():
    z = 0
    _tmp_di = []
    while (z < 6):
        red = random.randint(1, 33)
        count_rand_di = 0
        if len(_tmp_di) > 0:
            for i in _tmp_di:
                if red == i:
                    count_rand_di += 1
        if count_rand_di == 0:
            _tmp_di.append(red)
            z += 1
    _tmp_di.sort()
    blue = random.randint(1, 16)
    _result_di = ''
    for j in xrange(6):
        if j == 1:
            _result_di = "%s" % _tmp_di[j]
        else:
            _result_di += " %s" % _tmp_di[j]
    _get_md5 = hashlib.md5("%s+%s" % (_result_di, blue)).hexdigest()
    if _get_md5[0:2] == "00":
       _create_sql = "INSERT INTO  dt_00(CreateUnixTime,RedBlue,HashValues) VALUES (\'%s\',\'%s+%s\',\'%s\');" % (int(time.time()), _result_di, blue, _get_md5)
    else:
       _create_sql = ""
    return _create_sql


def write_time():
    _open_file = open("history.txt", "ab")
    get_time = time.localtime()
    _open_file.write("%s-%s-%s %s:%s:%s\n" % (get_time[0], get_time[1], get_time[2], get_time[3], get_time[4], get_time[5]))
    _open_file.close()


def main():
    _get_sql = ''
    for i in xrange(1000000):
        _loop_status = 0
        while _loop_status == 0:
            _tmp_sql = create_sql()
            if len(_tmp_sql) >2:
                _loop_status = 1
        _get_sql += _tmp_sql
        if i % 5000  == 0:
            print i
            mysql_run("%scommit;" % _get_sql)
            _get_sql = ''


if __name__ == "__main__":
    write_time()
    main()
    write_time()

  运行完毕使用sql分组统计,查看分组和排序结果SQL如下,运行一次用时6.693秒。

SELECT
	count(*) AS c,
	RedBlue,
	HashValues
FROM
	dt_00 d
GROUP BY
	d.HashValues
ORDER BY
	c DESC

  结论:因为要保证速度和稳定,所以购买系统应该是各省有各省的一套系统,然后准实时向中心汇数据,所以汇总需要的时间基本可以忽略。统计是按照分表的方式进行计算,然后在合

并结果,从计算效率来看,如果使用Dell R420高配的服务器,即使只有一台,系统也可以在3-5分钟产生出所有的分析报表(这个是估算,因为不知道需要多少统计表,也不知道需要统计的

逻辑)。如果是用oracle数据库应该会更安全快速。

  我写失败的C代码:

#include 
#include 
#include 

/*快速排序*/
void quickSort(int a[],int left,int right)
{
  int i,j,temp;
  i=left;
  j=right;
  temp=a[left];
  if (left>right)
    return;
  while(i!=j)
  {
    while(a[j]>=temp && j>i)
      j--;
    if(j>i)
      a[i++]=a[j];
    while (a[i]<=temp && j>i)
      i++;
    if(j>i)
      a[j--]=a[i];
  }
  a[i]=temp;
  quickSort(a,left,i-1);
  quickSort(a,i+1,right);
}

void main()
{
   int i,z;
   int red,blue;
   int count_rand_di;
   int di[6];
   char result_di[30]="";
   z=0;
   while(z<6)
   {
      srand(time(0)); 
      red=1+(int)(33.0*rand()/(RAND_MAX+1.0));
      count_rand_di=0;
      for(i=0;i

  编译命令:
gcc rand.c -o rand -Wall

参考资料:

http://baike.baidu.com/view/19016.htm

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里的值计算完毕,运行时直接取结果。有兴趣的可以学习编译原理。