51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1365|回复: 1
打印 上一主题 下一主题

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

[复制链接]
  • TA的每日心情
    无聊
    10 小时前
  • 签到天数: 978 天

    连续签到: 3 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2022-2-8 09:51:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    业务中常见的测试策略有功能测试、接口测试、性能测试等,而功能测试里常见的有场景测试、接口测试、脚本测试。今天我们来聊聊脚本测试。

      测试任务
      埋点日志分流。

      分析设计
      设计思路
      公共中心互联网web提供restful接口支持根据设备ID的权重计算进行分流。
      核心分析设计点:
      1.将设备ID取hash,将hash进行100取模;
      2.判断此设备ID的分布的权重所属区间;
      3.权重计算核心代码:
    1. /**
    2.      * 根据权重计算出分流分组
    3.      *
    4.      * @param weightGroupList 权重配置列表
    5.      * @param sbid            设备id
    6.      * [url=home.php?mod=space&uid=26358]@return[/url] 分组名称
    7.      */
    8.     private String weightCalculate(List<FlowControlGroupWeightDTO> weightGroupList, String sbid) {
    9.         //根据设备id进行hash,再取模
    10.         int hash = sbid.hashCode();
    11.         int index = hash > 0 ? hash : -hash;
    12.         index = index % 100;

    13.         String result = "";
    14.         int weightTmp = 0;
    15.         for (FlowControlGroupWeightDTO wg : weightGroupList) {
    16.             if (weightTmp <= index && index < weightTmp + wg.getWeight()) {
    17.                 result = wg.getGroup();
    18.                 break;
    19.             }
    20.             weightTmp += wg.getWeight();
    21.         }

    22.         return result;
    23.     }
    复制代码
    应用配置
      阿里云分布式配置ACM中增加以下内容:
    1. #分流控制分组权重配置
    2. flow.control.group.weight=[{"group":"gd","weight":"70"},{"group":"bj","weight":"30"}]
    3. #需要分流控制的url,多个以,分隔
    4. flow.control.url=/log/app/*
    复制代码
    说明:
      1.weight可以配置0-100的数字,要求多组配置的weight之和必须为100
      2.group分支使用的标签替代,调用方需自行判断出对应的域名
      3.flow.control.url配置项使用路径通配符方式配置,调用方再判断url时需考虑通配符的情况

      请求接口
      请求地址:
      http://{ip}:{port}/{context}/web/common/flow/bypass
      请求类型:POST
      请求入参:
    1. {"sbid":"设备ID"}
    复制代码
     返回结果:
    1. {
    2.     "code": "SUCCESS",
    3.     "params": null,
    4.     "message": null,
    5.     "data": {
    6.         "group": "fj",
    7.         "urlList": [
    8.             "/log/app/*",
    9.             "/log/test"
    10.         ]
    11.     },
    12.     "appCodeForEx": null,
    13.     "originalErrorCode": null,
    14.     "rid": null
    15. }
    复制代码
     测试方法-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,执行结果如下:
    脚本测试
      接下来分流配置、总数据量按照测试用例去设置、执行即可。
      所以,类似于这样的任务,脚本测试是比较不错的选择。
      好啦,以上就是今天想要分享的内容。说实话,一年下来也没写过几次脚本,但脚本测试香是真的香,哈哈哈。






    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2021-6-9 14:08
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    2#
    发表于 2022-3-30 17:43:19 | 只看该作者
    这感觉是自动化测试策略
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-7-3 19:16 , Processed in 0.066685 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表