BenchSyntaxHighlighter.Mod 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. MODULE BenchSyntaxHighlighter; (** AUTHOR "staubesv"; PURPOSE "Benchmarks for SyntaxHighlighter"; *)
  2. IMPORT
  3. Streams, Commands, Options, Dates, Strings, Files, Random, Texts, TextUtilities, SyntaxHighlighter;
  4. CONST
  5. DefaultHighlighterName = "Oberon";
  6. DefaultNofIterations = 1000;
  7. PROCEDURE Reader(reader : Texts.TextReader; nofIterations : LONGINT; out : Streams.Writer);
  8. VAR char32 : Texts.Char32; startTime, endTime : Dates.DateTime; i : LONGINT;
  9. BEGIN
  10. ASSERT((reader # NIL) & (nofIterations > 0) & (out # NIL));
  11. out.String("Reading text "); out.Int(nofIterations, 0); out.String(" times ... "); out.Update;
  12. startTime := Dates.Now();
  13. FOR i := 1 TO nofIterations DO
  14. reader.SetPosition(0);
  15. REPEAT
  16. reader.ReadCh(char32);
  17. UNTIL reader.eot;
  18. END;
  19. endTime := Dates.Now();
  20. Strings.ShowTimeDifference(startTime, endTime, out); out.Ln;
  21. END Reader;
  22. PROCEDURE Words(reader : Texts.TextReader; highlighter : SyntaxHighlighter.Highlighter; nofIterations : LONGINT; out : Streams.Writer);
  23. VAR
  24. char32 : Texts.Char32; style : SyntaxHighlighter.Style;
  25. wordEnd, readerPosition, i : LONGINT;
  26. startTime, endTime : Dates.DateTime;
  27. BEGIN
  28. ASSERT((reader # NIL) & (highlighter # NIL) & (nofIterations > 0) & (out # NIL));
  29. out.String("Match words "); out.Int(nofIterations, 0); out.String(" times ... "); out.Update;
  30. startTime := Dates.Now();
  31. FOR i := 1 TO nofIterations DO
  32. wordEnd := -1;
  33. reader.SetPosition(0);
  34. REPEAT
  35. readerPosition := reader.GetPosition();
  36. reader.ReadCh(char32);
  37. IF (readerPosition > wordEnd) & (char32 > 32) THEN
  38. style := highlighter.GetWordStyle(reader, readerPosition, wordEnd);
  39. reader.SetPosition(readerPosition);
  40. reader.ReadCh(char32);
  41. END;
  42. UNTIL reader.eot;
  43. END;
  44. endTime := Dates.Now();
  45. Strings.ShowTimeDifference(startTime, endTime, out); out.Ln;
  46. END Words;
  47. PROCEDURE RebuildRegions(reader : Texts.TextReader; highlighter : SyntaxHighlighter.Highlighter; nofIterations : LONGINT; out : Streams.Writer);
  48. VAR
  49. state : SyntaxHighlighter.State;
  50. startTime, endTime : Dates.DateTime;
  51. i : LONGINT;
  52. BEGIN
  53. ASSERT((reader # NIL) & (highlighter # NIL) & (nofIterations > 0) & (out # NIL));
  54. state := highlighter.GetState();
  55. out.String("Rebuild regions "); out.Int(nofIterations, 0); out.String(" times ... "); out.Update;
  56. startTime := Dates.Now();
  57. FOR i := 1 TO nofIterations DO
  58. highlighter.RebuildRegions(reader, state);
  59. END;
  60. endTime := Dates.Now();
  61. Strings.ShowTimeDifference(startTime, endTime, out); out.Ln;
  62. END RebuildRegions;
  63. PROCEDURE RegionLookup(reader : Texts.TextReader; highlighter : SyntaxHighlighter.Highlighter; nofIterations : LONGINT; out : Streams.Writer);
  64. VAR
  65. style : SyntaxHighlighter.Style;
  66. state : SyntaxHighlighter.State;
  67. random : Random.Generator;
  68. length, position, start, end, i : LONGINT;
  69. startTime, endTime : Dates.DateTime;
  70. BEGIN
  71. ASSERT((reader # NIL) & (highlighter # NIL) & (nofIterations > 0) & (out # NIL));
  72. state := highlighter.GetState();
  73. NEW(random);
  74. length := reader.text.GetLength();
  75. highlighter.RebuildRegions(reader, state);
  76. out.String("Region lookup"); out.Int(nofIterations, 0); out.String(" times ... "); out.Update;
  77. startTime := Dates.Now();
  78. FOR i := 1 TO nofIterations DO
  79. position := random.Dice(length);
  80. style := highlighter.GetRegionStyle(position, state, start, end);
  81. END;
  82. endTime := Dates.Now();
  83. Strings.ShowTimeDifference(startTime, endTime, out); out.Ln;
  84. END RegionLookup;
  85. PROCEDURE Full(reader : Texts.TextReader; highlighter : SyntaxHighlighter.Highlighter; nofIterations : LONGINT; out : Streams.Writer);
  86. VAR
  87. char32 : Texts.Char32; style : SyntaxHighlighter.Style;
  88. state : SyntaxHighlighter.State;
  89. startTime, endTime : Dates.DateTime;
  90. readerPosition, regionStart, regionEnd, lastEnd, i : LONGINT;
  91. BEGIN
  92. ASSERT((reader # NIL) & (highlighter # NIL) & (nofIterations > 0) & (out # NIL));
  93. state := highlighter.GetState();
  94. out.String("Full highlighting "); out.Int(nofIterations, 0); out.String(" times ... "); out.Update;
  95. startTime := Dates.Now();
  96. FOR i := 1 TO nofIterations DO
  97. reader.SetPosition(0);
  98. lastEnd := -1;
  99. REPEAT
  100. readerPosition := reader.GetPosition();
  101. reader.ReadCh(char32);
  102. IF (lastEnd < readerPosition) THEN
  103. style := NIL;
  104. style := highlighter.GetRegionStyle(readerPosition, state, regionStart, regionEnd);
  105. IF (style # NIL) THEN
  106. lastEnd := regionEnd;
  107. ELSE
  108. IF (char32 > 32) THEN
  109. style := highlighter.GetWordStyle(reader, readerPosition, lastEnd);
  110. reader.SetPosition(readerPosition);
  111. reader.ReadCh(char32);
  112. END;
  113. END;
  114. END;
  115. UNTIL reader.eot;
  116. END;
  117. endTime := Dates.Now();
  118. Strings.ShowTimeDifference(startTime, endTime, out); out.Ln;
  119. END Full;
  120. PROCEDURE Indent(writer : Streams.Writer; width : LONGINT);
  121. VAR i : LONGINT;
  122. BEGIN
  123. FOR i := 1 TO width DO writer.Char(" "); END;
  124. END Indent;
  125. PROCEDURE Bench*(context : Commands.Context); (** [Options] filename [benchmark] ~ *)
  126. VAR
  127. filename : Files.FileName; highlighterName, benchmark : ARRAY 64 OF CHAR; nofIterations : LONGINT;
  128. options : Options.Options;
  129. text : Texts.Text; reader : Texts.TextReader;
  130. format : LONGINT; res: WORD;
  131. highlighter : SyntaxHighlighter.Highlighter;
  132. BEGIN
  133. NEW(options);
  134. options.Add("h", "highlighter" , Options.String);
  135. options.Add("n", "nofIterations", Options.Integer);
  136. IF options.Parse(context.arg, context.error) THEN
  137. benchmark := "";
  138. context.arg.SkipWhitespace; context.arg.String(filename);
  139. context.arg.SkipWhitespace; context.arg.String(benchmark);
  140. IF ~options.GetString("highlighter", highlighterName) THEN highlighterName := DefaultHighlighterName; END;
  141. IF ~options.GetInteger("nofIterations", nofIterations) THEN nofIterations := DefaultNofIterations; END;
  142. IF (nofIterations > 0) THEN
  143. highlighter := SyntaxHighlighter.GetHighlighter(highlighterName);
  144. IF (highlighter # NIL) THEN
  145. NEW(text);
  146. TextUtilities.LoadAuto(text, filename, format, res);
  147. IF (res = 0) THEN
  148. context.out.String(filename); context.out.String(": ");
  149. NEW(reader, text);
  150. text.AcquireRead;
  151. IF (benchmark = "") THEN
  152. context.out.Ln;
  153. Indent(context.out, 4); Reader(reader, nofIterations, context.out); context.out.Update;
  154. Indent(context.out, 4); Words(reader, highlighter, nofIterations, context.out); context.out.Update;
  155. Indent(context.out, 4); RebuildRegions(reader, highlighter, nofIterations, context.out); context.out.Update;
  156. Indent(context.out, 4); RegionLookup(reader, highlighter, nofIterations, context.out); context.out.Update;
  157. Indent(context.out, 4); Full(reader, highlighter, nofIterations, context.out); context.out.Update;
  158. ELSIF (benchmark = "reader") THEN
  159. Reader(reader, nofIterations, context.out);
  160. ELSIF (benchmark = "words") THEN
  161. Words(reader, highlighter, nofIterations, context.out);
  162. ELSIF (benchmark = "rebuildregions") THEN
  163. RebuildRegions(reader, highlighter, nofIterations, context.out);
  164. ELSIF (benchmark = "regionlookup") THEN
  165. RegionLookup(reader, highlighter, nofIterations, context.out);
  166. ELSIF (benchmark = "full") THEN
  167. Full(reader, highlighter, nofIterations, context.out);
  168. ELSE
  169. context.error.String("Unknown benchmark: "); context.error.String(benchmark); context.error.Ln;
  170. END;
  171. text.ReleaseRead;
  172. ELSE
  173. context.error.String("Could not open file "); context.error.String(filename);
  174. context.error.Ln;
  175. END;
  176. ELSE
  177. context.error.String("Highlighter "); context.error.String(highlighterName);
  178. context.error.String(" not found."); context.error.Ln;
  179. END;
  180. ELSE
  181. context.error.String("Parameter error: {nofIterations > 0}!"); context.error.Ln;
  182. END;
  183. END;
  184. END Bench;
  185. PROCEDURE TestScanner*(context : Commands.Context); (** [options] filename highlighterName ~ *)
  186. VAR
  187. options : Options.Options;
  188. filename : Files.FileName; highlighterName : ARRAY 64 OF CHAR;
  189. highlighter : SyntaxHighlighter.Highlighter;
  190. text : Texts.Text; reader : Texts.TextReader; char32 : Texts.Char32;
  191. token : SyntaxHighlighter.Token;
  192. position, format : LONGINT; res: WORD;
  193. BEGIN
  194. NEW(options);
  195. options.Add("d", "details", Options.Flag);
  196. IF options.Parse(context.arg, context.error) THEN
  197. filename := "";
  198. context.arg.SkipWhitespace; context.arg.String(filename);
  199. highlighterName := "";
  200. context.arg.SkipWhitespace; context.arg.String(highlighterName);
  201. highlighter := SyntaxHighlighter.GetHighlighter(highlighterName);
  202. IF (highlighter # NIL) THEN
  203. NEW(text);
  204. TextUtilities.LoadAuto(text, filename, format, res);
  205. IF (res = 0) THEN
  206. context.out.String("Token chain for file "); context.out.String(filename); context.out.String(":"); context.out.Ln;
  207. text.AcquireRead;
  208. NEW(reader, text);
  209. reader.SetPosition(0); position := 0;
  210. reader.ReadCh(char32);
  211. REPEAT
  212. (* skip whitespace *)
  213. WHILE (char32 <= 32) & ~reader.eot DO reader.ReadCh(char32); INC(position); END;
  214. IF ~reader.eot THEN
  215. ASSERT(char32 > 32);
  216. context.out.Ln; context.out.String("Scan "); context.out.Int(position, 0); context.out.Ln;
  217. reader.SetPosition(position);
  218. highlighter.GetToken(reader, position, token);
  219. context.out.String(" -> ");
  220. CASE token.type OF
  221. |SyntaxHighlighter.Type_Invalid: context.out.String("INV");
  222. |SyntaxHighlighter.Type_Identifier: context.out.String("ID("); context.out.String(token.value);
  223. |SyntaxHighlighter.Type_Number: context.out.String("NUM("); context.out.String(token.value);
  224. |SyntaxHighlighter.Type_Token: context.out.String("T("); context.out.String(token.value);
  225. ELSE
  226. context.out.String("UNKNOWN");
  227. END;
  228. IF (token.type = SyntaxHighlighter.Type_Identifier) OR (token.type = SyntaxHighlighter.Type_Number) OR (token.type = SyntaxHighlighter.Type_Token) THEN
  229. IF options.GetFlag("details") THEN
  230. context.out.String(", ");
  231. context.out.Int(token.startPosition, 0); context.out.String("..");
  232. context.out.Int(token.endPosition, 0);
  233. END;
  234. context.out.String(")");
  235. END;
  236. IF (token.type # SyntaxHighlighter.Type_Invalid) THEN
  237. reader.SetPosition(token.endPosition + 1); position := token.endPosition + 1;
  238. ELSE
  239. INC(position);
  240. END;
  241. reader.ReadCh(char32);
  242. END;
  243. UNTIL reader.eot;
  244. text.ReleaseRead;
  245. ELSE
  246. context.error.String("Could not open file "); context.error.String(filename); context.error.String(", res = ");
  247. context.error.Int(res, 0); context.error.Ln;
  248. END;
  249. ELSE
  250. context.error.String("Highlighter "); context.error.String(highlighterName); context.error.String(" not found.");
  251. context.error.Ln;
  252. END;
  253. END;
  254. END TestScanner;
  255. END BenchSyntaxHighlighter.
  256. System.Free BenchSyntaxHighlighter ~
  257. BenchSyntaxHighlighter.Bench Usb.Mod reader ~
  258. BenchSyntaxHighlighter.Bench BIOS.I386.Machine.Mod reader ~
  259. BenchSyntaxHighlighter.Bench Usb.Mod words ~
  260. BenchSyntaxHighlighter.Bench BIOS.I386.Machine.Mod words ~
  261. BenchSyntaxHighlighter.Bench Usb.Mod rebuildregions~
  262. BenchSyntaxHighlighter.Bench BIOS.I386.Machine.Mod rebuildregions ~
  263. BenchSyntaxHighlighter.Bench -n=1000000 Usb.Mod regionlookup~
  264. BenchSyntaxHighlighter.Bench BIOS.I386.Machine.Mod regionlookup ~
  265. BenchSyntaxHighlighter.Bench Usb.Mod full ~
  266. BenchSyntaxHighlighter.Bench BIOS.I386.Machine.Mod full ~
  267. BenchSyntaxHighlighter.Bench Usb.Mod ~
  268. BenchSyntaxHighlighter.Bench BIOS.I386.Machine.Mod ~
  269. Tests:
  270. BenchSyntaxHighlighter.TestScanner Test.Mod Oberon ~
  271. BenchSyntaxHighlighter.TestScanner --details Test.Mod Oberon ~
  272. System.DoCommands
  273. FSTools.Enumerate *.Mod BenchSyntaxHighlighter.Bench -n=1 <#filename#> ~
  274. FSTools.Enumerate *.XML BenchSyntaxHighlighter.Bench -n=1 <#filename#> ~
  275. FSTools.Enumerate *.TOOL BenchSyntaxHighlighter.Bench -n=1 <#filename#> ~
  276. FSTools.Enumerate *.C BenchSyntaxHighlighter.Bench -n=1 <#filename#> ~
  277. ~