1
0

ScriptPerformanceTests.cs 6.4 KB

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