View Javadoc
1   package org.davidmoten.text.utils;
2   
3   import java.io.ByteArrayOutputStream;
4   import java.io.File;
5   import java.io.IOException;
6   import java.io.OutputStreamWriter;
7   import java.io.Writer;
8   import java.nio.charset.StandardCharsets;
9   import java.nio.file.Files;
10  
11  import org.openjdk.jmh.annotations.Benchmark;
12  import org.openjdk.jmh.annotations.Scope;
13  import org.openjdk.jmh.annotations.State;
14  
15  @State(Scope.Benchmark)
16  public class Benchmarks {
17  
18      private static final String text = createText();
19      private static final ByteArrayOutputStream bytes = new ByteArrayOutputStream(128 * 1024);
20  
21      @Benchmark
22      public int wrapNovel() {
23          WordWrap.from(text) //
24                  .maxWidth(80) //
25                  .wrap(createWriter());
26          return bytes.size();
27      }
28  
29      private static Writer createWriter() {
30          bytes.reset();
31          return new OutputStreamWriter(bytes, StandardCharsets.UTF_8);
32      }
33  
34      private static String createText() {
35          byte[] bytes;
36          try {
37              bytes = Files.readAllBytes(
38                      new File("src/test/resources/treasure-island-fragment.txt").toPath());
39          } catch (IOException e) {
40              throw new RuntimeException(e);
41          }
42          return new String(bytes, StandardCharsets.UTF_8);
43      }
44  }