PageRenderTime 68ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/Alpha/Source/Libraries/Tests/GSF.SortedTreeStore.Test/Engine/ArchiveReaderSequential_Test.cs

#
C# | 528 lines | 444 code | 72 blank | 12 comment | 30 complexity | 9f4aaa2cc94168b9361bf820034f8452 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, EPL-1.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using GSF.IO.Unmanaged;
  7. using GSF.SortedTreeStore.Engine.Reader;
  8. using GSF.SortedTreeStore.Filters;
  9. using NUnit.Framework;
  10. using openHistorian;
  11. using GSF.SortedTreeStore.Storage;
  12. using openHistorian.Collections;
  13. using GSF.SortedTreeStore.Tree;
  14. using GSF.SortedTreeStore.Tree.TreeNodes;
  15. using openHistorian.Data.Query;
  16. using GSF.SortedTreeStore.Net.Compression;
  17. namespace GSF.SortedTreeStore.Engine
  18. {
  19. [TestFixture]
  20. public class ArchiveReaderSequential_Test
  21. {
  22. [Test]
  23. public void TestOneFile()
  24. {
  25. MemoryPoolTest.TestMemoryLeak();
  26. ArchiveList<HistorianKey, HistorianValue> list = new ArchiveList<HistorianKey, HistorianValue>();
  27. var master = CreateTable();
  28. var table1 = CreateTable();
  29. AddData(master, 100, 100, 100);
  30. AddData(table1, 100, 100, 100);
  31. using (var editor = list.AcquireEditLock())
  32. {
  33. editor.Add(table1, false);
  34. }
  35. using (var masterRead = master.BeginRead())
  36. {
  37. var masterScan = masterRead.GetTreeScanner();
  38. masterScan.SeekToStart();
  39. var masterScanSequential = masterScan.TestSequential();
  40. var sequencer = new SortedTreeEngineReaderSequential<HistorianKey, HistorianValue>(list);
  41. var scanner = sequencer.Read().TestSequential();
  42. int count = 0;
  43. while (scanner.Read())
  44. {
  45. count++;
  46. if (!masterScanSequential.Read())
  47. throw new Exception();
  48. if (!scanner.CurrentKey.IsEqualTo(masterScan.CurrentKey))
  49. throw new Exception();
  50. if (!scanner.CurrentValue.IsEqualTo(masterScan.CurrentValue))
  51. throw new Exception();
  52. }
  53. if (masterScan.Read())
  54. throw new Exception();
  55. }
  56. list.Dispose();
  57. master.Dispose();
  58. MemoryPoolTest.TestMemoryLeak();
  59. }
  60. [Test]
  61. public void TestTwoFiles()
  62. {
  63. MemoryPoolTest.TestMemoryLeak();
  64. ArchiveList<HistorianKey, HistorianValue> list = new ArchiveList<HistorianKey, HistorianValue>();
  65. var master = CreateTable();
  66. var table1 = CreateTable();
  67. var table2 = CreateTable();
  68. AddData(master, 100, 100, 100);
  69. AddData(table1, 100, 100, 100);
  70. AddData(master, 101, 100, 100);
  71. AddData(table2, 101, 100, 100);
  72. using (var editor = list.AcquireEditLock())
  73. {
  74. editor.Add(table1, false);
  75. editor.Add(table2, false);
  76. }
  77. using (var masterRead = master.BeginRead())
  78. {
  79. var masterScan = masterRead.GetTreeScanner();
  80. masterScan.SeekToStart();
  81. var masterScanSequential = masterScan.TestSequential();
  82. var sequencer = new SortedTreeEngineReaderSequential<HistorianKey, HistorianValue>(list);
  83. var scanner = sequencer.Read().TestSequential();
  84. while (scanner.Read())
  85. {
  86. if (!masterScanSequential.Read())
  87. throw new Exception();
  88. if (!scanner.CurrentKey.IsEqualTo(masterScan.CurrentKey))
  89. throw new Exception();
  90. if (!scanner.CurrentValue.IsEqualTo(masterScan.CurrentValue))
  91. throw new Exception();
  92. }
  93. if (masterScan.Read())
  94. throw new Exception();
  95. }
  96. master.Dispose();
  97. list.Dispose();
  98. MemoryPoolTest.TestMemoryLeak();
  99. }
  100. [Test]
  101. public void TestTwoIdenticalFiles()
  102. {
  103. MemoryPoolTest.TestMemoryLeak();
  104. ArchiveList<HistorianKey, HistorianValue> list = new ArchiveList<HistorianKey, HistorianValue>();
  105. var master = CreateTable();
  106. var table1 = CreateTable();
  107. var table2 = CreateTable();
  108. AddData(master, 100, 100, 100);
  109. AddData(table1, 100, 100, 100);
  110. AddData(table2, 100, 100, 100);
  111. using (var editor = list.AcquireEditLock())
  112. {
  113. editor.Add(table1, false);
  114. editor.Add(table2, false);
  115. }
  116. using (var masterRead = master.BeginRead())
  117. {
  118. var masterScan = masterRead.GetTreeScanner();
  119. masterScan.SeekToStart();
  120. var masterScanSequential = masterScan.TestSequential();
  121. var sequencer = new SortedTreeEngineReaderSequential<HistorianKey, HistorianValue>(list);
  122. var scanner = sequencer.Read().TestSequential();
  123. while (scanner.Read())
  124. {
  125. if (!masterScanSequential.Read())
  126. throw new Exception();
  127. if (!scanner.CurrentKey.IsEqualTo(masterScan.CurrentKey))
  128. throw new Exception();
  129. if (!scanner.CurrentValue.IsEqualTo(masterScan.CurrentValue))
  130. throw new Exception();
  131. }
  132. if (masterScan.Read())
  133. throw new Exception();
  134. }
  135. list.Dispose();
  136. master.Dispose();
  137. MemoryPoolTest.TestMemoryLeak();
  138. }
  139. SortedTreeTable<HistorianKey, HistorianValue> CreateTable()
  140. {
  141. var file = SortedTreeFile.CreateInMemory();
  142. var table = file.OpenOrCreateTable<HistorianKey, HistorianValue>(SortedTree.FixedSizeNode);
  143. return table;
  144. }
  145. void AddData(SortedTreeTable<HistorianKey, HistorianValue> table, ulong start, ulong step, ulong count)
  146. {
  147. using (var edit = table.BeginEdit())
  148. {
  149. var key = new HistorianKey();
  150. var value = new HistorianValue();
  151. for (ulong v = start; v < start + step * count; v += step)
  152. {
  153. key.SetMin();
  154. key.PointID = v;
  155. edit.AddPoint(key, value);
  156. }
  157. edit.Commit();
  158. }
  159. }
  160. void AddData(SortedTreeTable<HistorianKey, HistorianValue> table, DateTime startTime, TimeSpan stepTime, int countTime, ulong startPoint, ulong stepPoint, ulong countPoint)
  161. {
  162. using (var edit = table.BeginEdit())
  163. {
  164. var key = new HistorianKey();
  165. var value = new HistorianValue();
  166. key.SetMin();
  167. var stepTimeTicks = (ulong)stepTime.Ticks;
  168. var stopTime = (ulong)(startTime.Ticks + countTime * stepTime.Ticks);
  169. for (ulong t = (ulong)startTime.Ticks; t < stopTime; t += stepTimeTicks)
  170. {
  171. for (ulong v = startPoint; v < startPoint + stepPoint * countPoint; v += stepPoint)
  172. {
  173. key.Timestamp = t;
  174. key.PointID = v;
  175. edit.AddPoint(key, value);
  176. }
  177. }
  178. edit.Commit();
  179. }
  180. }
  181. void AddDataTerminal(SortedTreeTable<HistorianKey, HistorianValue> table, ulong pointID, DateTime startTime, TimeSpan stepTime, ulong startValue, ulong stepValue, int count)
  182. {
  183. using (var edit = table.BeginEdit())
  184. {
  185. var key = new HistorianKey();
  186. var value = new HistorianValue();
  187. key.SetMin();
  188. var t = (ulong)startTime.Ticks;
  189. var v = startValue;
  190. while (count > 0)
  191. {
  192. count--;
  193. key.Timestamp = t;
  194. key.PointID = pointID;
  195. value.Value1 = v;
  196. edit.AddPoint(key, value);
  197. t += (ulong)stepTime.Ticks;
  198. v += stepValue;
  199. }
  200. edit.Commit();
  201. }
  202. }
  203. [Test]
  204. public void BenchmarkRawFile()
  205. {
  206. MemoryPoolTest.TestMemoryLeak();
  207. const int Max = 1000000;
  208. var master = CreateTable();
  209. AddData(master, 100, 100, Max);
  210. DebugStopwatch sw = new DebugStopwatch();
  211. using (var masterRead = master.BeginRead())
  212. {
  213. double sec = sw.TimeEvent(() =>
  214. {
  215. var scanner = masterRead.GetTreeScanner();
  216. scanner.SeekToStart();
  217. while (scanner.Read())
  218. {
  219. }
  220. });
  221. Console.WriteLine(Max / sec / 1000000);
  222. }
  223. master.Dispose();
  224. MemoryPoolTest.TestMemoryLeak();
  225. }
  226. [Test]
  227. public void BenchmarkOneFile()
  228. {
  229. MemoryPoolTest.TestMemoryLeak();
  230. const int Max = 1000000;
  231. ArchiveList<HistorianKey, HistorianValue> list = new ArchiveList<HistorianKey, HistorianValue>();
  232. var table1 = CreateTable();
  233. AddData(table1, 100, 100, Max);
  234. using (var editor = list.AcquireEditLock())
  235. {
  236. editor.Add(table1, false);
  237. }
  238. var sequencer = new SortedTreeEngineReaderSequential<HistorianKey, HistorianValue>(list);
  239. DebugStopwatch sw = new DebugStopwatch();
  240. double sec = sw.TimeEvent(() =>
  241. {
  242. var scanner = sequencer.Read();
  243. while (scanner.Read())
  244. {
  245. }
  246. });
  247. Console.WriteLine(Max / sec / 1000000);
  248. table1.Dispose();
  249. MemoryPoolTest.TestMemoryLeak();
  250. }
  251. [Test]
  252. public void BenchmarkTwoFiles()
  253. {
  254. MemoryPoolTest.TestMemoryLeak();
  255. const int Max = 1000000;
  256. ArchiveList<HistorianKey, HistorianValue> list = new ArchiveList<HistorianKey, HistorianValue>();
  257. var table1 = CreateTable();
  258. var table2 = CreateTable();
  259. AddData(table1, 100, 100, Max / 2);
  260. AddData(table2, 101, 100, Max / 2);
  261. using (var editor = list.AcquireEditLock())
  262. {
  263. editor.Add(table1, false);
  264. editor.Add(table2, false);
  265. }
  266. var sequencer = new SortedTreeEngineReaderSequential<HistorianKey, HistorianValue>(list);
  267. DebugStopwatch sw = new DebugStopwatch();
  268. double sec = sw.TimeEvent(() =>
  269. {
  270. var scanner = sequencer.Read();
  271. while (scanner.Read())
  272. {
  273. }
  274. });
  275. Console.WriteLine(Max / sec / 1000000);
  276. list.Dispose();
  277. MemoryPoolTest.TestMemoryLeak();
  278. }
  279. [Test]
  280. public void BenchmarkThreeFiles()
  281. {
  282. MemoryPoolTest.TestMemoryLeak();
  283. const int Max = 1000000;
  284. ArchiveList<HistorianKey, HistorianValue> list = new ArchiveList<HistorianKey, HistorianValue>();
  285. var table1 = CreateTable();
  286. var table2 = CreateTable();
  287. var table3 = CreateTable();
  288. AddData(table1, 100, 100, Max / 3);
  289. AddData(table2, 101, 100, Max / 3);
  290. AddData(table3, 102, 100, Max / 3);
  291. using (var editor = list.AcquireEditLock())
  292. {
  293. editor.Add(table1, false);
  294. editor.Add(table2, false);
  295. editor.Add(table3, false);
  296. }
  297. var sequencer = new SortedTreeEngineReaderSequential<HistorianKey, HistorianValue>(list);
  298. DebugStopwatch sw = new DebugStopwatch();
  299. double sec = sw.TimeEvent(() =>
  300. {
  301. var scanner = sequencer.Read();
  302. while (scanner.Read())
  303. {
  304. }
  305. });
  306. Console.WriteLine(Max / sec / 1000000);
  307. list.Dispose();
  308. MemoryPoolTest.TestMemoryLeak();
  309. }
  310. [Test]
  311. public void BenchmarkRealisticSamples()
  312. {
  313. MemoryPoolTest.TestMemoryLeak();
  314. const int Max = 1000000;
  315. const int FileCount = 1000;
  316. ArchiveList<HistorianKey, HistorianValue> list = new ArchiveList<HistorianKey, HistorianValue>();
  317. DateTime start = DateTime.Now.Date;
  318. for (int x = 0; x < FileCount; x++)
  319. {
  320. var table1 = CreateTable();
  321. AddData(table1, start.AddMinutes(2 * x), new TimeSpan(TimeSpan.TicksPerSecond), 60, 100, 1, Max / 60 / FileCount);
  322. using (var editor = list.AcquireEditLock())
  323. {
  324. editor.Add(table1, false);
  325. }
  326. }
  327. var filter = TimestampFilter.CreateFromIntervalData<HistorianKey>(start, start.AddMinutes(2 * FileCount), new TimeSpan(TimeSpan.TicksPerSecond * 2), new TimeSpan(TimeSpan.TicksPerMillisecond));
  328. var sequencer = new SortedTreeEngineReaderSequential<HistorianKey, HistorianValue>(list);
  329. DebugStopwatch sw = new DebugStopwatch();
  330. int xi = 0;
  331. double sec = sw.TimeEvent(() =>
  332. {
  333. var scanner = sequencer.Read(filter);
  334. while (scanner.Read())
  335. {
  336. xi++;
  337. }
  338. });
  339. Console.WriteLine(Max / sec / 1000000);
  340. //TreeKeyMethodsBase<HistorianKey>.WriteToConsole();
  341. //TreeValueMethodsBase<HistorianValue>.WriteToConsole();
  342. //Console.WriteLine("KeyMethodsBase calls");
  343. //for (int x = 0; x < 23; x++)
  344. //{
  345. // Console.WriteLine(TreeKeyMethodsBase<HistorianKey>.CallMethods[x] + "\t" + ((TreeKeyMethodsBase<HistorianKey>.Method)(x)).ToString());
  346. //}
  347. //Console.WriteLine("ValueMethodsBase calls");
  348. //for (int x = 0; x < 5; x++)
  349. //{
  350. // Console.WriteLine(TreeValueMethodsBase<HistorianValue>.CallMethods[x] + "\t" + ((TreeValueMethodsBase<HistorianValue>.Method)(x)).ToString());
  351. //}
  352. list.Dispose();
  353. MemoryPoolTest.TestMemoryLeak();
  354. }
  355. [Test]
  356. public void ConsoleTest1()
  357. {
  358. MemoryPoolTest.TestMemoryLeak();
  359. ArchiveList<HistorianKey, HistorianValue> list = new ArchiveList<HistorianKey, HistorianValue>();
  360. DateTime start = DateTime.Now.Date;
  361. for (int x = 0; x < 3; x++)
  362. {
  363. var table1 = CreateTable();
  364. AddDataTerminal(table1, (ulong)x, start, new TimeSpan(TimeSpan.TicksPerSecond), (ulong)(1000 * x), 1, 60 * 60);
  365. using (var editor = list.AcquireEditLock())
  366. {
  367. editor.Add(table1, false);
  368. }
  369. }
  370. var filter = TimestampFilter.CreateFromIntervalData<HistorianKey>(start, start.AddMinutes(10), new TimeSpan(TimeSpan.TicksPerSecond * 1), new TimeSpan(TimeSpan.TicksPerMillisecond));
  371. var sequencer = new SortedTreeEngineReaderSequential<HistorianKey, HistorianValue>(list);
  372. var frames = sequencer.Read(filter).GetFrames();
  373. WriteToConsole(frames);
  374. list.Dispose();
  375. MemoryPoolTest.TestMemoryLeak();
  376. }
  377. [Test]
  378. public void ConsoleTest2()
  379. {
  380. MemoryPoolTest.TestMemoryLeak();
  381. ArchiveList<HistorianKey, HistorianValue> list = new ArchiveList<HistorianKey, HistorianValue>();
  382. DateTime start = DateTime.Now.Date;
  383. for (int x = 0; x < 3; x++)
  384. {
  385. var table1 = CreateTable();
  386. AddDataTerminal(table1, (ulong)x, start, new TimeSpan(TimeSpan.TicksPerSecond), (ulong)(1000 * x), 1, 60 * 60);
  387. using (var editor = list.AcquireEditLock())
  388. {
  389. editor.Add(table1, false);
  390. }
  391. }
  392. var filter = TimestampFilter.CreateFromIntervalData<HistorianKey>(start.AddMinutes(-100), start.AddMinutes(10), new TimeSpan(TimeSpan.TicksPerSecond * 60), new TimeSpan(TimeSpan.TicksPerSecond));
  393. var sequencer = new SortedTreeEngineReaderSequential<HistorianKey, HistorianValue>(list);
  394. var frames = sequencer.Read(filter).GetFrames();
  395. WriteToConsole(frames);
  396. list.Dispose();
  397. MemoryPoolTest.TestMemoryLeak();
  398. }
  399. [Test]
  400. public void ConsoleTest3()
  401. {
  402. MemoryPoolTest.TestMemoryLeak();
  403. ArchiveList<HistorianKey, HistorianValue> list = new ArchiveList<HistorianKey, HistorianValue>();
  404. DateTime start = DateTime.Now.Date;
  405. for (int x = 0; x < 3; x++)
  406. {
  407. var table1 = CreateTable();
  408. AddDataTerminal(table1, (ulong)x, start, new TimeSpan(TimeSpan.TicksPerSecond), (ulong)(1000 * x), 1, 60 * 60);
  409. using (var editor = list.AcquireEditLock())
  410. {
  411. editor.Add(table1, false);
  412. }
  413. }
  414. for (int x = 0; x < 3; x++)
  415. {
  416. var table1 = CreateTable();
  417. AddDataTerminal(table1, (ulong)x, start, new TimeSpan(TimeSpan.TicksPerSecond), (ulong)(1000 * x), 1, 60 * 60);
  418. using (var editor = list.AcquireEditLock())
  419. {
  420. editor.Add(table1, false);
  421. }
  422. }
  423. var filter = TimestampFilter.CreateFromIntervalData<HistorianKey>(start, start.AddMinutes(10), new TimeSpan(TimeSpan.TicksPerSecond * 60), new TimeSpan(TimeSpan.TicksPerSecond));
  424. var sequencer = new SortedTreeEngineReaderSequential<HistorianKey, HistorianValue>(list);
  425. var frames = sequencer.Read(filter).GetFrames();
  426. WriteToConsole(frames);
  427. list.Dispose();
  428. MemoryPoolTest.TestMemoryLeak();
  429. }
  430. void WriteToConsole(SortedList<DateTime, FrameData> frames)
  431. {
  432. StringBuilder sb = new StringBuilder();
  433. ulong?[] data = new ulong?[10];
  434. foreach (var frame in frames)
  435. {
  436. Array.Clear(data, 0, data.Length);
  437. foreach (var sample in frame.Value.Points)
  438. {
  439. data[sample.Key] = sample.Value.Value1;
  440. }
  441. sb.Append(frame.Key.ToString());
  442. sb.Append('\t');
  443. foreach (var value in data)
  444. {
  445. if (value.HasValue)
  446. {
  447. sb.Append(value.Value);
  448. }
  449. sb.Append('\t');
  450. }
  451. Console.WriteLine(sb.ToString());
  452. sb.Clear();
  453. }
  454. }
  455. }
  456. }