1 /* 2 * Copyright Lodovico Giaretta 2016 - . 3 * Distributed under the Boost Software License, Version 1.0. 4 * (See accompanying file LICENSE_1_0.txt or copy at 5 * http://www.boost.org/LICENSE_1_0.txt) 6 */ 7 8 module benchmark; 9 10 import std.experimental.xml; 11 12 import std.stdio; 13 import std.file; 14 import std.conv; 15 16 void doNotOptimize(T)(auto ref T result) 17 { 18 import std.process: thisProcessID; 19 if (thisProcessID == 1) 20 writeln(result); 21 } 22 23 auto getTestFiles() 24 { 25 return dirEntries("benchmark", SpanMode.shallow); 26 } 27 28 void performTests(void delegate(string) dg) 29 { 30 import core.time; 31 auto i = 1; 32 foreach(string test; getTestFiles) 33 { 34 auto data = readText(test); 35 MonoTime before = MonoTime.currTime; 36 dg(data); 37 MonoTime after = MonoTime.currTime; 38 Duration elapsed = after - before; 39 writeln("test ", i++,": \t", elapsed, "\t(", data.length, " characters)"); 40 } 41 } 42 43 void main() 44 { 45 writeln("\n=== PARSER PERFORMANCE ==="); 46 47 writeln("SliceLexer:"); 48 performTests((data) { 49 auto parser = Parser!(SliceLexer!(string, void delegate()), void delegate())(); 50 parser.setSource(data); 51 foreach (e; parser) 52 { 53 doNotOptimize(e); 54 } 55 }); 56 57 writeln("RangeLexer:"); 58 performTests((data) { 59 auto parser = Parser!(RangeLexer!(string, void delegate()), void delegate())(); 60 parser.setSource(data); 61 foreach (e; parser) 62 { 63 doNotOptimize(e); 64 } 65 }); 66 67 writeln("\n=== CURSOR PERFORMANCE ==="); 68 69 void inspectOneLevel(T)(ref T cursor) 70 { 71 do 72 { 73 foreach(attr; cursor.attributes) 74 doNotOptimize(attr); 75 76 if (cursor.enter) 77 { 78 inspectOneLevel(cursor); 79 cursor.exit(); 80 } 81 } 82 while (cursor.next()); 83 } 84 85 writeln("SliceLexer:"); 86 performTests((data) { 87 auto cursor = Cursor!(Parser!(SliceLexer!(string, void delegate()), void delegate()))(); 88 cursor.setErrorHandler(delegate void(CursorError err) { return; }); 89 cursor.setSource(data); 90 inspectOneLevel(cursor); 91 }); 92 93 writeln("RangeLexer:"); 94 performTests((data) { 95 auto cursor = Cursor!(Parser!(RangeLexer!(string, void delegate()), void delegate()))(); 96 cursor.setErrorHandler(delegate void(CursorError err) { return; }); 97 cursor.setSource(data); 98 inspectOneLevel(cursor); 99 }); 100 }