1
0

ThrottleTests.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 Nini.Config;
  29. using NUnit.Framework;
  30. using OpenMetaverse.Packets;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Framework.Scenes;
  33. using OpenSim.Tests.Common;
  34. namespace OpenSim.Region.ClientStack.LindenUDP.Tests
  35. {
  36. [TestFixture]
  37. public class ThrottleTests : OpenSimTestCase
  38. {
  39. [TestFixtureSetUp]
  40. public void FixtureInit()
  41. {
  42. // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
  43. Util.FireAndForgetMethod = FireAndForgetMethod.RegressionTest;
  44. }
  45. [TestFixtureTearDown]
  46. public void TearDown()
  47. {
  48. // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
  49. // threads. Possibly, later tests should be rewritten so none of them require async stuff (which regression
  50. // tests really shouldn't).
  51. Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
  52. }
  53. [Test]
  54. public void TestSetRequestDripRate()
  55. {
  56. TestHelpers.InMethod();
  57. TokenBucket tb = new TokenBucket("tb", null, 5000, 0);
  58. AssertRates(tb, 5000, 0, 5000, 0);
  59. tb.RequestedDripRate = 4000;
  60. AssertRates(tb, 4000, 0, 4000, 0);
  61. tb.RequestedDripRate = 6000;
  62. AssertRates(tb, 6000, 0, 6000, 0);
  63. }
  64. [Test]
  65. public void TestSetRequestDripRateWithMax()
  66. {
  67. TestHelpers.InMethod();
  68. TokenBucket tb = new TokenBucket("tb", null, 5000, 10000);
  69. AssertRates(tb, 5000, 0, 5000, 10000);
  70. tb.RequestedDripRate = 4000;
  71. AssertRates(tb, 4000, 0, 4000, 10000);
  72. tb.RequestedDripRate = 6000;
  73. AssertRates(tb, 6000, 0, 6000, 10000);
  74. tb.RequestedDripRate = 12000;
  75. AssertRates(tb, 10000, 0, 10000, 10000);
  76. }
  77. [Test]
  78. public void TestSetRequestDripRateWithChildren()
  79. {
  80. TestHelpers.InMethod();
  81. TokenBucket tbParent = new TokenBucket("tbParent", null, 0, 0);
  82. TokenBucket tbChild1 = new TokenBucket("tbChild1", tbParent, 3000, 0);
  83. TokenBucket tbChild2 = new TokenBucket("tbChild2", tbParent, 5000, 0);
  84. AssertRates(tbParent, 8000, 8000, 8000, 0);
  85. AssertRates(tbChild1, 3000, 0, 3000, 0);
  86. AssertRates(tbChild2, 5000, 0, 5000, 0);
  87. // Test: Setting a parent request greater than total children requests.
  88. tbParent.RequestedDripRate = 10000;
  89. AssertRates(tbParent, 10000, 8000, 8000, 0);
  90. AssertRates(tbChild1, 3000, 0, 3000, 0);
  91. AssertRates(tbChild2, 5000, 0, 5000, 0);
  92. // Test: Setting a parent request lower than total children requests.
  93. tbParent.RequestedDripRate = 6000;
  94. AssertRates(tbParent, 6000, 8000, 6000, 0);
  95. AssertRates(tbChild1, 3000, 0, 6000 / 8 * 3, 0);
  96. AssertRates(tbChild2, 5000, 0, 6000 / 8 * 5, 0);
  97. }
  98. private void AssertRates(
  99. TokenBucket tb, double requestedDripRate, double totalDripRequest, double dripRate, double maxDripRate)
  100. {
  101. Assert.AreEqual((int)requestedDripRate, tb.RequestedDripRate, "Requested drip rate");
  102. Assert.AreEqual((int)totalDripRequest, tb.TotalDripRequest, "Total drip request");
  103. Assert.AreEqual((int)dripRate, tb.DripRate, "Drip rate");
  104. Assert.AreEqual((int)maxDripRate, tb.MaxDripRate, "Max drip rate");
  105. }
  106. [Test]
  107. public void TestClientThrottleSetNoLimit()
  108. {
  109. TestHelpers.InMethod();
  110. // TestHelpers.EnableLogging();
  111. Scene scene = new SceneHelpers().SetupScene();
  112. TestLLUDPServer udpServer = ClientStackHelpers.AddUdpServer(scene);
  113. ScenePresence sp
  114. = ClientStackHelpers.AddChildClient(
  115. scene, udpServer, TestHelpers.ParseTail(0x1), TestHelpers.ParseTail(0x2), 123456);
  116. LLUDPClient udpClient = ((LLClientView)sp.ControllingClient).UDPClient;
  117. udpServer.Throttle.DebugLevel = 1;
  118. udpClient.ThrottleDebugLevel = 1;
  119. int resendBytes = 1000;
  120. int landBytes = 2000;
  121. int windBytes = 3000;
  122. int cloudBytes = 4000;
  123. int taskBytes = 5000;
  124. int textureBytes = 6000;
  125. int assetBytes = 7000;
  126. SetThrottles(
  127. udpClient, resendBytes, landBytes, windBytes, cloudBytes, taskBytes, textureBytes, assetBytes);
  128. // We expect this to be lower because of the minimum bound set by MTU
  129. int totalBytes = LLUDPServer.MTU + landBytes + windBytes + cloudBytes + taskBytes + textureBytes + assetBytes;
  130. AssertThrottles(
  131. udpClient,
  132. LLUDPServer.MTU, landBytes, windBytes, cloudBytes, taskBytes,
  133. textureBytes, assetBytes, totalBytes, 0, 0);
  134. }
  135. [Test]
  136. public void TestClientThrottleAdaptiveNoLimit()
  137. {
  138. TestHelpers.InMethod();
  139. // TestHelpers.EnableLogging();
  140. Scene scene = new SceneHelpers().SetupScene();
  141. IniConfigSource ics = new IniConfigSource();
  142. IConfig config = ics.AddConfig("ClientStack.LindenUDP");
  143. config.Set("enable_adaptive_throttles", true);
  144. config.Set("adaptive_throttle_min_bps", 32000);
  145. TestLLUDPServer udpServer = ClientStackHelpers.AddUdpServer(scene, ics);
  146. ScenePresence sp
  147. = ClientStackHelpers.AddChildClient(
  148. scene, udpServer, TestHelpers.ParseTail(0x1), TestHelpers.ParseTail(0x2), 123456);
  149. LLUDPClient udpClient = ((LLClientView)sp.ControllingClient).UDPClient;
  150. udpServer.Throttle.DebugLevel = 1;
  151. udpClient.ThrottleDebugLevel = 1;
  152. // Total is 275000
  153. int resendBytes = 5000; // this is set low to test the minimum throttle override
  154. int landBytes = 20000;
  155. int windBytes = 30000;
  156. int cloudBytes = 40000;
  157. int taskBytes = 50000;
  158. int textureBytes = 60000;
  159. int assetBytes = 70000;
  160. int totalBytes = resendBytes + landBytes + windBytes + cloudBytes + taskBytes + textureBytes + assetBytes;
  161. SetThrottles(
  162. udpClient, resendBytes, landBytes, windBytes, cloudBytes, taskBytes, textureBytes, assetBytes);
  163. // Ratio of current adaptive drip rate to requested bytes, minimum rate is 32000
  164. double commitRatio = 32000.0 / totalBytes;
  165. AssertThrottles(
  166. udpClient,
  167. LLUDPServer.MTU, landBytes * commitRatio, windBytes * commitRatio, cloudBytes * commitRatio, taskBytes * commitRatio,
  168. textureBytes * commitRatio, assetBytes * commitRatio, udpClient.FlowThrottle.AdjustedDripRate, totalBytes, 0);
  169. // Test an increase in target throttle, ack of 20 packets adds 20 * LLUDPServer.MTU bytes
  170. // to the throttle, recompute commitratio from those numbers
  171. udpClient.FlowThrottle.AcknowledgePackets(20);
  172. commitRatio = (32000.0 + 20.0 * LLUDPServer.MTU) / totalBytes;
  173. AssertThrottles(
  174. udpClient,
  175. LLUDPServer.MTU, landBytes * commitRatio, windBytes * commitRatio, cloudBytes * commitRatio, taskBytes * commitRatio,
  176. textureBytes * commitRatio, assetBytes * commitRatio, udpClient.FlowThrottle.AdjustedDripRate, totalBytes, 0);
  177. // Test a decrease in target throttle, adaptive throttle should cut the rate by 50% with a floor
  178. // set by the minimum adaptive rate
  179. udpClient.FlowThrottle.ExpirePackets(1);
  180. commitRatio = (32000.0 + (20.0 * LLUDPServer.MTU)/Math.Pow(2,1)) / totalBytes;
  181. AssertThrottles(
  182. udpClient,
  183. LLUDPServer.MTU, landBytes * commitRatio, windBytes * commitRatio, cloudBytes * commitRatio, taskBytes * commitRatio,
  184. textureBytes * commitRatio, assetBytes * commitRatio, udpClient.FlowThrottle.AdjustedDripRate, totalBytes, 0);
  185. }
  186. /// <summary>
  187. /// Test throttle setttings where max client throttle has been limited server side.
  188. /// </summary>
  189. [Test]
  190. public void TestSingleClientThrottleRegionLimited()
  191. {
  192. TestHelpers.InMethod();
  193. // TestHelpers.EnableLogging();
  194. int resendBytes = 6000;
  195. int landBytes = 8000;
  196. int windBytes = 10000;
  197. int cloudBytes = 12000;
  198. int taskBytes = 14000;
  199. int textureBytes = 16000;
  200. int assetBytes = 18000;
  201. int totalBytes
  202. = (int)((resendBytes + landBytes + windBytes + cloudBytes + taskBytes + textureBytes + assetBytes) / 2);
  203. Scene scene = new SceneHelpers().SetupScene();
  204. TestLLUDPServer udpServer = ClientStackHelpers.AddUdpServer(scene);
  205. udpServer.Throttle.RequestedDripRate = totalBytes;
  206. ScenePresence sp1
  207. = ClientStackHelpers.AddChildClient(
  208. scene, udpServer, TestHelpers.ParseTail(0x1), TestHelpers.ParseTail(0x2), 123456);
  209. LLUDPClient udpClient1 = ((LLClientView)sp1.ControllingClient).UDPClient;
  210. SetThrottles(
  211. udpClient1, resendBytes, landBytes, windBytes, cloudBytes, taskBytes, textureBytes, assetBytes);
  212. AssertThrottles(
  213. udpClient1,
  214. resendBytes / 2, landBytes / 2, windBytes / 2, cloudBytes / 2, taskBytes / 2,
  215. textureBytes / 2, assetBytes / 2, totalBytes, 0, 0);
  216. // Test: Now add another client
  217. ScenePresence sp2
  218. = ClientStackHelpers.AddChildClient(
  219. scene, udpServer, TestHelpers.ParseTail(0x10), TestHelpers.ParseTail(0x20), 123457);
  220. LLUDPClient udpClient2 = ((LLClientView)sp2.ControllingClient).UDPClient;
  221. // udpClient.ThrottleDebugLevel = 1;
  222. SetThrottles(
  223. udpClient2, resendBytes, landBytes, windBytes, cloudBytes, taskBytes, textureBytes, assetBytes);
  224. AssertThrottles(
  225. udpClient1,
  226. resendBytes / 4, landBytes / 4, windBytes / 4, cloudBytes / 4, taskBytes / 4,
  227. textureBytes / 4, assetBytes / 4, totalBytes / 2, 0, 0);
  228. AssertThrottles(
  229. udpClient2,
  230. resendBytes / 4, landBytes / 4, windBytes / 4, cloudBytes / 4, taskBytes / 4,
  231. textureBytes / 4, assetBytes / 4, totalBytes / 2, 0, 0);
  232. }
  233. /// <summary>
  234. /// Test throttle setttings where max client throttle has been limited server side.
  235. /// </summary>
  236. [Test]
  237. public void TestClientThrottlePerClientLimited()
  238. {
  239. TestHelpers.InMethod();
  240. // TestHelpers.EnableLogging();
  241. int resendBytes = 4000;
  242. int landBytes = 6000;
  243. int windBytes = 8000;
  244. int cloudBytes = 10000;
  245. int taskBytes = 12000;
  246. int textureBytes = 14000;
  247. int assetBytes = 16000;
  248. int totalBytes
  249. = (int)((resendBytes + landBytes + windBytes + cloudBytes + taskBytes + textureBytes + assetBytes) / 2);
  250. Scene scene = new SceneHelpers().SetupScene();
  251. TestLLUDPServer udpServer = ClientStackHelpers.AddUdpServer(scene);
  252. udpServer.ThrottleRates.Total = totalBytes;
  253. ScenePresence sp
  254. = ClientStackHelpers.AddChildClient(
  255. scene, udpServer, TestHelpers.ParseTail(0x1), TestHelpers.ParseTail(0x2), 123456);
  256. LLUDPClient udpClient = ((LLClientView)sp.ControllingClient).UDPClient;
  257. // udpClient.ThrottleDebugLevel = 1;
  258. SetThrottles(
  259. udpClient, resendBytes, landBytes, windBytes, cloudBytes, taskBytes, textureBytes, assetBytes);
  260. AssertThrottles(
  261. udpClient,
  262. resendBytes / 2, landBytes / 2, windBytes / 2, cloudBytes / 2, taskBytes / 2,
  263. textureBytes / 2, assetBytes / 2, totalBytes, 0, totalBytes);
  264. }
  265. [Test]
  266. public void TestClientThrottlePerClientAndRegionLimited()
  267. {
  268. TestHelpers.InMethod();
  269. //TestHelpers.EnableLogging();
  270. int resendBytes = 4000;
  271. int landBytes = 6000;
  272. int windBytes = 8000;
  273. int cloudBytes = 10000;
  274. int taskBytes = 12000;
  275. int textureBytes = 14000;
  276. int assetBytes = 16000;
  277. // current total 70000
  278. int totalBytes = resendBytes + landBytes + windBytes + cloudBytes + taskBytes + textureBytes + assetBytes;
  279. Scene scene = new SceneHelpers().SetupScene();
  280. TestLLUDPServer udpServer = ClientStackHelpers.AddUdpServer(scene);
  281. udpServer.ThrottleRates.Total = (int)(totalBytes * 1.1);
  282. udpServer.Throttle.RequestedDripRate = (int)(totalBytes * 1.5);
  283. ScenePresence sp1
  284. = ClientStackHelpers.AddChildClient(
  285. scene, udpServer, TestHelpers.ParseTail(0x1), TestHelpers.ParseTail(0x2), 123456);
  286. LLUDPClient udpClient1 = ((LLClientView)sp1.ControllingClient).UDPClient;
  287. udpClient1.ThrottleDebugLevel = 1;
  288. SetThrottles(
  289. udpClient1, resendBytes, landBytes, windBytes, cloudBytes, taskBytes, textureBytes, assetBytes);
  290. AssertThrottles(
  291. udpClient1,
  292. resendBytes, landBytes, windBytes, cloudBytes, taskBytes,
  293. textureBytes, assetBytes, totalBytes, 0, totalBytes * 1.1);
  294. // Now add another client
  295. ScenePresence sp2
  296. = ClientStackHelpers.AddChildClient(
  297. scene, udpServer, TestHelpers.ParseTail(0x10), TestHelpers.ParseTail(0x20), 123457);
  298. LLUDPClient udpClient2 = ((LLClientView)sp2.ControllingClient).UDPClient;
  299. udpClient2.ThrottleDebugLevel = 1;
  300. SetThrottles(
  301. udpClient2, resendBytes, landBytes, windBytes, cloudBytes, taskBytes, textureBytes, assetBytes);
  302. AssertThrottles(
  303. udpClient1,
  304. resendBytes * 0.75, landBytes * 0.75, windBytes * 0.75, cloudBytes * 0.75, taskBytes * 0.75,
  305. textureBytes * 0.75, assetBytes * 0.75, totalBytes * 0.75, 0, totalBytes * 1.1);
  306. AssertThrottles(
  307. udpClient2,
  308. resendBytes * 0.75, landBytes * 0.75, windBytes * 0.75, cloudBytes * 0.75, taskBytes * 0.75,
  309. textureBytes * 0.75, assetBytes * 0.75, totalBytes * 0.75, 0, totalBytes * 1.1);
  310. }
  311. private void AssertThrottles(
  312. LLUDPClient udpClient,
  313. double resendBytes, double landBytes, double windBytes, double cloudBytes, double taskBytes, double textureBytes, double assetBytes,
  314. double totalBytes, double targetBytes, double maxBytes)
  315. {
  316. ClientInfo ci = udpClient.GetClientInfo();
  317. // Console.WriteLine(
  318. // "Resend={0}, Land={1}, Wind={2}, Cloud={3}, Task={4}, Texture={5}, Asset={6}, TOTAL = {7}",
  319. // ci.resendThrottle, ci.landThrottle, ci.windThrottle, ci.cloudThrottle, ci.taskThrottle, ci.textureThrottle, ci.assetThrottle, ci.totalThrottle);
  320. Assert.AreEqual((int)resendBytes, ci.resendThrottle, "Resend");
  321. Assert.AreEqual((int)landBytes, ci.landThrottle, "Land");
  322. Assert.AreEqual((int)windBytes, ci.windThrottle, "Wind");
  323. Assert.AreEqual((int)cloudBytes, ci.cloudThrottle, "Cloud");
  324. Assert.AreEqual((int)taskBytes, ci.taskThrottle, "Task");
  325. Assert.AreEqual((int)textureBytes, ci.textureThrottle, "Texture");
  326. Assert.AreEqual((int)assetBytes, ci.assetThrottle, "Asset");
  327. Assert.AreEqual((int)totalBytes, ci.totalThrottle, "Total");
  328. Assert.AreEqual((int)targetBytes, ci.targetThrottle, "Target");
  329. Assert.AreEqual((int)maxBytes, ci.maxThrottle, "Max");
  330. }
  331. private void SetThrottles(
  332. LLUDPClient udpClient, int resendBytes, int landBytes, int windBytes, int cloudBytes, int taskBytes, int textureBytes, int assetBytes)
  333. {
  334. byte[] throttles = new byte[28];
  335. Array.Copy(BitConverter.GetBytes((float)resendBytes * 8), 0, throttles, 0, 4);
  336. Array.Copy(BitConverter.GetBytes((float)landBytes * 8), 0, throttles, 4, 4);
  337. Array.Copy(BitConverter.GetBytes((float)windBytes * 8), 0, throttles, 8, 4);
  338. Array.Copy(BitConverter.GetBytes((float)cloudBytes * 8), 0, throttles, 12, 4);
  339. Array.Copy(BitConverter.GetBytes((float)taskBytes * 8), 0, throttles, 16, 4);
  340. Array.Copy(BitConverter.GetBytes((float)textureBytes * 8), 0, throttles, 20, 4);
  341. Array.Copy(BitConverter.GetBytes((float)assetBytes * 8), 0, throttles, 24, 4);
  342. udpClient.SetThrottles(throttles);
  343. }
  344. }
  345. }