package edu.pdx.cs410J.whitlock;

import edu.pdx.cs410J.InvokeMainTestCase;
import org.junit.Test;

import java.util.Random;

/**
 * Integration tests for the <code>Student</code> class's main method.
 * These tests extend <code>InvokeMainTestCase</code> which allows them
 * to easily invoke the <code>main</code> method of <code>Student</code>.
 */
public class WordSpinnerPerformanceIT extends InvokeMainTestCase {

  @Test
  public void measurePerformanceOfWordSpinnerWithHugeSentence() {
    HugeSentenceGenerator generator = new HugeSentenceGenerator();
    generator.setSentenceLength(100);
    generator.setMinimumWordSize(4);
    generator.setMaximumWordSize(10);

    String sentence = generator.generateSentence();
    System.out.println(sentence);

    long duration = measureSpinningSentence(sentence);
    System.out.println("It look " + duration + "ms to spin a sentence of length " + sentence.length());
    ;
  }

  private long measureSpinningSentence(String sentence) {
    long start = System.currentTimeMillis();

    WordSpinner.spinSentence(sentence);

    long end = System.currentTimeMillis();

    return end - start;
  }

  private class HugeSentenceGenerator {
    private final Random random = new Random();

    private int sentenceLength;
    private int minimumWordSize;
    private int maximumWordSize;

    void setSentenceLength(int sentenceLength) {
      this.sentenceLength = sentenceLength;
    }

    void setMinimumWordSize(int minimumWordSize) {
      this.minimumWordSize = minimumWordSize;
    }

    void setMaximumWordSize(int maximumWordSize) {
      this.maximumWordSize = maximumWordSize;
    }

    String generateSentence() {
      StringBuilder sb = new StringBuilder(this.sentenceLength);

      while (sb.length() <= this.sentenceLength) {
        sb.append(generateRandomWord());
        sb.append(" ");
      }

      return sb.toString();
    }

    private String generateRandomWord() {
      int wordLength = this.minimumWordSize + this.random.nextInt(maximumWordSize - minimumWordSize);

      StringBuilder sb = new StringBuilder(wordLength);
      for (int i = 0; i < wordLength; i++) {
        sb.append(getRandomLetter());
      }

      return sb.toString();
    }

    private char getRandomLetter() {
      int offset = random.nextInt(26);
      return (char) ('a' + offset);
    }
  }
}

@@ -587,16 +587,23 @@
 ength(10
+_000_00
 0);%0A    
@@ -722,24 +722,26 @@
 Sentence();%0A
+//
     System.o
@@ -751,16 +751,43 @@
 println(
+sentence.length() + %22: %22 + 
 sentence
@@ -1835,26 +1835,14 @@
     
-sb.
 append
-(generate
 Rand
@@ -1848,17 +1848,18 @@
 domWord(
-)
+sb
 );%0A     
@@ -1937,34 +1937,46 @@
 ate 
-String generateRandomWord(
+void appendRandomWord(StringBuilder sb
 ) %7B%0A
@@ -2082,64 +2082,8 @@
 );%0A%0A
-      StringBuilder sb = new StringBuilder(wordLength);%0A
     
@@ -2174,36 +2174,8 @@
  %7D%0A%0A
-      return sb.toString();%0A
     

Use fewer StringBuilder objects when building a huge sentence.