1
0

ScriptPerformanceTests.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 System.Threading;
  32. using log4net;
  33. using Nini.Config;
  34. using NUnit.Framework;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.CoreModules.Scripting.WorldComm;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. using OpenSim.Region.ScriptEngine.XEngine;
  41. using OpenSim.Tests.Common;
  42. using OpenSim.Tests.Common.Mock;
  43. namespace OpenSim.Tests.Performance
  44. {
  45. /// <summary>
  46. /// Script performance tests
  47. /// </summary>
  48. /// <remarks>
  49. /// Don't rely on the numbers given by these tests - they will vary a lot depending on what is already cached,
  50. /// how much memory is free, etc. In some cases, later larger tests will apparently take less time than smaller
  51. /// earlier tests.
  52. /// </remarks>
  53. [TestFixture]
  54. public class ScriptPerformanceTests : OpenSimTestCase
  55. {
  56. private TestScene m_scene;
  57. private XEngine m_xEngine;
  58. private AutoResetEvent m_chatEvent = new AutoResetEvent(false);
  59. private int m_expectedChatMessages;
  60. private List<OSChatMessage> m_osChatMessagesReceived = new List<OSChatMessage>();
  61. [SetUp]
  62. public void Init()
  63. {
  64. //AppDomain.CurrentDomain.SetData("APPBASE", Environment.CurrentDirectory + "/bin");
  65. // Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
  66. m_xEngine = new XEngine();
  67. // Necessary to stop serialization complaining
  68. WorldCommModule wcModule = new WorldCommModule();
  69. IniConfigSource configSource = new IniConfigSource();
  70. IConfig startupConfig = configSource.AddConfig("Startup");
  71. startupConfig.Set("DefaultScriptEngine", "XEngine");
  72. IConfig xEngineConfig = configSource.AddConfig("XEngine");
  73. xEngineConfig.Set("Enabled", "true");
  74. // These tests will not run with AppDomainLoading = true, at least on mono. For unknown reasons, the call
  75. // to AssemblyResolver.OnAssemblyResolve fails.
  76. xEngineConfig.Set("AppDomainLoading", "false");
  77. m_scene = new SceneHelpers().SetupScene("My Test", UUID.Random(), 1000, 1000, configSource);
  78. SceneHelpers.SetupSceneModules(m_scene, configSource, m_xEngine, wcModule);
  79. m_scene.EventManager.OnChatFromWorld += OnChatFromWorld;
  80. m_scene.StartScripts();
  81. }
  82. [TearDown]
  83. public void TearDown()
  84. {
  85. m_scene.Close();
  86. m_scene = null;
  87. GC.Collect();
  88. GC.WaitForPendingFinalizers();
  89. }
  90. [Test]
  91. public void TestCompileAndStart100Scripts()
  92. {
  93. TestHelpers.InMethod();
  94. log4net.Config.XmlConfigurator.Configure();
  95. TestCompileAndStartScripts(100);
  96. }
  97. private void TestCompileAndStartScripts(int scriptsToCreate)
  98. {
  99. UUID userId = TestHelpers.ParseTail(0x1);
  100. m_expectedChatMessages = scriptsToCreate;
  101. int startingObjectIdTail = 0x100;
  102. GC.Collect();
  103. for (int idTail = startingObjectIdTail;idTail < startingObjectIdTail + scriptsToCreate; idTail++)
  104. {
  105. AddObjectAndScript(idTail, userId);
  106. }
  107. m_chatEvent.WaitOne(40000 + scriptsToCreate * 1000);
  108. Assert.That(m_osChatMessagesReceived.Count, Is.EqualTo(m_expectedChatMessages));
  109. foreach (OSChatMessage msg in m_osChatMessagesReceived)
  110. Assert.That(
  111. msg.Message,
  112. Is.EqualTo("Script running"),
  113. string.Format(
  114. "Message from {0} was {1} rather than {2}", msg.SenderUUID, msg.Message, "Script running"));
  115. }
  116. private void AddObjectAndScript(int objectIdTail, UUID userId)
  117. {
  118. // UUID itemId = TestHelpers.ParseTail(0x3);
  119. string itemName = string.Format("AddObjectAndScript() Item for object {0}", objectIdTail);
  120. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, userId, "AddObjectAndScriptPart_", objectIdTail);
  121. m_scene.AddNewSceneObject(so, true);
  122. InventoryItemBase itemTemplate = new InventoryItemBase();
  123. // itemTemplate.ID = itemId;
  124. itemTemplate.Name = itemName;
  125. itemTemplate.Folder = so.UUID;
  126. itemTemplate.InvType = (int)InventoryType.LSL;
  127. m_scene.RezNewScript(userId, itemTemplate);
  128. }
  129. private void OnChatFromWorld(object sender, OSChatMessage oscm)
  130. {
  131. // Console.WriteLine("Got chat [{0}]", oscm.Message);
  132. lock (m_osChatMessagesReceived)
  133. {
  134. m_osChatMessagesReceived.Add(oscm);
  135. if (m_osChatMessagesReceived.Count == m_expectedChatMessages)
  136. m_chatEvent.Set();
  137. }
  138. }
  139. }
  140. }