常见测试策略——脚本测试在业务测试中的使用

业务中常见的测试策略有功能测试、接口测试、性能测试等,而功能测试里常见的有场景测试、接口测试、脚本测试。今天我们来聊聊脚本测试。

测试任务

埋点日志分流

分析设计

设计思路

公共中心互联网web提供restful接口支持根据设备ID的权重计算进行分流。

核心分析设计点:

将设备ID取hash,将hash进行100取模

判断此设备ID的分布的权重所属区间

权重计算核心代码
/**
     * 根据权重计算出分流分组
     *
     * @param weightGroupList 权重配置列表
     * @param sbid            设备id
     * @return 分组名称
     */
    private String weightCalculate(List<FlowControlGroupWeightDTO> weightGroupList, String sbid) {
        //根据设备id进行hash,再取模
        int hash = sbid.hashCode();
        int index = hash > 0 ? hash : -hash;
        index = index % 100;
 
        String result = "";
        int weightTmp = 0;
        for (FlowControlGroupWeightDTO wg : weightGroupList) {
            if (weightTmp <= index && index < weightTmp + wg.getWeight()) {
                result = wg.getGroup();
                break;
            }
            weightTmp += wg.getWeight();
        }
 
        return result;
    }

应用配置

阿里云分布式配置ACM中增加以下内容

#分流控制分组权重配置
flow.control.group.weight=[{"group":"gd","weight":"70"},{"group":"bj","weight":"30"}]
#需要分流控制的url,多个以,分隔
flow.control.url=/log/app/*

说明:

weight可以配置0-100的数字,要求多组配置的weight之和必须为100

group分支使用的标签替代,调用方需自行判断出对应的域名

flow.control.url配置项使用路径通配符方式配置,调用方再判断url时需考虑通配符的情况

请求接口

请求地址:

http://{ip}:{port}/{context}/web/common/flow/bypass

请求类型:POST

请求入参:

{"sbid":"设备ID"}

返回结果:
{
    "code": "SUCCESS",
    "params": null,
    "message": null,
    "data": {
        "group": "fj",
        "urlList": [
            "/log/app/*",
            "/log/test"
        ]
    },
    "appCodeForEx": null,
    "originalErrorCode": null,
    "rid": null
}

测试方法-JMeter

一开始我采用的JMeter进行测试,模拟1W用户请求,通过请求结果的文本信息搜索关键字,查看分流情况。

 

通过结果可知1W数据里,gd:6975,bj:3025

但这样的测试方法显然是很鸡肋的,单单统计就很麻烦了。考虑到hash 散列方式计算,基数越大才会越准确,所以我打算写脚本进行测试。

测试方法-脚本测试

脚本设计

package com.servyou.test;
 
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
 
public class Demo {
    private static AtomicLong atomicLongBJ = new AtomicLong();
 
    private static AtomicLong atomicLongGD = new AtomicLong();
 
    private final static String url = "http://IP:端口号/web/common/flow/bypass";
 
    public static void main(String[] args) throws InterruptedException {
 
        // 定义10个任务分别负责一定范围内的元素累计
        for (int i = 0; i < 100000; i++) {
 
                    DataBean dataBean = null;
                    dataBean = sendRequest();
 
                    if (dataBean.getData().getGroup().equals("dzswj")) {
                        atomicLongGD.incrementAndGet();
                    }
                    if (dataBean.getData().getGroup().equals("bj")) {
                        atomicLongBJ.incrementAndGet();
 
                    }
                }
        System.out.println("北京:" + atomicLongBJ);
        System.out.println("广东:" + atomicLongGD);
            }
 
 
 
                public static DataBean sendRequest() throws InterruptedException {
                    DeviceEntity deviceEntity = new DeviceEntity();
                    //生成随机数
                    Random random = new Random();
                    deviceEntity.setSbid(String.valueOf((random.nextInt(100) + 1)));
                    String json = JSONObject.toJSONString(deviceEntity);
                    return JSONObject.parseObject(sendPost(url, json), DataBean.class);
                }
 
 
                @Data
                public static class DeviceEntity {
                    private String sbid;
                }
 
                public static String sendPost(String url, String params) throws InterruptedException {
                    CloseableHttpClient httpclient = HttpClients.createDefault();
                    HttpPost httppost = new HttpPost(url);
                    StringEntity entity = new StringEntity(params, ContentType.APPLICATION_JSON);
                    httppost.setEntity(entity);
                    httppost.setHeader("Content-Type", "application/json");
                    CloseableHttpResponse response = null;
                    try {
                        response = httpclient.execute(httppost);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Thread.sleep(200);
                    HttpEntity entity1 = response.getEntity();
                    String result = null;
                    try {
                        result = EntityUtils.toString(entity1);
                    } catch (ParseException | IOException e) {
                        e.printStackTrace();
                    }
                    return result;
                }
 
                @Data
                public static class DataBean {
                    private String code;
                    private Object params;
                    private Object message;
                    private DataBean data;
                    private Object appCodeForEx;
                    private Object originalErrorCode;
                    private Object rid;
 
                    private String group;
                    private List<String> urlList;
 
                    public String getGroup() {
                        return group;
                    }
 
                    public void setGroup(String group) {
                        this.group = group;
                    }
 
                    public List<String> getUrlList() {
                        return urlList;
                    }
 
                    public void setUrlList(List<String> urlList) {
                        this.urlList = urlList;
                    }
                }
 
}

脚本执行

分流配置gd和bj各50%,总数据量10W,执行结果如下:

 

脚本测试

接下来分流配置、总数据量按照测试用例去设置、执行即可。

所以,类似于这样的任务,脚本测试是比较不错的选择。

好啦,以上就是今天想要分享的内容。说实话,一年下来也没写过几次脚本,但脚本测试香是真的香,哈哈哈。

最后: 为了回馈铁杆粉丝们,我给大家整理了完整的软件测试视频学习教程,朋友们如果需要可以自行免费领取 【保证100%免费】

 全套资料获取方式:点击下方小卡片自行领取即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码小怡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值