测试策略有
功能测试、
接口测试、
性能测试等,而功能测试里常见的有场景测试、接口测试、脚本测试。今天我们来聊聊脚本测试。
测试任务 埋点日志分流。
分析设计
设计思路 公共中心互联网
web提供restful接口支持根据设备ID的权重计算进行分流。 核心分析设计点: 1.将设备ID取hash,将hash进行100取模; 2.判断此设备ID的分布的权重所属区间; 3.权重计算核心代码: /** * 根据权重计算出分流分组 * * @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/*
说明: 1.weight可以配置0-100的数字,要求多组配置的weight之和必须为100 2.group分支使用的标签替代,调用方需自行判断出对应的域名 3.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,执行结果如下:
脚本测试 接下来分流配置、总数据量按照
测试用例去设置、执行即可。 所以,类似于这样的任务,脚本测试是比较不错的选择。 好啦,以上就是今天想要分享的内容。说实话,一年下来也没写过几次脚本,但脚本测试香是真的香,哈哈哈。
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
来这里,成为51Testing签约原创作者!