测试工具

前言

类型 版本
操作系统 Mac os / Linux
opendiffy 19.11.8.00
IDEA 2019.03

OpenDiffy部署

  1. 调整 sbt 源,设置为国内源
 #如果没有这个目录和文件,可以先手工创建
 #cat ~/.sbt/repositories
 [repositories]
	local
	huaweicloud-maven: https://repo.huaweicloud.com/repository/maven/
	maven-central: https://repo1.maven.org/maven2/
	sbt-plugin-repo: https://repo.scala-sbt.org/scalasbt/sbt-plugin-releases, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
  1. 编译源码
# 编译时间较长,需耐心等候
# 编辑后的可执行文件在 : target/scala-2.12
git clone https://github.com/twitter/opendiffy.git
cd opendiffy
./sbt assembly
  1. 启动命令
java -jar diffy-server.jar -candidate='localhost:8080' 
                                   -master.primary='localhost:8081' 
                                   -master.secondary='localhost:8082' 
                                   -service.protocol='http' 
                                   -serviceName='My Service' 
                                   -proxy.port=:8880 
                                   -admin.port=:8881 
                                   -http.port=:8888 
                                   -rootUrl='localhost:8888' 
                                   -summary.email="test@test.com.cn" 
                                   -summary.delay="5" 
                                   -allowHttpSideEffects=true
  1. 启动命令解释
    candidate='localhost:9200'  (待上线版本部署地址)
    master.primary='localhost:9000' (已上线版本地址1)
    master.secondary='localhost:9100' (已上线版本地址2)
    service.protocol='http'  (http协议或https)
    serviceName='My Service'  (服务名称,无影响)
    proxy.port=:8880  (diffy代理端口,所以请求都应从这个端口访问)
    admin.port=:8881 ( 通过http://localhost:8881/admin可以查看请求状况)
    http.port=:8888 (查看界面,在这里可以比较差异)
    rootUrl='localhost:8888' (同上)
    responseMode=primary (代理服务器是否返回结果,默认(empty)无返回,可指定primary返回线上版本,secondary(同线上版本,用于噪音消除),candidate(待测试版本)
    excludeHttpHeadersComparison=false (是否排除header的差异,不同服务器,cookie,nginx版本可能有所差异,设置为true可以忽略这些差异)
    summary.email=1"test@test.com.cn" (必填,但没有多大意义)
    summary.delay="5"(延迟时间)
    allowHttpSideEffects=true (为了防止测试对数据造成不必要的影响,diffy默认只支持读,即Post及Delete影响数据的等请求不会转发,如果需要支持这样的请求,需要增加参数)
    

OpenDiffy验证

  1. 编写被测服务A
package com.gykj.test.servicedemo.controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class Hello {

    @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
    public String GetInformation(){
        String name = "test";
        int age = 30;
        String id = "1234567890";

        return String.format("name: %s; age: %d; id: %s", name, age, id);
    }

    @RequestMapping(value = "/setInfo", method = RequestMethod.POST)
    public String SetInformation(HttpServletRequest request,
                                 HttpServletResponse response,
                                 @RequestBody Object object){
        return request.getMethod() + "
" +
                request.getRequestURI() + "
" +
                request.getContentType() + "
" +
                object.getClass() + "
" +
                object.toString();
    }
}
  1. 编写被测服务 B
package com.gykj.test.servicedemo.controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class Hello {

    @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
    public String GetInformation(){
        String name = "wangyong";
        int age = 30;
        String id = "001245731";

        return String.format("name: %s; age: %d; id: %s", name, age, id);
    }

    @RequestMapping(value = "/setInfo", method = RequestMethod.POST)
    public String SetInformation(HttpServletRequest request,
                                 HttpServletResponse response,
                                 @RequestBody Object object){
        return request.getMethod() + "
" +
                request.getRequestURI() + "
" +
                request.getContentType();
    }
}
  1. 启动服务
java -jar servicedemo.8080.jar        # http 服务占用端口8080
java -jar servicedemo.8081.jar         # http 服务占用端口8081
java -jar servicedemo.8082.jar        # http 服务占用端口8082
  1. 启动 OpenDiffy
java -jar diffy-server.jar -candidate='localhost:8080' -master.primary='localhost:8082' -master.secondary='localhost:8081' -service.protocol='http' -serviceName='My Service' -proxy.port=:8880 -admin.port=:8881 -http.port=:8888 -rootUrl='localhost:8888' -summary.email="wangy01@tech.icbc.com.cn" -summary.delay="5" -allowHttpSideEffects=true
  1. 发送请求,可以多发送几次
# 此处服务的地址,为 opendiffy 的地址,由其进行转发
curl -H "Content-Type: application/json;charset=UTF-8" -X POST --data '{"begin}' http://localhost:8880/setInfo
  1. 查看对比分析结果


原文地址:https://www.cnblogs.com/just4life/p/14680954.html