Friday, May 27, 2011

GUID generation using JMeter

Use the below Java script to generate the GUID in JMeter:

MyGUIDScript=var chars = '0123456789abcdef'.split(''); var uuid = [], rnd =  Math.random, r; uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'; uuid[14]  = '4';  for (var i = 0; i < 36; i++) { if (!uuid[i]) {r = 0 | rnd()*16;  uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r & 0xf]; } }; uuid.join('');

then use this to get the GUID
${__javaScript(${MyGUIDScript},GUID)}

15 comments:

  1. can this be explained more...how to use it and wher to use it jmeter...tahnx ..any help is appeciated....

    ReplyDelete
    Replies
    1. Create the following test plan.

      Test Plan
      --Thread Group
      --User Defined Variable1
      --User Defined Variable2
      --Debug Sampler
      --View Results Tree

      In the User Defined Variable1 enter
      Name: MyGUIDScript
      Value: var chars = '0123456789abcdef'.split(''); var uuid = [], rnd = Math.random, r; uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'; uuid[14] = '4'; for (var i = 0; i < 36; i++) { if (!uuid[i]) {r = 0 | rnd()*16; uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r & 0xf]; } }; uuid.join('');

      In the User Defined Variable2 enter
      Name: GUID
      Value: ${__javaScript(${MyGUIDScript},GUID)}

      And the run the test, you will find the GUID value in debug sampler.

      Delete
    2. Use this "${__javaScript(${MyGUIDScript},GUID)}" to generate GUID

      Delete
    3. Great script, many thanks. Quick question, what is the reason behind the following code in the function?

      uuid[14] = '4';

      Delete
    4. I believe the 4 in that position in the string is to account for the UUID/GUID being "version 4" - see here: http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29

      Delete
  2. Thank you very much :-) that saved me some time, works like a charm!

    ReplyDelete
  3. When I use this in order to generate a random GUID to be inserted into the path of my request, it only gets generated once per test run so all requests use the same GUID. I assume this is because the variable is calculated only once, then used over and over. Is there any way to adapt this to generate once per request or if that would be too costly, to generate a list of them, then use the list?

    ReplyDelete
  4. Just wanted to say thanks for this. very useful!

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Hey, incase anyone is still wondering about Big B's question, if you use a user parameter instead of 2nd user defined variable it will change every iteration (if you check the relevant option). Many thanks for this!

    ReplyDelete
  7. Thanks that works like a charm userParameter helps generate GUID every thread

    ReplyDelete
  8. ...as detailed here:
    http://jmeter.apache.org/usermanual/functions.html#__UUID

    ReplyDelete
  9. Ok, once more with feeling. Two underscores must precede the function name. Hoping this renders correctly: ${__UUID()}

    ReplyDelete
  10. HOW TO PASS UUID FOR EACH SAMPLES AND ALSO HOW WE CAN PASS IN JMS SUBSCRIBER

    ReplyDelete