为UUID编写的Java脚本在jmeter中不起作用

时间:2018-06-28 12:34:30

标签: javascript jmeter jsr223

在下面找到我们在Java脚本中开发的代码,以使用JSR223 sampler(作为Java脚本运行)在jmeter中运行。相同的代码在开发时可在Jquery上使用,但在jmeter中运行时不会发布ID。未定义引发“ appid”的错误。有人可以通过代码帮助调试问题

vars.put("guid", "${__UUID}"); 
vars.put("appId", "ce547c40-acf9-11e6-80f5-76304dec7eb7");
var id=getAppInfo(appId, guid);

函数getAppInfo(appId,guid)     {

    var appInfo = null;
    var appIdBytes = guidToBytes(appId);
    var guidBytes = guidToBytes(guid);
    var appInfoBytes = [];
    for (var cnt = 0; cnt < appIdBytes.length; cnt++) 
    {
        appInfoBytes[cnt] = appIdBytes[cnt] + guidBytes[cnt];
    }
    var appInfoGuidfromBytes = bytesToGuid(appInfoBytes);
    return appInfoGuidfromBytes;
}  

function bytesToGuid(guidBytes) {
    var x = guidBytes;
    var result = "";
    var bytes = x.slice(0, 4).reverse().concat(x.slice(4, 6).reverse()).concat(x.slice(6, 8).reverse()).concat(x.slice(8));

    var y = bytes.map(function (item) { return ('00' + item.toString(16)).substr(-2, 2) });
    var byteArray = y;
    for (var cnt = 0; cnt < byteArray.length; cnt++) {
        if (cnt === 4 || cnt === 6 || cnt === 8 || cnt === 10)
            result = result + "-" + byteArray[cnt];
        else
            result = result + byteArray[cnt];
    }
    return result;
}
function guidToBytes(guid) {
    var bytes = [];
    guid.split('-').map(function (number, index) {
        var bytesInChar = index < 3 ? number.match(/.{1,2}/g).reverse() : number.match(/.{1,2}/g);
        bytesInChar.map(function (byte) { bytes.push(parseInt(byte, 16)); });
    });
    return bytes;
}

// Create the Randon Number. It will call from NewGuid function.
function getRandomNumber() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);    
};
// Creating GUID eg. "cbe26df8-2b01-4377-9ae8-1d023ccd5171"
// getRandomNumber returns 4 digit Alphanumeric number and we are going to concatenating this with "-" symbol and third number starts with "-4" and substring the random number and concatenate the string and return.

function newGuid() {
    return (getRandomNumber() + getRandomNumber() + "-" + getRandomNumber() + "-4" + getRandomNumber().substr(0, 3) + "-" + getRandomNumber() + "-" + getRandomNumber() + getRandomNumber() + getRandomNumber()).toLowerCase();
};

1 个答案:

答案 0 :(得分:0)

您不应该在JSR223 script内部调用JMeter的函数。

相反,将其作为${__UUID}添加到“参数”字段

,然后通过args使用它:

vars.put("guid", args[0]); 
  

args-参数为String数组(在空白处分割)