1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| package dubbo;
import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.ReferenceConfig; import com.alibaba.dubbo.config.RegistryConfig; import com.alibaba.dubbo.config.utils.ReferenceConfigCache; import com.alibaba.fastjson.JSON; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; import org.apache.log4j.Logger;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type;
/** * 基类 Created by binli on 2015-12-15. */ public abstract class DubboSampleClient<T> extends AbstractJavaSamplerClient { private static Logger logger = Logger.getLogger(DubboSampleClient.class); /** * privider实例 */ private T instance;
/** * * @return */ protected String getInterfaceName() { String interfaceName = getSuperClassGenricType(getClass(), 0).getName(); System.out.println(interfaceName); return interfaceName; }
/** * 拿到provider的实例 * * @return */ protected T getInstance() { return instance; }
/** * 初始化test * * @param context */ @Override public void setupTest(JavaSamplerContext context) { super.setupTest(context); // 当前应用配置 ApplicationConfig application = new ApplicationConfig(); application.setName(context.getParameter("consumerName1", "jmeter-dubbo-test1"));
// 连接注册中心配置 RegistryConfig registry = new RegistryConfig(); registry.setAddress(context.getParameter("zkAddress", "zktestserver1.wacai.info:22181,zktestserver2.wacai.info:22181,zktestserver3.wacai.info:22181")); registry.setGroup(context.getParameter("dubboGroup", "dubbo_test")); registry.setProtocol("zookeeper"); registry.setClient("zkclient");
/** * 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接 * 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏 */ ReferenceConfig<T> reference = new ReferenceConfig<T>(); reference.setApplication(application); /** * 多个注册中心可以用setRegistries() * */
reference.setRegistry(registry); reference.setInterface(getInterfaceName()); reference.setTimeout(50000); reference.setUrl(context.getParameter("url")); String version = context.getParameter("version"); if(null!=version&&!"".equals(version)){ reference.setVersion("3.0"); } String zkAddress = context.getParameter("zkAddress"); if (null != zkAddress) { if (zkAddress.startsWith("dubbo")) { reference.setUrl(context.getParameter("zkAddress")); } ReferenceConfigCache cache = ReferenceConfigCache.getCache(); instance = cache.get(reference); }
}
/** * 返回给jmeter默认可设置的参数 * * @return */ @Override public Arguments getDefaultParameters() { Arguments args = new Arguments(); args.addArgument("consumerName", "jmeter-dubbo-test"); args.addArgument("dubboGroup", "dubbo_test"); args.addArgument("url", ""); args.addArgument("zkAddress", "zktestserver1.wacai.info:22181,zktestserver2.wacai.info:22181,zktestserver3.wacai.info:22181"); return args; }
/** * 获得泛型 * * @param clazz * @param index * @return */ private Class<Object> getSuperClassGenricType(final Class clazz, final int index) {
Type genType = clazz.getGenericSuperclass();
if (!(genType instanceof ParameterizedType)) { return Object.class; } Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
if (index >= params.length || index < 0) { return Object.class; } if (!(params[index] instanceof Class)) { return Object.class; }
return (Class) params[index]; }
public SampleResult sampleRun(JavaSamplerContext jsc, Action a) { SampleResult result = new SampleResult(); result.sampleStart(); // start stopwatch Object res = null; try { res = a.preform(); } catch (Exception e) { // e.printStackTrace(); logger.error(e.getMessage()); } System.out.println(JSON.toJSON(res)); logger.info("res-----:"+JSON.toJSON(res)); if (null != res) { result.sampleEnd(); // stop stopwatch result.setSuccessful(true); result.setResponseData(JSON.toJSONString(res), "utf-8"); result.setResponseMessage(JSON.toJSONString(res)); result.setResponseCodeOK(); // 200 code } else { result.sampleEnd(); // stop stopwatch result.setSuccessful(false); } return result;
}
/** * The Action Class is for making the Demo View Custom Code */ public interface Action { public Object preform(); }
}
|