NPCPerformanceTests.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Diagnostics;
  30. using System.Reflection;
  31. using log4net;
  32. using Nini.Config;
  33. using NUnit.Framework;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Communications;
  37. using OpenSim.Region.CoreModules.Avatar.Attachments;
  38. using OpenSim.Region.CoreModules.Avatar.AvatarFactory;
  39. using OpenSim.Region.CoreModules.Framework.InventoryAccess;
  40. using OpenSim.Region.CoreModules.Framework.UserManagement;
  41. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Avatar;
  42. using OpenSim.Region.Framework.Interfaces;
  43. using OpenSim.Region.Framework.Scenes;
  44. using OpenSim.Region.OptionalModules.World.NPC;
  45. using OpenSim.Services.AvatarService;
  46. using OpenSim.Tests.Common;
  47. using OpenSim.Tests.Common.Mock;
  48. namespace OpenSim.Tests.Performance
  49. {
  50. /// <summary>
  51. /// NPC performance tests
  52. /// </summary>
  53. /// <remarks>
  54. /// Don't rely on the numbers given by these tests - they will vary a lot depending on what is already cached,
  55. /// how much memory is free, etc. In some cases, later larger tests will apparently take less time than smaller
  56. /// earlier tests.
  57. /// </remarks>
  58. [TestFixture]
  59. public class NPCPerformanceTests : OpenSimTestCase
  60. {
  61. private TestScene scene;
  62. private AvatarFactoryModule afm;
  63. private UserManagementModule umm;
  64. private AttachmentsModule am;
  65. [TestFixtureSetUp]
  66. public void FixtureInit()
  67. {
  68. // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
  69. Util.FireAndForgetMethod = FireAndForgetMethod.None;
  70. }
  71. [TestFixtureTearDown]
  72. public void TearDown()
  73. {
  74. scene.Close();
  75. scene = null;
  76. GC.Collect();
  77. GC.WaitForPendingFinalizers();
  78. // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
  79. // threads. Possibly, later tests should be rewritten not to worry about such things.
  80. Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
  81. }
  82. [SetUp]
  83. public void Init()
  84. {
  85. IConfigSource config = new IniConfigSource();
  86. config.AddConfig("NPC");
  87. config.Configs["NPC"].Set("Enabled", "true");
  88. config.AddConfig("Modules");
  89. config.Configs["Modules"].Set("InventoryAccessModule", "BasicInventoryAccessModule");
  90. afm = new AvatarFactoryModule();
  91. umm = new UserManagementModule();
  92. am = new AttachmentsModule();
  93. scene = new SceneHelpers().SetupScene();
  94. SceneHelpers.SetupSceneModules(scene, config, afm, umm, am, new BasicInventoryAccessModule(), new NPCModule());
  95. }
  96. [Test]
  97. public void Test_0001_AddRemove100NPCs()
  98. {
  99. TestHelpers.InMethod();
  100. // log4net.Config.XmlConfigurator.Configure();
  101. TestAddRemoveNPCs(100);
  102. }
  103. [Test]
  104. public void Test_0002_AddRemove1000NPCs()
  105. {
  106. TestHelpers.InMethod();
  107. // log4net.Config.XmlConfigurator.Configure();
  108. TestAddRemoveNPCs(1000);
  109. }
  110. [Test]
  111. public void Test_0003_AddRemove2000NPCs()
  112. {
  113. TestHelpers.InMethod();
  114. // log4net.Config.XmlConfigurator.Configure();
  115. TestAddRemoveNPCs(2000);
  116. }
  117. private void TestAddRemoveNPCs(int numberOfNpcs)
  118. {
  119. ScenePresence sp = SceneHelpers.AddScenePresence(scene, TestHelpers.ParseTail(0x1));
  120. // ScenePresence originalAvatar = scene.GetScenePresence(originalClient.AgentId);
  121. // 8 is the index of the first baked texture in AvatarAppearance
  122. UUID originalFace8TextureId = TestHelpers.ParseTail(0x10);
  123. Primitive.TextureEntry originalTe = new Primitive.TextureEntry(UUID.Zero);
  124. Primitive.TextureEntryFace originalTef = originalTe.CreateFace(8);
  125. originalTef.TextureID = originalFace8TextureId;
  126. // We also need to add the texture to the asset service, otherwise the AvatarFactoryModule will tell
  127. // ScenePresence.SendInitialData() to reset our entire appearance.
  128. scene.AssetService.Store(AssetHelpers.CreateNotecardAsset(originalFace8TextureId));
  129. afm.SetAppearance(sp, originalTe, null);
  130. INPCModule npcModule = scene.RequestModuleInterface<INPCModule>();
  131. List<UUID> npcs = new List<UUID>();
  132. long startGcMemory = GC.GetTotalMemory(true);
  133. Stopwatch sw = new Stopwatch();
  134. sw.Start();
  135. for (int i = 0; i < numberOfNpcs; i++)
  136. {
  137. npcs.Add(
  138. npcModule.CreateNPC("John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, scene, sp.Appearance));
  139. }
  140. for (int i = 0; i < numberOfNpcs; i++)
  141. {
  142. Assert.That(npcs[i], Is.Not.Null);
  143. ScenePresence npc = scene.GetScenePresence(npcs[i]);
  144. Assert.That(npc, Is.Not.Null);
  145. }
  146. for (int i = 0; i < numberOfNpcs; i++)
  147. {
  148. Assert.That(npcModule.DeleteNPC(npcs[i], scene), Is.True);
  149. ScenePresence npc = scene.GetScenePresence(npcs[i]);
  150. Assert.That(npc, Is.Null);
  151. }
  152. sw.Stop();
  153. long endGcMemory = GC.GetTotalMemory(true);
  154. Console.WriteLine("Took {0} ms", sw.ElapsedMilliseconds);
  155. Console.WriteLine(
  156. "End {0} MB, Start {1} MB, Diff {2} MB",
  157. endGcMemory / 1024 / 1024,
  158. startGcMemory / 1024 / 1024,
  159. (endGcMemory - startGcMemory) / 1024 / 1024);
  160. }
  161. }
  162. }