Adding javascript function to jMeter using jsr223

*爱你&永不变心* 提交于 2019-12-25 04:57:18

问题


I am trying to get a javascript function work with jMeter test plan uing JSR223. It is used to decode a string. I have the below two functions which i need to implement in jmeter:

function AESEncryption(text, passphase, bytessize) {

var key = CryptoJS.enc.Utf8.parse('ABCDEFGHIJKL1234567891234');
var iv = CryptoJS.enc.Utf8.parse('1234567890123456');
var blocksize = bytessize / 2;
var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(text), passphase, key,
{
    keySize: bytessize,
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
});
var dta = String(encrypted);
return dta;}


function AESDecryption(text, key, bytessize) {
try {
    //alert(text + ":" + key + ":" + bytessize);
    var e = CryptoJS.AES.decrypt(text, key, bytessize);
    //alert("Ec:" + e);
    return CryptoJS.AES.decrypt(text, key, bytessize).toString(CryptoJS.enc.Utf8);
}
catch (Error) {
    return "";}}

回答1:


You can import external JavaScript into JSR223 Sampler using load directive.

  1. Download latest release of crypto-js from https://github.com/brix/crypto-js/releases (I used 3.1.9) and unpack it to JMeter's "bin" folder
  2. Add the next line to the beginning of your JSR223 script:

    load('crypto-js-3.1.9/crypto-js.js');
    
  3. You should be able to access your JavaScript functions directly in the JSR223 Sampler


Be aware that JavaScript being interpreted via Rhino/Nashorn has serious performance drawbacks therefore if you are planning to call this funciton by many threads it makes more sense to rewrite your functions in Groovy.



来源:https://stackoverflow.com/questions/41717966/adding-javascript-function-to-jmeter-using-jsr223

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!