|
| 1 | +package org.antlr.codebuff.validation; |
| 2 | + |
| 3 | +import org.antlr.codebuff.Corpus; |
| 4 | +import org.antlr.codebuff.Formatter; |
| 5 | +import org.antlr.codebuff.InputDocument; |
| 6 | +import org.antlr.codebuff.Tool; |
| 7 | +import org.antlr.codebuff.misc.BuffUtils; |
| 8 | +import org.antlr.codebuff.misc.LangDescriptor; |
| 9 | +import org.antlr.v4.runtime.misc.Pair; |
| 10 | + |
| 11 | +import java.io.File; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import static org.antlr.codebuff.Tool.getFilenames; |
| 16 | +import static org.antlr.codebuff.Tool.languages; |
| 17 | +import static org.antlr.codebuff.Trainer.FEATURES_HPOS; |
| 18 | +import static org.antlr.codebuff.Trainer.FEATURES_INJECT_WS; |
| 19 | +import static org.antlr.codebuff.misc.BuffUtils.filter; |
| 20 | + |
| 21 | +/** Test the speed of loading (parsing), training on corpus - doc, and formatting one doc. |
| 22 | + * |
| 23 | + * Sample runs: |
| 24 | + * |
| 25 | + * -antlr corpus/antlr4/training/Java8.g4 |
| 26 | + * -java_guava corpus/java/training/guava/cache/LocalCache.java |
| 27 | + * -java8_guava corpus/java/training/guava/cache/LocalCache.java |
| 28 | + */ |
| 29 | + |
| 30 | +public class Speed { |
| 31 | + public static final int TRIALS = 15; |
| 32 | + public static void main(String[] args) throws Exception { |
| 33 | + String langname = args[0].substring(1); |
| 34 | + String testFilename = args[1]; |
| 35 | + LangDescriptor language = null; |
| 36 | + for (int i = 0; i<languages.length; i++) { |
| 37 | + if ( languages[i].name.equals(langname) ) { |
| 38 | + language = languages[i]; |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + if ( language==null ) { |
| 43 | + System.err.println("Language "+langname+" unknown"); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // load all files up front |
| 48 | + long load_start = System.nanoTime(); |
| 49 | + List<String> allFiles = getFilenames(new File(language.corpusDir), language.fileRegex); |
| 50 | + List<InputDocument> documents = Tool.load(allFiles, language); |
| 51 | + long load_stop = System.nanoTime(); |
| 52 | + long load_time = (load_stop-load_start)/1_000_000; |
| 53 | + System.out.printf("Loaded %d files in %dms\n", documents.size(), load_time); |
| 54 | + |
| 55 | + final String path = new File(testFilename).getAbsolutePath(); |
| 56 | + List<InputDocument> others = filter(documents, d -> !d.fileName.equals(path)); |
| 57 | + List<InputDocument> excluded = filter(documents, d -> d.fileName.equals(path)); |
| 58 | + assert others.size() == documents.size() - 1; |
| 59 | + if ( excluded.size()==0 ) { |
| 60 | + System.err.println("Doc not in corpus: "+path); |
| 61 | + return; |
| 62 | + } |
| 63 | + InputDocument testDoc = excluded.get(0); |
| 64 | + |
| 65 | + List<Integer> training = new ArrayList<>(); |
| 66 | + List<Integer> formatting = new ArrayList<>(); |
| 67 | + for (int i = 1; i<=TRIALS; i++) { |
| 68 | + Pair<Integer, Integer> timing = test(language, others, testDoc); |
| 69 | + training.add(timing.a); |
| 70 | + formatting.add(timing.b); |
| 71 | + } |
| 72 | + // drop first four |
| 73 | + training = training.subList(4,training.size()); |
| 74 | + formatting = formatting.subList(4,formatting.size()); |
| 75 | + System.out.printf("median of [4:] training %d\n", BuffUtils.median(training)); |
| 76 | + System.out.printf("median of [4:] formatting %d\n", BuffUtils.median(formatting)); |
| 77 | + } |
| 78 | + |
| 79 | + public static Pair<Integer,Integer> test(LangDescriptor language, |
| 80 | + List<InputDocument> others, |
| 81 | + InputDocument testDoc) |
| 82 | + throws Exception |
| 83 | + { |
| 84 | + long train_start = System.nanoTime(); |
| 85 | + Corpus corpus = new Corpus(others, language); |
| 86 | + corpus.train(); |
| 87 | + long train_stop = System.nanoTime(); |
| 88 | + |
| 89 | + long format_start = System.nanoTime(); |
| 90 | + Formatter formatter = new Formatter(corpus, language.indentSize, Formatter.DEFAULT_K, |
| 91 | + FEATURES_INJECT_WS, FEATURES_HPOS); |
| 92 | + formatter.format(testDoc, false); |
| 93 | + long format_stop = System.nanoTime(); |
| 94 | + |
| 95 | + long train_time = (train_stop-train_start)/1_000_000; |
| 96 | + long format_time = (format_stop-format_start)/1_000_000; |
| 97 | + |
| 98 | + System.out.printf("%s training of %s = %dms formatting = %dms\n", |
| 99 | + language.name, |
| 100 | + testDoc.fileName, |
| 101 | + train_time, |
| 102 | + format_time); |
| 103 | + |
| 104 | + return new Pair<>((int)train_time, (int)format_time); |
| 105 | + } |
| 106 | +} |
0 commit comments