Forum Discussion

gomezsergio77's avatar
gomezsergio77
Regular Visitor
8 years ago

Randomizing DataSource rows: data loops gets stuck

Hi,

I need to get a random row from a file. First approach that I used was the proposed in https://www.soapui.org/scripting-properties/tips-tricks.html#2-3-randomizing-datasource-rows&_ga=1.130692043.571140239.1467297572. It is working ok for small amount of rows but when I tried to use more than 1000, Ready Api! starts to consume a lot of memory and test case gets stuck in the data loop. Are there limitation for this solution? Do you know what could be happening? 

 

1 Reply

  • M_McDonald's avatar
    M_McDonald
    Super Contributor

    If you just need the entire line from a text file, you can do something like the following:

     

     

     

     

     

     

    1. Create a DataSource as a Data Generator for random integers to use as the line number

    2. Create a Groovy Script step that reads the line from the file:

     

    String readLine( File f, int n ) {
      String line = null;
      f.withReader { r ->
        while( n-- > 0 && ( ( line = r.readLine() ) != null ) ) ;
      }
      return line;
    }
    
    def lineNum = context.expand('${Get Random Line Number#lineNum}') as Integer;
    
    File infile = new File("C:\\local\\files\\lesmis.txt")
    String line = readLine(infile, lineNum )
    
    log.info("$lineNum: $line");
    return line;

     

    3. Create a DataSource Loop step that points back to the Groovy Script step.

    4. Put your steps between the Groovy Script step and the DataSource Loop, referencing the Groovy Script step result property:

     

    ${Get Random Row#result}

     

    This method reads every row in the file up to the selected one each time through, so it will be slower on larger files, but does not require loading all rows into memory. 1000 reads from a 50000 line file took about 15 seconds to complete.

     

    Code adapted from http://stackoverflow.com/questions/8223264/read-a-specific-line-of-a-file-with-groovy