/Xtensive.Indexing/Xtensive.Indexing.Tests/Index/PerformanceTest.cs

https://code.google.com/p/dataobjectsdotnet/ · C# · 311 lines · 275 code · 31 blank · 5 comment · 16 complexity · 48a0614416c9176fb835e31799c2bf80 MD5 · raw file

  1. // Copyright (C) 2003-2010 Xtensive LLC.
  2. // All rights reserved.
  3. // For conditions of distribution and use, see license.
  4. // Created by: Alexey Kochetov
  5. // Created: 2008.06.05
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using NUnit.Framework;
  10. using Xtensive.Core.Comparison;
  11. using Xtensive.Core.Diagnostics;
  12. using Xtensive.Core.Testing;
  13. using Xtensive.Core.Tuples;
  14. using Xtensive.Core.Tuples.Transform;
  15. using Xtensive.Indexing;
  16. namespace Xtensive.Indexing.Tests.Index
  17. {
  18. public class PerformanceTest
  19. {
  20. private const int ItemsCount = 100000;
  21. [TestFixture]
  22. public abstract class IndexProfiling<TKey,TItem>
  23. {
  24. protected List<TItem> list;
  25. protected List<TItem> orderedList;
  26. protected Index<TKey, TItem> index;
  27. protected Index<TKey, TItem> readOnlyIndex;
  28. protected Converter<TItem, TKey> keyExtractor;
  29. [TestFixtureSetUp]
  30. public abstract void SetUp();
  31. [Test]
  32. [Explicit]
  33. [Category("Profile")]
  34. public void DictionaryAddTest()
  35. {
  36. var dictionary = new SortedDictionary<TKey, TItem>();
  37. TestHelper.CollectGarbage();
  38. using (new Measurement("Dictionary.Add", MeasurementOptions.Log, ItemsCount))
  39. foreach (TItem item in list)
  40. dictionary.Add(keyExtractor(item), item);
  41. }
  42. [Test]
  43. [Explicit]
  44. [Category("Profile")]
  45. public void AddTest()
  46. {
  47. index.Clear();
  48. TestHelper.CollectGarbage();
  49. using (new Measurement("Add", MeasurementOptions.Log, ItemsCount))
  50. foreach (TItem item in list)
  51. index.Add(item);
  52. }
  53. [Test]
  54. [Explicit]
  55. [Category("Profile")]
  56. public void SeekTest()
  57. {
  58. TestHelper.CollectGarbage();
  59. using (new Measurement("Seek", MeasurementOptions.Log, ItemsCount))
  60. foreach (TItem item in list)
  61. {
  62. var key = keyExtractor(item);
  63. SeekResult<TItem> result = readOnlyIndex.Seek(new Ray<Entire<TKey>>(new Entire<TKey>(key)));
  64. }
  65. }
  66. [Test]
  67. [Explicit]
  68. [Category("Profile")]
  69. public void EnumerateTest()
  70. {
  71. TestHelper.CollectGarbage();
  72. using (new Measurement("Enumerator.MoveNext+Current", MeasurementOptions.Log, ItemsCount))
  73. foreach (TItem i in readOnlyIndex)
  74. {
  75. }
  76. }
  77. [Test]
  78. [Explicit]
  79. [Category("Profile")]
  80. public void GetItemsTest()
  81. {
  82. TestHelper.CollectGarbage();
  83. int count = ItemsCount;
  84. using (new Measurement("GetItems", MeasurementOptions.Log, count))
  85. for (int j = 0; j < count; j++)
  86. {
  87. var from = keyExtractor(orderedList[j]);
  88. var to = keyExtractor(orderedList[count - 1]);
  89. readOnlyIndex.GetItems(new Range<Entire<TKey>>(
  90. new Entire<TKey>(from),
  91. new Entire<TKey>(to)));
  92. }
  93. }
  94. [Test]
  95. [Explicit]
  96. [Category("Profile")]
  97. public void GetFirstItemTest()
  98. {
  99. TestHelper.CollectGarbage();
  100. int count = ItemsCount;
  101. using (new Measurement("GetItems+First", MeasurementOptions.Log, count))
  102. for (int j = 0; j < count; j++)
  103. {
  104. var from = keyExtractor(orderedList[j]);
  105. var to = keyExtractor(orderedList[count - 1]);
  106. TItem first = readOnlyIndex.GetItems(new Range<Entire<TKey>>(
  107. new Entire<TKey>(from),
  108. new Entire<TKey>(to))).First();
  109. }
  110. }
  111. [Test]
  112. [Explicit]
  113. [Category("Profile")]
  114. public void EnumerateGetItemsTest()
  115. {
  116. TestHelper.CollectGarbage();
  117. int count = ItemsCount;
  118. using (new Measurement("GetItems,Enumerator.MoveNext+Current", MeasurementOptions.Log, count))
  119. foreach (TItem i in readOnlyIndex.GetItems(new Range<Entire<TKey>>(
  120. new Entire<TKey>(InfinityType.Negative),
  121. new Entire<TKey>(InfinityType.Positive))))
  122. {
  123. }
  124. }
  125. [Test]
  126. [Explicit]
  127. [Category("Profile")]
  128. public void ContainsTest()
  129. {
  130. TestHelper.CollectGarbage();
  131. using (new Measurement("Contains", MeasurementOptions.Log, ItemsCount))
  132. foreach (TItem item in list)
  133. Assert.IsTrue(readOnlyIndex.Contains(item));
  134. }
  135. [Test]
  136. [Explicit]
  137. [Category("Profile")]
  138. public void RemoveTest()
  139. {
  140. if (index.Count != ItemsCount) {
  141. index.Clear();
  142. Assert.AreEqual(0, index.Count);
  143. AddTest();
  144. }
  145. TestHelper.CollectGarbage();
  146. using (new Measurement("Remove", MeasurementOptions.Log, ItemsCount))
  147. foreach (TItem item in list)
  148. Assert.IsTrue(index.Remove(item));
  149. Assert.AreEqual(0, index.Count);
  150. }
  151. }
  152. [TestFixture]
  153. public class Int32Int32Profiling : IndexProfiling<int, int>
  154. {
  155. [TestFixtureSetUp]
  156. public override void SetUp()
  157. {
  158. var dictionary = new SortedDictionary<int, int>();
  159. list = new List<int>(ItemsCount);
  160. var random = RandomManager.CreateRandom(SeedVariatorType.CallingMethod);
  161. IEnumerator<int> generator = InstanceGenerationUtils<int>.GetInstances(random, 0).GetEnumerator();
  162. int count = 0;
  163. while (count < ItemsCount && generator.MoveNext()) {
  164. int i = generator.Current;
  165. if (!dictionary.ContainsKey(i)) {
  166. dictionary.Add(i, i);
  167. list.Add(i);
  168. count++;
  169. }
  170. }
  171. keyExtractor = input => input;
  172. orderedList = new List<int>(list);
  173. orderedList.Sort();
  174. var configuration = new IndexConfiguration<int, int> {KeyExtractor = keyExtractor, KeyComparer = AdvancedComparer<int>.Default};
  175. index = new Index<int, int>(configuration);
  176. AddTest();
  177. readOnlyIndex = index;
  178. index = new Index<int, int>(configuration);
  179. }
  180. }
  181. [TestFixture]
  182. public class TupleTupleProfiling : IndexProfiling<Tuple,Tuple>
  183. {
  184. [TestFixtureSetUp]
  185. public override void SetUp()
  186. {
  187. var dictionary = new SortedDictionary<int, int>();
  188. var descriptor = TupleDescriptor.Create(new[] {typeof (int), typeof (int)});
  189. list = new List<Tuple>(ItemsCount);
  190. AdvancedComparer<Tuple> comparer = AdvancedComparer<Tuple>.Default.ApplyRules(
  191. new ComparisonRules(ComparisonRule.Positive, new[]{ComparisonRules.Positive}, ComparisonRules.None));
  192. var random = RandomManager.CreateRandom(SeedVariatorType.CallingMethod);
  193. IEnumerator<int> generator = InstanceGenerationUtils<int>.GetInstances(random, 0).GetEnumerator();
  194. int count = 0;
  195. while (count < ItemsCount && generator.MoveNext())
  196. {
  197. int i = generator.Current;
  198. if (!dictionary.ContainsKey(i)) {
  199. dictionary.Add(i, i);
  200. list.Add(Tuple.Create(descriptor, i, i));
  201. count++;
  202. }
  203. }
  204. keyExtractor = input => input;
  205. orderedList = new List<Tuple>(list);
  206. orderedList.Sort(comparer.Implementation);
  207. var configuration = new IndexConfiguration<Tuple,Tuple> {KeyExtractor = keyExtractor, KeyComparer = comparer};
  208. index = new Index<Tuple, Tuple>(configuration);
  209. AddTest();
  210. readOnlyIndex = index;
  211. index = new Index<Tuple, Tuple>(configuration);
  212. }
  213. }
  214. [TestFixture]
  215. public class TupleTupleProfilingWithExtractor : IndexProfiling<Tuple,Tuple>
  216. {
  217. [TestFixtureSetUp]
  218. public override void SetUp()
  219. {
  220. var dictionary = new SortedDictionary<int, int>();
  221. var descriptor = TupleDescriptor.Create(new[] {typeof (int), typeof (int)});
  222. list = new List<Tuple>(ItemsCount);
  223. AdvancedComparer<Tuple> comparer = AdvancedComparer<Tuple>.Default;
  224. var random = RandomManager.CreateRandom(SeedVariatorType.CallingMethod);
  225. IEnumerator<int> generator = InstanceGenerationUtils<int>.GetInstances(random, 0).GetEnumerator();
  226. int count = 0;
  227. while (count < ItemsCount && generator.MoveNext()) {
  228. int i = generator.Current;
  229. if (!dictionary.ContainsKey(i)) {
  230. dictionary.Add(i, i);
  231. list.Add(Tuple.Create(descriptor, i, i));
  232. count++;
  233. }
  234. }
  235. TupleDescriptor keyDescriptor = TupleDescriptor.Create(new [] {typeof(int)});
  236. var transform = new MapTransform(true, keyDescriptor, new[] {0});
  237. keyExtractor = input => transform.Apply(TupleTransformType.TransformedTuple, input);
  238. orderedList = new List<Tuple>(list);
  239. orderedList.Sort(comparer.Implementation);
  240. var configuration = new IndexConfiguration<Tuple, Tuple> {KeyExtractor = keyExtractor, KeyComparer = comparer};
  241. index = new Index<Tuple, Tuple>(configuration);
  242. AddTest();
  243. readOnlyIndex = index;
  244. index = new Index<Tuple, Tuple>(configuration);
  245. }
  246. }
  247. [TestFixture]
  248. public class Int32TupleProfilingWithExtractor : IndexProfiling<int, Tuple>
  249. {
  250. [TestFixtureSetUp]
  251. public override void SetUp()
  252. {
  253. var dictionary = new SortedDictionary<int, int>();
  254. var descriptor = TupleDescriptor.Create(new[] { typeof(int), typeof(int) });
  255. list = new List<Tuple>(ItemsCount);
  256. AdvancedComparer<int> comparer = AdvancedComparer<int>.Default;
  257. var random = RandomManager.CreateRandom(SeedVariatorType.CallingMethod);
  258. IEnumerator<int> generator = InstanceGenerationUtils<int>.GetInstances(random, 0).GetEnumerator();
  259. int count = 0;
  260. while (count < ItemsCount && generator.MoveNext())
  261. {
  262. int i = generator.Current;
  263. if (!dictionary.ContainsKey(i))
  264. {
  265. dictionary.Add(i, i);
  266. list.Add(Tuple.Create(descriptor, i, i));
  267. count++;
  268. }
  269. }
  270. keyExtractor = input => input.GetValueOrDefault<int>(0);
  271. orderedList = list.OrderBy(input => input.GetValueOrDefault<int>(0), comparer.Implementation).ToList();
  272. var configuration = new IndexConfiguration<int, Tuple> { KeyExtractor = keyExtractor, KeyComparer = comparer };
  273. index = new Index<int, Tuple>(configuration);
  274. AddTest();
  275. readOnlyIndex = index;
  276. index = new Index<int, Tuple>(configuration);
  277. }
  278. }
  279. }
  280. }