ThrottleTests.cs 19 KB

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