|
13 | 13 | * See the License for the specific language governing permissions and
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 |
| -package com.github.benmanes.caffeine.cache.simulator.parser.rewrite; |
| 16 | +package com.github.benmanes.caffeine.cache.simulator.parser; |
17 | 17 |
|
18 |
| -import java.io.BufferedWriter; |
| 18 | +import java.io.BufferedOutputStream; |
19 | 19 | import java.io.IOException;
|
| 20 | +import java.io.OutputStream; |
| 21 | +import java.io.UncheckedIOException; |
20 | 22 | import java.nio.file.Files;
|
21 | 23 | import java.nio.file.Path;
|
22 | 24 | import java.util.ArrayList;
|
23 | 25 | import java.util.Iterator;
|
24 | 26 | import java.util.List;
|
25 | 27 | import java.util.stream.Stream;
|
26 | 28 |
|
27 |
| -import com.beust.jcommander.JCommander; |
28 |
| -import com.beust.jcommander.Parameter; |
29 |
| -import com.github.benmanes.caffeine.cache.simulator.parser.TraceFormat; |
30 | 29 | import com.github.benmanes.caffeine.cache.simulator.policy.AccessEvent;
|
31 | 30 | import com.google.common.base.Stopwatch;
|
32 | 31 |
|
| 32 | +import picocli.CommandLine; |
| 33 | +import picocli.CommandLine.Help; |
| 34 | +import picocli.CommandLine.Option; |
| 35 | + |
33 | 36 | /**
|
34 | 37 | * A simple utility to rewrite traces for into the format used by other simulators. This lets us
|
35 | 38 | * run multiple simulators in parallel for a quick-and-dirty analysis, rather than port their code
|
36 | 39 | * into Java.
|
37 | 40 | * <p>
|
38 | 41 | * <pre>{@code
|
39 |
| - * ./gradlew :simulator:rewrite -PinputFiles=? -PoutputFile=? -PoutputFormat=? |
| 42 | + * ./gradlew :simulator:rewrite \ |
| 43 | + * -PinputFormat=? \ |
| 44 | + * -PinputFiles=? \ |
| 45 | + * -PoutputFile=? \ |
| 46 | + * -PoutputFormat=? |
40 | 47 | * }</pre>
|
41 | 48 | *
|
42 | 49 | * @author ben.manes@gmail.com (Ben Manes)
|
43 | 50 | */
|
44 | 51 | @SuppressWarnings("PMD.ImmutableField")
|
45 |
| -public final class Rewriter { |
46 |
| - @Parameter(names = "--inputFiles", required = true, description = "The trace input files. To use " |
| 52 | +public final class Rewriter implements Runnable { |
| 53 | + @Option(names = "--inputFiles", required = true, description = "The trace input files. To use " |
47 | 54 | + "a mix of formats, specify the entry as format:path, e.g. lirs:loop.trace.gz")
|
48 | 55 | private List<String> inputFiles = new ArrayList<>();
|
49 |
| - @Parameter(names = "--inputFormat", description = "The default trace input format") |
50 |
| - private TraceFormat inputFormat = TraceFormat.LIRS; |
| 56 | + @Option(names = "--inputFormat", required = true, description = "The default trace input format") |
| 57 | + private TraceFormat inputFormat; |
51 | 58 |
|
52 |
| - @Parameter(names = "--outputFile", description = "The trace output file", required = true) |
| 59 | + @Option(names = "--outputFile", required = true, description = "The trace output file") |
53 | 60 | private Path outputFile;
|
54 |
| - @Parameter(names = "--outputFormat", description = "The trace output format", required = true) |
| 61 | + @Option(names = "--outputFormat", required = true, description = "The trace output format") |
55 | 62 | private OutputFormat outputFormat;
|
56 | 63 |
|
57 |
| - @Parameter(names = "--help", help = true, hidden = true) |
58 |
| - private boolean help; |
59 |
| - |
| 64 | + @Override |
60 | 65 | @SuppressWarnings("PMD.ForLoopCanBeForeach")
|
61 |
| - public void run() throws IOException { |
62 |
| - long count = 0; |
| 66 | + public void run() { |
| 67 | + int tick = 0; |
63 | 68 | Stopwatch stopwatch = Stopwatch.createStarted();
|
64 |
| - try (Stream<AccessEvent> events = inputFormat.readFiles(inputFiles).events(); |
65 |
| - BufferedWriter writer = Files.newBufferedWriter(outputFile)) { |
| 69 | + try (OutputStream output = new BufferedOutputStream(Files.newOutputStream(outputFile)); |
| 70 | + Stream<AccessEvent> events = inputFormat.readFiles(inputFiles).events(); |
| 71 | + TraceWriter writer = outputFormat.writer(output)) { |
| 72 | + writer.writeHeader(); |
66 | 73 | for (Iterator<AccessEvent> i = events.iterator(); i.hasNext();) {
|
67 |
| - outputFormat.write(writer, i.next(), count); |
68 |
| - count++; |
| 74 | + writer.writeEvent(tick, i.next()); |
| 75 | + tick++; |
69 | 76 | }
|
| 77 | + writer.writeFooter(); |
| 78 | + System.out.printf("Rewrote %,d events in %s%n", tick, stopwatch); |
| 79 | + } catch (IOException e) { |
| 80 | + throw new UncheckedIOException(e); |
70 | 81 | }
|
71 |
| - System.out.printf("Rewrote %,d events in %s%n", count, stopwatch); |
72 | 82 | }
|
73 | 83 |
|
74 | 84 | public static void main(String[] args) throws IOException {
|
75 |
| - Rewriter rewriter = new Rewriter(); |
76 |
| - JCommander commander = JCommander.newBuilder() |
77 |
| - .programName(Rewriter.class.getSimpleName()) |
78 |
| - .addObject(rewriter) |
79 |
| - .build(); |
80 |
| - commander.parse(args); |
81 |
| - if (rewriter.help) { |
82 |
| - commander.usage(); |
83 |
| - } else { |
84 |
| - rewriter.run(); |
85 |
| - } |
| 85 | + new CommandLine(Rewriter.class) |
| 86 | + .setColorScheme(Help.defaultColorScheme(Help.Ansi.ON)) |
| 87 | + .setCommandName(Rewriter.class.getSimpleName()) |
| 88 | + .setCaseInsensitiveEnumValuesAllowed(true) |
| 89 | + .execute(args); |
86 | 90 | }
|
87 | 91 | }
|
0 commit comments