AssetsClient.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.Net;
  30. using System.Text;
  31. using System.Reflection;
  32. using System.Threading;
  33. using OpenMetaverse;
  34. using log4net;
  35. using log4net.Appender;
  36. using log4net.Layout;
  37. using OpenSim.Framework;
  38. using OpenSim.Services.Interfaces;
  39. using OpenSim.Services.Connectors;
  40. namespace OpenSim.Tests.Clients.AssetsClient
  41. {
  42. public class AssetsClient
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(
  46. MethodBase.GetCurrentMethod().DeclaringType);
  47. private static int m_MaxThreadID = 0;
  48. private static readonly int NREQS = 150;
  49. private static int m_NReceived = 0;
  50. public static void Main(string[] args)
  51. {
  52. ConsoleAppender consoleAppender = new ConsoleAppender();
  53. consoleAppender.Layout =
  54. new PatternLayout("[%thread] - %message%newline");
  55. log4net.Config.BasicConfigurator.Configure(consoleAppender);
  56. string serverURI = "http://127.0.0.1:8003";
  57. if (args.Length > 1)
  58. serverURI = args[1];
  59. int max1, max2;
  60. ThreadPool.GetMaxThreads(out max1, out max2);
  61. m_log.InfoFormat("[ASSET CLIENT]: Connecting to {0} max threads = {1} - {2}", serverURI, max1, max2);
  62. ThreadPool.GetMinThreads(out max1, out max2);
  63. m_log.InfoFormat("[ASSET CLIENT]: Connecting to {0} min threads = {1} - {2}", serverURI, max1, max2);
  64. if (!ThreadPool.SetMinThreads(1, 1))
  65. m_log.WarnFormat("[ASSET CLIENT]: Failed to set min threads");
  66. if (!ThreadPool.SetMaxThreads(10, 3))
  67. m_log.WarnFormat("[ASSET CLIENT]: Failed to set max threads");
  68. ThreadPool.GetMaxThreads(out max1, out max2);
  69. m_log.InfoFormat("[ASSET CLIENT]: Post set max threads = {1} - {2}", serverURI, max1, max2);
  70. ThreadPool.GetMinThreads(out max1, out max2);
  71. m_log.InfoFormat("[ASSET CLIENT]: Post set min threads = {1} - {2}", serverURI, max1, max2);
  72. ServicePointManager.DefaultConnectionLimit = 12;
  73. AssetServicesConnector m_Connector = new AssetServicesConnector(serverURI);
  74. m_Connector.MaxAssetRequestConcurrency = 30;
  75. for (int i = 0; i < NREQS; i++)
  76. {
  77. UUID uuid = UUID.Random();
  78. m_Connector.Get(uuid.ToString(), null, ResponseReceived);
  79. m_log.InfoFormat("[ASSET CLIENT]: [{0}] requested asset {1}", i, uuid);
  80. }
  81. for (int i = 0; i < 500; i++)
  82. {
  83. var x = i;
  84. ThreadPool.QueueUserWorkItem(delegate
  85. {
  86. Dummy(x);
  87. });
  88. }
  89. Thread.Sleep(30 * 1000);
  90. m_log.InfoFormat("[ASSET CLIENT]: Received responses {0}", m_NReceived);
  91. }
  92. private static void ResponseReceived(string id, Object sender, AssetBase asset)
  93. {
  94. if (Thread.CurrentThread.ManagedThreadId > m_MaxThreadID)
  95. m_MaxThreadID = Thread.CurrentThread.ManagedThreadId;
  96. int max1, max2;
  97. ThreadPool.GetAvailableThreads(out max1, out max2);
  98. m_log.InfoFormat("[ASSET CLIENT]: Received asset {0} ({1}) ({2}-{3}) {4}", id, m_MaxThreadID, max1, max2, DateTime.Now.ToString("hh:mm:ss"));
  99. m_NReceived++;
  100. }
  101. private static void Dummy(int i)
  102. {
  103. int max1, max2;
  104. ThreadPool.GetAvailableThreads(out max1, out max2);
  105. m_log.InfoFormat("[ASSET CLIENT]: ({0}) Hello! {1} - {2} {3}", i, max1, max2, DateTime.Now.ToString("hh:mm:ss"));
  106. Thread.Sleep(2000);
  107. }
  108. }
  109. }