您好,请问是否可以实现类似loadrunner的参数化。即参数是从文件读取,而每个新的负载进行脚本执行时读取的是文件的下一个参数?我这里如果负载比较多如果用多个文件执行可能会比较麻烦
|
在创建脚本时,可以创建resource文件,你可以把参数写到这个文件里面,然后再脚本里面读取文件,用随机或者顺序的方式使用里面的参数来调用相应的request。 注意,读取文件应该是在初始化的时候只读取一次,然后把参数数据写到一个类似list的容器里。避免每个执行方法里面去读取。
具体方法看: http://www.cubrid.org/wiki_ngrinder/entry/how-to-use-resources
-----Original Message----- If you reply to this email, your message will be added to the discussion below:
http://ngrinder.642.n7.nabble.com/loadrunner-tp1746.html
To start a new topic under ngrinder-user-cn, email [hidden email]
To unsubscribe from ngrinder-user-cn, click here. NAML |
抱歉我没有描述的太清楚。我现在遇到的问题是这样的,我目前用jython写脚本(从the grinder录制场景),我的参数化文件是写在resource里的param文件中。在脚本里我的文件读取代码写在class TestRunner之前,但是,我的Test只写了一个。执行测试的方法中URI需要传参进行测试,这个参数就是我进行参数化的目标。假如我的param文件中有10个数据,设置虚拟用户也是10个,现在希望实现的是,每个用户执行的URI传的参数是param中依次读取的,即用户1传入param中第1个数据;用户2传入param中第2个数据;用户3传入param中第3 个数据;以此类推。我曾经考虑用获取当前grinder的线程number去对应实现,但看到100个用户以后可能会有问题需要其他方式实现,实例代码是groovy的且脚本中的
agentNumber = grinder.agentNumber processNumber = grinder.processNumber threadNumber = grinder.threadNumber 我用jython获取不到,所以可能还需要您这里解答一下 |
你是怎么解决的
|
In reply to this post by Mavlarn
兄弟你好,我碰到和楼主类似的问题,但用你的方法无法读取文件。
我的 txt 文件上传放在 resources 目录下,但执行脚本时候读不进来。 跑 ngrinder 后台去看,执行时候的临时生成的 resources 目录下其实是有加载进来 txt 文件的。 能否帮忙看看?谢谢! 代码是:file = open("./resources/1.txt", "r") 报错如下: 2016-08-04 21:42:14,617 ERROR Aborted run: Jython exception: <type 'exceptions.IOError'>: (2, 'No such file or directory', 'ngrinder_req.py') [calling TestRunner] 后台上看,有这个文件的: [root@CDVM-213025073 resources]# ll total 4 -rw-r--r-- 1 root root 10 Aug 4 21:42 1.txt 版本是: The Grinder version 3.9.1 Jython 2.5.3 |
我没记错的话,应该是:
file = open("./1.txt", "r") |
In reply to this post by pyqf66
qq:281955050 求指导,参数化唯一性问题(每台机器每个线程顺序读取参数,需要进行文件分割?分到各机器的进程内存中?)。
|
In reply to this post by pyqf66
可以实现的,使用java写个类,生成:
package com.didi.utils; /** * * * */ public class GCounter { // 定义一个私有构造方法 private GCounter() { } private static volatile long counter=10000; synchronized public static String getIstance(int processNumber) { counter=counter+1; return processNumber+"->"+counter; } } ===== 调用的groovy脚本: import static net.grinder.script.Grinder.grinder import static org.junit.Assert.* import static org.hamcrest.Matchers.* import net.grinder.plugin.http.HTTPRequest import net.grinder.plugin.http.HTTPPluginControl import net.grinder.script.GTest import net.grinder.script.Grinder import net.grinder.scriptengine.groovy.junit.GrinderRunner import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3 import org.junit.Before import org.junit.BeforeClass import org.junit.Test import org.junit.runner.RunWith import java.util.Date import java.util.List import java.util.ArrayList import HTTPClient.Cookie import HTTPClient.CookieModule import HTTPClient.HTTPResponse import HTTPClient.NVPair import java.sql.DriverManager import java.sql.SQLException import java.sql.Statement import java.sql.ResultSet import com.mysql.jdbc.Connection import com.didi.utils.GCounter import com.didi.utils.JavaStackTrace /** * 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 */ @RunWith(GrinderRunner) class TestRunner { public static String msg public static GTest test public static HTTPRequest request public static NVPair[] headers = [] public static NVPair[] params = [] public static Cookie[] cookies = [] //增加数据 public static Boolean addUser(){ Connection con = null Statement stmt = null ResultSet rs = null boolean status=true String msg="" //def GCounter de=new GCounter(); int agentNumber = grinder.agentNumber int processNumber = grinder.processNumber int threadNumber = grinder.threadNumber //long threadId = GrinderUtils.threadUniqId String counter=GCounter.getIstance(processNumber); try{ Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://10.94.66.30:3309/zblog", "root", "123456"); stmt = con.createStatement(); //stmt = con.createStatement(); String name="A_"+counter+"|"+agentNumber+"_"+processNumber+"_"+threadNumber; String pass="PW_"+counter; String sql="insert into user(`username`,`password`) values ('"+name+"','"+pass+"')"; stmt.executeUpdate(sql); }catch(SQLException e){ status=false; msg=JavaStackTrace.errorStack2String(e); grinder.logger.info("错误:"+msg); }catch(ClassNotFoundException e){ status=false; msg=JavaStackTrace.errorStack2String(e); grinder.logger.info("错误:"+msg); }finally{ try{stmt.close();}catch(Exception e){} try{con.close();}catch(Exception e){} } return status } public static void jdbcTest() { Connection con = null; Statement stmt = null; ResultSet rs = null; try{ Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://10.94.66.30:3309/zblog", "root", "123456"); stmt = con.createStatement(); rs = stmt.executeQuery("select * from user"); while (rs.next()) { System.out.println("user id: " + rs.getLong(1) + " username: " + rs.getString(2) + " pass: " + rs.getString(3)); } }catch(SQLException e){ e.printStackTrace(); }catch(ClassNotFoundException e){ e.printStackTrace(); }finally{ try{rs.close();}catch(Exception e){} try{stmt.close();}catch(Exception e){} try{con.close();}catch(Exception e){} } } @BeforeProcess public static void beforeProcess() { HTTPPluginControl.getConnectionDefaults().timeout = 6000 test = new GTest(1, "Test1") request = new HTTPRequest() grinder.logger.info("before process."); } @BeforeThread public void beforeThread() { test.record(this, "test") grinder.statistics.delayReports=true; grinder.logger.info("before thread."); } @Before public void before() { request.setHeaders(headers) cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) } grinder.logger.info("before thread. init headers and cookies"); } @Test public void test(){ //HTTPResponse result = request.GET("http://10.94.120.205:8866/ci/login?from=%2Fci%2F", params) //jdbcTest(); def status=addUser(); if(status==false){ grinder.logger.info("添加用户失败。。。"); assertThat(100, is(200)); }else{ assertThat(200, is(200)); } //if (result.statusCode == 301 || result.statusCode == 302 || result.statusCode == 403 || status==false) { // grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode); //} else { // //} } } |
Free forum by Nabble | Edit this page |