使用jython進行dubbo介面及ngrinder效能測試
快速入門(介面測試)
-
確認mvn和jdk、jython安裝ok。
-
先下載dubbo的demo,編譯執行demo。
# git clone https://github.com/alibaba/dubbo.git dubbo # cd dubbo/ # mvn clean install -Dmaven.test.skip # cd dubbo-demo/dubbo-demo-provider/target/ # tar xzvf dubbo-demo-provider-2.5.4-SNAPSHOT-assembly.tar.gz # cd dubbo-demo-provider-2.5.4-SNAPSHOT/bin # ./start.sh # cd /opt/code/dubbo-demo/dubbo-demo-consumer/target/ # tar xzvf dubbo-demo-provider-2.5.4-SNAPSHOT-assembly.tar.gz # cd dubbo-demo-consumer-2.5.4-SNAPSHOT/bin # ./start.sh
注意:阿里的demo啟動指令碼有bug,如果啟動時報程序已經存在,請修改start.sh中的grep部分,增加" | grep -v grep"。
確認在consumer的的日誌可以看到"hello"輸出,恭喜你,dubbo的demo已經成功。現在關閉上面程式,用eclipse或其他IDE開啟工程進行修改。
- 安裝zk, 下載地址:ofollow,noindex">http://www.apache.org/dyn/closer.cgi/zookeeper/
下載完畢後解壓,執行:"# ./zkServer.sh start"
- 修改工程
修改工程dubbo-demo-provider和dubbo-demo-consumer的dubbo.properties:
dubbo.container=log4j,spring dubbo.application.name=demo-provider dubbo.application.owner=william #dubbo.registry.address=multicast://224.5.6.7:1234 dubbo.registry.address=zookeeper://127.0.0.1:2181 #dubbo.registry.address=redis://127.0.0.1:6379 #dubbo.registry.address=dubbo://127.0.0.1:9090 #dubbo.monitor.protocol=registry dubbo.protocol.name=dubbo dubbo.protocol.port=20880 dubbo.service.loadbalance=roundrobin #dubbo.log4j.file=logs/dubbo-demo-consumer.log #dubbo.log4j.level=WARN
dubbo-demo-consumer的pom.xml載入的內容太多,需要進行精簡,如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.alibaba</groupId> <artifactId>dubbo-demo</artifactId> <version>2.5.4-SNAPSHOT</version> </parent> <artifactId>dubbo-demo-consumer</artifactId> <packaging>jar</packaging> <name>${project.artifactId}</name> <description>The demo consumer module of dubbo project</description> <properties> <skip_maven_deploy>false</skip_maven_deploy> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-demo-api</artifactId> <version>${project.parent.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${project.parent.version}</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack</id> <phase>package</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${project.parent.version}</version> <outputDirectory>${project.build.directory}/dubbo</outputDirectory> <includes>META-INF/assembly/**</includes> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptor>src/main/assembly/assembly.xml</descriptor> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
dubbo-demo-consumer工程新增demo.xml, 為jython訪問dubbo的定義。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="hello-world-app"/> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" /> </beans>
新增jython指令碼:
from org.springframework.context.support import ClassPathXmlApplicationContext context = ClassPathXmlApplicationContext("demo.xml") service = context.getBean("demoService") print service.sayHello("How are you!")
重新執行第2步的編譯,並在CLASSPATH新增對應的目錄,比如:
export CLASSPATH=$CLASSPATH:/opt/lib/*
在IDE中執行:DemoProvider
執行:
$ jython dubbo_test.py "my" variable $jythonHome masks earlier declaration in same scope at /usr/bin/jython line 15. log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Hello How are you!, response form provider: 172.17.153.6:20880
java裡面也可以採用這種方法。下面修改DemoConsumer類:
package com.alibaba.dubbo.demo.consumer; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.alibaba.dubbo.demo.DemoService; public class DemoConsumer { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("demo.xml"); DemoService service = context.getBean(DemoService.class); System.out.println(service.sayHello("How are you!")); } }
可見java的操作和jython是極其類似的,只是在實際測試中,java改動需要頻繁,帶來不少不便。實際應用通常把多個包含dubbo服務定義的檔案放在一個jar包中,這樣一個jython就可以靈活地測試多個dubbo介面。當然不同介面要載入的jar是不同的,好在jython可以動態修改CLASSPATH, 參見:http://www.jython.org/jythonbook/en/1.0/appendixB.html#working-with-classpath 。
Jmeter做dubbo效能測試就可以用上面這種方法,繼承AbstractJavaSamplerClient類就可以。參考資料如下:
下面是一個實際使用的jython介面測試指令碼:
#!/usr/local/jython/bin/jython # -*- coding: utf-8 -*- # Author Rongzhong Xu 2016-09-06 wechat: pythontesting """ Name: dubbo.py Tesed in python3.5 """ from org.springframework.context.support import ClassPathXmlApplicationContext from com.oppo.sso.model.request import SecurityRequest context = ClassPathXmlApplicationContext("onekey-register-consumer.xml") service = context.getBean("registerService") request = SecurityRequest() request.setMobile("13244448888") request.setApplicationKey("test") request.setCreateBy("127.0.0.1") request.setCreateIP("127.0.0.1") print("{0} {1} {0}".format("="*30, "Result(")) print(service.register(request)) result = service.register(request) print(result.getResultCode()) print(result.getResultDesc())
效能測試支援
這裡對nGrinder不做入門介紹,相關資料請參考:測試工具nGrinder介紹
nGrinder管理庫的方式和grinder並不一樣。可以通過web操作,但是如果檔案較多的話,還是建議使用svn。
在nGrinder的web頁面點選"指令碼",選中測試目標之後,裡面有個"TestRunner.py"之類的指令碼,在當前目前新建lib目錄,jar包和python庫檔案都可以扔到這裡,這樣nGrinder就可以訪問了。
上面demo的測試指令碼如下:
# -*- coding:utf-8 -*- # A simple example using the HTTP plugin that shows the retrieval of a # single page via HTTP. # # This script is automatically generated by ngrinder. # # @author admin from net.grinder.script.Grinder import grinder from net.grinder.script import Test from net.grinder.plugin.http import HTTPRequest from net.grinder.plugin.http import HTTPPluginControl from java.util import Date from HTTPClient import NVPair, Cookie, CookieModule from org.springframework.context.support import ClassPathXmlApplicationContext control = HTTPPluginControl.getConnectionDefaults() # if you don't want that HTTPRequest follows the redirection, please modify the following option 0. # control.followRedirects = 1 # if you want to increase the timeout, please modify the following option. control.timeout = 6000 test1 = Test(1, "127.0.0.1") request1 = HTTPRequest() # Set header datas headers = [] # Array of NVPair # Set param datas params = [] # Array of NVPair # Set cookie datas cookies = [] # Array of Cookie class TestRunner: # initlialize a thread def __init__(self): test1.record(TestRunner.__call__) grinder.statistics.delayReports=True context = ClassPathXmlApplicationContext("demo.xml") self.service = context.getBean("demoService") def before(self): request1.headers = headers for c in cookies: CookieModule.addCookie(c, HTTPPluginControl.getThreadHTTPClientContext()) # test method def __call__(self): self.before() result = self.service.sayHello("How are you!") print result
jython英文教程:http://www.jython.org/jythonbook/en/1.0/
參考資料
- 技術支援qq群144081101(程式碼和模型存放)
- 本文最新版本地址
- 本文涉及的python測試開發庫 謝謝點贊!
- 本文相關海量書籍下載
- 2018最佳人工智慧機器學習工具書及下載(持續更新)
- 介面測試面試題.pdf
- 軟體測試精品書籍下載
python通過協議支援dubbo介面
以下方式支援dubbo的部分協議,序列化是個難點。
# git clone https://github.com/alibaba/dubbo.git dubbo # cd dubbo/ # mvn clean install -Dmaven.test.skip # cd dubbo-demo/dubbo-demo-provider/target/ # tar xzvf dubbo-demo-provider-2.5.4-SNAPSHOT-assembly.tar.gz # cd dubbo-demo-provider-2.5.4-SNAPSHOT/bin # ./start.sh # cd /opt/code/dubbo-demo/dubbo-demo-consumer/target/ # tar xzvf dubbo-demo-provider-2.5.4-SNAPSHOT-assembly.tar.gz # cd dubbo-demo-consumer-2.5.4-SNAPSHOT/bin # ./start.sh
python環境安裝
# git clone https://github.com/dmall/dudubbo # cd dudubbo/ # git checkout remotes/origin/feature/block-socket # python3 setup.py install
python測試
# /opt/python3.5/bin/python3 Python 3.5.1 (default, May 19 2016, 11:47:26) [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from dubbo import Dubbo >>> from dubbo._model import Object >>> config = { 'classpath' : '/data/code/dubbo/dubbo-demo/dubbo-demo-api/target/dubbo-demo-api-2.5.4-SNAPSHOT.jar' } >>> client = Dubbo((('localhost', 20880),), config, enable_heartbeat=True) >>> q = client.getProxy('com.alibaba.dubbo.demo.DemoService') >>> type(q) <class 'dubbo.dubbo.ServiceProxy'> >>> q.sayHello("Test") Connected to localhost:20880 successfully 'Hello Test, response form provider: 10.51.51.152:20880'