PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/SharpTAL.Demo/Demo.cs

https://bitbucket.org/rlacko/sharptal
C# | 236 lines | 184 code | 16 blank | 36 comment | 6 complexity | 8031c0073b3e2dc5594151381d166f84 MD5 | raw file
Possible License(s): Apache-2.0
  1. //
  2. // Demo.cs
  3. //
  4. // Author:
  5. // Roman Lacko (backup.rlacko@gmail.com)
  6. //
  7. // Copyright (c) 2010 - 2013 Roman Lacko
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. namespace SharpTAL.Demo
  29. {
  30. using System;
  31. using System.Linq;
  32. using System.Collections.Generic;
  33. using System.Text;
  34. using System.IO;
  35. using System.Reflection;
  36. using System.Xml;
  37. using System.Diagnostics;
  38. using System.Collections;
  39. using SharpTAL.TemplateProgram;
  40. using SharpTAL.TemplateCache;
  41. public static class DemoExtensions
  42. {
  43. public static string XmlToString(this XmlDocument xml)
  44. {
  45. StringBuilder sb = new StringBuilder();
  46. StringWriter sw = new StringWriter(sb);
  47. xml.Save(sw);
  48. return sb.ToString();
  49. }
  50. public static string ToUpperExtension(this string s)
  51. {
  52. return s.ToUpper();
  53. }
  54. }
  55. public class Friend
  56. {
  57. public string Name;
  58. public int Age;
  59. }
  60. class Demo
  61. {
  62. static void Main(string[] args)
  63. {
  64. // Referenced Assemblies
  65. List<Assembly> refAssemblies = new List<Assembly>() { typeof(Demo).Assembly };
  66. // Globals
  67. Dictionary<string, object> globals = new Dictionary<string, object>()
  68. {
  69. {
  70. "friends", new List<Friend>()
  71. {
  72. new Friend() { Name="Samantha", Age=33 },
  73. new Friend() { Name="Kim", Age=35 },
  74. new Friend() { Name="Sandra", Age=22 },
  75. new Friend() { Name="Natalie", Age=20 }
  76. }
  77. }
  78. };
  79. XmlDocument xmlDoc = new XmlDocument();
  80. xmlDoc.LoadXml(Resources.Macros);
  81. globals.Add("xmlDoc", xmlDoc);
  82. // Globals types
  83. Dictionary<string, Type> globalsTypes = new Dictionary<string, Type>();
  84. foreach (var kw in globals)
  85. {
  86. globalsTypes.Add(kw.Key, kw.Value.GetType());
  87. }
  88. try
  89. {
  90. Stopwatch sw = new Stopwatch();
  91. // Basic test
  92. Console.WriteLine("Basic tests:");
  93. Console.WriteLine("=======================================");
  94. sw.Start();
  95. Console.WriteLine(new SharpTAL.Template("Hello ${w}!").Render(new Dictionary<string, object> { { "w", "world" } }));
  96. sw.Stop();
  97. Console.WriteLine(string.Format("{0} ms", sw.ElapsedMilliseconds));
  98. // Template program generator speed tests
  99. Console.WriteLine();
  100. Console.WriteLine("Template program generator speed tests:");
  101. Console.WriteLine("=======================================");
  102. sw.Reset();
  103. sw.Start();
  104. ProgramGenerator pageTemplateParser = new ProgramGenerator();
  105. for (int i = 0; i < 5; i++)
  106. {
  107. sw.Reset();
  108. sw.Start();
  109. TemplateInfo ti = new TemplateInfo
  110. {
  111. TemplateBody = Resources.Main,
  112. GlobalsTypes = globalsTypes,
  113. ReferencedAssemblies = refAssemblies
  114. };
  115. pageTemplateParser.GenerateTemplateProgram(ref ti);
  116. sw.Stop();
  117. Console.WriteLine(string.Format("{0}: {1} ms", i + 1, sw.ElapsedMilliseconds));
  118. }
  119. sw.Stop();
  120. // Template generation speed tests
  121. Console.WriteLine();
  122. Console.WriteLine("Template compilation speed tests:");
  123. Console.WriteLine("=================================");
  124. Template template = new Template(Resources.Main, globalsTypes, refAssemblies);
  125. for (int i = 0; i < 5; i++)
  126. {
  127. sw.Reset();
  128. sw.Start();
  129. template.Compile();
  130. sw.Stop();
  131. Console.WriteLine(string.Format("{0}: {1} ms", i + 1, sw.ElapsedMilliseconds));
  132. }
  133. // FS Template cache no. 1
  134. Console.WriteLine();
  135. Console.WriteLine("FS template cache no.1");
  136. Console.WriteLine("======================");
  137. string cacheFolder = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Template Cache");
  138. if (!Directory.Exists(cacheFolder))
  139. {
  140. Directory.CreateDirectory(cacheFolder);
  141. }
  142. FileSystemTemplateCache cache1 = new FileSystemTemplateCache(cacheFolder, true, @"Demo_{key}.dll");
  143. template.TemplateCache = cache1;
  144. string result = "";
  145. for (int i = 0; i < 5; i++)
  146. {
  147. sw.Reset();
  148. sw.Start();
  149. result = template.Render(globals);
  150. sw.Stop();
  151. Console.WriteLine(string.Format("{0}: {1} ms", i + 1, sw.ElapsedMilliseconds));
  152. }
  153. Console.WriteLine();
  154. // FS Template cache no. 2
  155. Console.WriteLine();
  156. Console.WriteLine("FS template cache no.2 (loading generated templates):");
  157. Console.WriteLine("=====================================================");
  158. FileSystemTemplateCache cache2 = new FileSystemTemplateCache(cacheFolder, false, @"Demo_{key}.dll");
  159. template.TemplateCache = cache2;
  160. for (int i = 0; i < 5; i++)
  161. {
  162. sw.Reset();
  163. sw.Start();
  164. template.Render(globals);
  165. sw.Stop();
  166. Console.WriteLine(string.Format("{0}: {1} ms", i + 1, sw.ElapsedMilliseconds));
  167. }
  168. // Memory Template cache
  169. Console.WriteLine();
  170. Console.WriteLine("Memory template cache:");
  171. Console.WriteLine("======================");
  172. MemoryTemplateCache cache3 = new MemoryTemplateCache();
  173. template.TemplateCache = cache3;
  174. for (int i = 0; i < 5; i++)
  175. {
  176. sw.Reset();
  177. sw.Start();
  178. template.Render(globals);
  179. sw.Stop();
  180. Console.WriteLine(string.Format("{0}: {1} ms", i + 1, sw.ElapsedMilliseconds));
  181. }
  182. Console.WriteLine();
  183. Console.WriteLine("Render result:");
  184. Console.WriteLine("==============");
  185. Console.WriteLine(result);
  186. }
  187. catch (TemplateParseException ex)
  188. {
  189. Console.WriteLine("");
  190. Console.WriteLine("-------------------------------");
  191. Console.WriteLine(ex.Message);
  192. Console.WriteLine("-------------------------------");
  193. }
  194. catch (CompileSourceException ex)
  195. {
  196. Console.WriteLine("");
  197. Console.WriteLine("-------------------------------");
  198. Console.WriteLine(ex.Message);
  199. Console.WriteLine("-------------------------------");
  200. }
  201. catch (RenderTemplateException ex)
  202. {
  203. Console.WriteLine("");
  204. Console.WriteLine("-------------------------------");
  205. Console.WriteLine(ex.Message);
  206. Console.WriteLine("-------------------------------");
  207. }
  208. catch (Exception ex)
  209. {
  210. Console.WriteLine("");
  211. Console.WriteLine("-------------------------------");
  212. Console.WriteLine(ex.Message);
  213. Console.WriteLine("-------------------------------");
  214. }
  215. Console.WriteLine("");
  216. Console.WriteLine("Press any key ...");
  217. Console.ReadKey();
  218. }
  219. }
  220. }