Advertisement
scottgalPastes

k6 CSV generator

Oct 1st, 2022 (edited)
1,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*To generate a log file e.g.  k6 run the-best-tests.js --log-format raw --log-output=file="./log_$(((get-date).ToUniversalTime()).ToString("yyyyMMddTHHmmssZ")).txt"
  2. './outfile_en_GB.txt' is a plaiin text file with one 'address' per line (auto generated with Bogus), cut down to (poorly) emulate autocomplete strings.
  3. 585 Favian Flats, South Joe
  4. 015 Hermis
  5. 64348 Candace Corner, North Kirstin, Nor
  6. 8528
  7. 49202 Guiseppe Ranch, Beierha
  8. 8270 Krista Well,
  9. 1028 Braden Estate, Julietfor
  10. 2146 Hellen Key, S
  11. 78838 Rau Plaza, Port Gwenburgh
  12. 6532 Cleo Underpass, New Frankietown, Falkland Islands (Ma
  13. 76290 Rutherford Ranch, Lockmanhaven
  14. 28463 Erdman Meadow
  15. 8502 Levi Islands, Keny
  16. 51686 Ebony Stravenue, Ru
  17. 493 R
  18. 84928 Collier
  19. 6971 Hettinger Light, Taylorchester,
  20. 69877 Furman Ke
  21. 65374 Ansel Union, Ulicesbo
  22. 305 Zaria Glen, North Tessiel
  23. 50725 Tara Walks, Port Kor
  24. 054 Jacobs Light, Georgestad,
  25. 891
  26. 17310 Macejkovic View, South M
  27. 21439 Dayna Port, New Avishaven, Saint Kitt
  28. 9258 Dillan Viaduc
  29. 9998 Blanda Ov
  30. 9021 Clotilde Estate, Providenciburgh, Solomon Isla
  31. 3844 Domenick Plains, Port Berylchester, Th
  32. 35914 Li
  33. 07025 Ks
  34.  
  35. */
  36. import { SharedArray } from 'k6/data';
  37. import { scenario } from 'k6/execution';
  38. import { check, fail }from 'k6'
  39. import http from 'k6/http';
  40. import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js';
  41. import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
  42. const textData = new SharedArray('AddressData', function () {
  43.    return open('./outfile_en_GB.txt').split(/\r?\n/);
  44. });
  45. export const options = {
  46.     scenarios: {
  47.         'use-all-the-data': {
  48.             executor: 'shared-iterations',
  49.             vus: 5,
  50.             iterations: 100,//csvData.length,
  51.             maxDuration: '30s',
  52.         },
  53.     }
  54. };
  55.  
  56. export default function () {
  57.     // this is unique even in the cloud
  58.    
  59.     const address = textData[scenario.iterationInTest];
  60.     var flipBit = randomIntBetween(0,100);
  61.    var urlStr = "https://httpstat.us/200";
  62.     switch (true) {
  63.         case (flipBit < 50 ):
  64.             urlStr ="https://httpstat.us/200";
  65.             break;
  66.         case (flipBit <75):
  67.             urlStr ="https://httpstat.us/418";
  68.             break;
  69.         case (flipBit < 90):
  70.             urlStr ="https://httpstat.us/503";
  71.             break;
  72.         case (flipBit < 100):
  73.             urlStr ="https://httpstat.us/404";
  74.             break;
  75.    
  76.     }
  77.     const url = new URL(urlStr  );
  78.     url.searchParams.append('address', address);
  79.     const res = http.get(url.toString());
  80.     if ( !check(res, {
  81.             'status code MUST be 200': (res) => res.status === 200,
  82.          })
  83.      ) {
  84.         console.log(`${scenario.iterationInTest}, \"${address}\", ${res.url} , ${res.status}`);
  85.     }
  86. }
Tags: k6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement