接口测试 Http 接口测试框架索引器

Heyniu · 2016年07月24日 · 最后由 Heyniu 回复于 2016年09月14日 · 1523 次阅读

衔接 Http 接口测试框架

代码这么挫?

是的,就是这么挫

  • 非程序出身
  • 不懂真正的索引器,伪造了个
  • 先实现后优化

代码测试过吗?

代码经过测试的

贴下代码

package search;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class LetterIndexer {

    public static final String SEPARATOR = "|";
    /**
     * sourceList >> 服务器最新接口列表
     */
    private ArrayList<String> sourceList = new ArrayList<String>();
    /**
     * targetList >> 目标列表,遍历后剩下的列表数据就是diff
     */
    private ArrayList<String> targetList = new ArrayList<String>();
    /**
     * hashMap >> 存储字母在sourceList出现的区间
     */
    private HashMap<String, String> hashMap = new HashMap<>();
    /**
     * 是否把sourceList下标为0的首字母存进HashMap
     */
    private boolean isPutFirstLetterForSoureList = false;

    public LetterIndexer(ArrayList<String> sourceList){
        this.sourceList.addAll(sourceList);
        this.targetList.addAll(sourceList); //相等是为了得到差异化文件,本次是初级接口,所以未判断接口是否更新
    }

    public void diff(ArrayList<String> localList) {
        int size = sourceList.size();
        if (size > 0) {
            Collections.sort(sourceList);
            initHashMap(size);
            for (String session : localList) {
                int[] array = getArray(session);
                match(array, session);
            }
        }
    }

    /**
     * 初始化HashMap
     * @param size sourceList大小
     */
    private void initHashMap(int size) {
        String temp = sourceList.get(0).substring(0, 1);
        int index = 0;
        for (int i = 0; i < size; i ++) {
            String firstLetter = sourceList.get(i).substring(0, 1);
            if (!temp.equals(firstLetter)){
                if (!isPutFirstLetterForSoureList) {
                    //sourceList首字母
                    hashMap.put(temp, index + SEPARATOR + (i - 1));
                    isPutFirstLetterForSoureList = true;
                    temp = firstLetter;
                    index = i;
                    continue;
                }
                hashMap.put(temp, index + SEPARATOR + (i - 1));
                temp = firstLetter;
                index = i;
            }
        }
        //sourceList末字母
        hashMap.put(temp, index + SEPARATOR + (size - 1));
    }

    /**
     * 获取本地一个接口的首字母在sourceList的开始结束位置
     * @param session 本地的一个接口
     * @return
     */
    private int[] getArray(String session){
        Set<Entry<String, String>> entrySet = hashMap.entrySet();
        for (Entry<String, String> entry : entrySet) {
            if (entry.getKey().equals(session.substring(0, 1))) {
                String[] str =  entry.getValue().toString().split("\\|");
                int[] array = new int[2];
                array[0] = Integer.parseInt(str[0]);
                array[1] = Integer.parseInt(str[1]);
                return array;
            }
        }
        return null;
    } 

    /**
     * 查询本地的一个接口是否存在sourceList中, 是则在targetList中移除
     * @param str sourceList某字母区间
     * @param letter 本地接口首字母
     * @return
     */
    private void match(int[] str, String session){
                if (str == null) return;
        int index = str[0];
        int size = str[1];
        for (int i = index; i < size; i ++) {
            String temp = sourceList.get(i);
            if (temp.equals(session)) {
                targetList.remove(temp);
            }
        }
    }

}

然后呢?然后就要打脸了

写完后才发现,我当初怎么那么傻,居然想到索引器这种鬼东西,明明 List 原生 API 就有提供去重方法removeAll()

然后说下差距吧

  • 几百条测试数据,我的耗时是 API 的 10 倍(平均值)
  • 几千条测试数据,耗时是 API 的 60 倍(平均值)
  • 几万条测试数据,不知道多少倍,等了 1 分钟还没算出来
  • 明天我就看一下源码,研究下 API 怎么实现的

最后,眼界决定高度,站得高,接触的世界越大

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 8 条回复 时间 点赞
Heyniu [该话题已被删除] 中提及了此贴 07月24日 23:28

有点 bug,明天改下。在 match 方法的 int[] 为空时,会报空指针异常

—— 来自 TesterHome 官方 安卓客户端

周末都拿来研究了,赞一个

#3 楼 @darker50 谢谢支持,一起加油

牛逼啊

有没有 python 版本的。

#6 楼 @cloudwind 有框架已经走通了,细节还需要调整,不久后能公布

没明白这个索引器到底是做什么的。。。LZ 能给讲一下么

#8 楼 @cyj86 这个已经废弃了,看这个吧
https://testerhome.com/topics/5631

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册