AssetStatsCollector.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 OpenSim 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.Timers;
  29. namespace OpenSim.Framework.Statistics
  30. {
  31. /// <summary>
  32. /// Asset service statistics collection
  33. /// </summary>
  34. public class AssetStatsCollector : IStatsCollector
  35. {
  36. private Timer ageStatsTimer = new Timer(24 * 60 * 60 * 1000);
  37. private DateTime startTime = DateTime.Now;
  38. private long assetRequestsToday;
  39. private long assetRequestsNotFoundToday;
  40. private long assetRequestsYesterday;
  41. private long assetRequestsNotFoundYesterday;
  42. public long AssetRequestsToday { get { return assetRequestsToday; } }
  43. public long AssetRequestsNotFoundToday { get { return assetRequestsNotFoundToday; } }
  44. public long AssetRequestsYesterday { get { return assetRequestsYesterday; } }
  45. public long AssetRequestsNotFoundYesterday { get { return assetRequestsNotFoundYesterday; } }
  46. public AssetStatsCollector()
  47. {
  48. ageStatsTimer.Elapsed += new ElapsedEventHandler(OnAgeing);
  49. ageStatsTimer.Enabled = true;
  50. }
  51. private void OnAgeing(object source, ElapsedEventArgs e)
  52. {
  53. assetRequestsYesterday = assetRequestsToday;
  54. // There is a possibility that an asset request could occur between the execution of these
  55. // two statements. But we're better off without the synchronization overhead.
  56. assetRequestsToday = 0;
  57. assetRequestsNotFoundYesterday = assetRequestsNotFoundToday;
  58. assetRequestsNotFoundToday = 0;
  59. }
  60. /// <summary>
  61. /// Record that an asset request failed to find an asset
  62. /// </summary>
  63. public void AddNotFoundRequest()
  64. {
  65. assetRequestsNotFoundToday++;
  66. }
  67. /// <summary>
  68. /// Record that a request was made to the asset server
  69. /// </summary>
  70. public void AddRequest()
  71. {
  72. assetRequestsToday++;
  73. }
  74. /// <summary>
  75. /// Report back collected statistical information.
  76. /// </summary>
  77. /// <returns></returns>
  78. public string Report()
  79. {
  80. double elapsedHours = (DateTime.Now - startTime).TotalHours;
  81. if (elapsedHours <= 0) { elapsedHours = 1; } // prevent divide by zero
  82. long assetRequestsTodayPerHour = (long)Math.Round(AssetRequestsToday / elapsedHours);
  83. long assetRequestsYesterdayPerHour = (long)Math.Round(AssetRequestsYesterday / 24.0);
  84. return string.Format(
  85. @"Asset requests today : {0} ({1} per hour) of which {2} were not found
  86. Asset requests yesterday : {3} ({4} per hour) of which {5} were not found",
  87. AssetRequestsToday, assetRequestsTodayPerHour, AssetRequestsNotFoundToday,
  88. AssetRequestsYesterday, assetRequestsYesterdayPerHour, AssetRequestsNotFoundYesterday);
  89. }
  90. }
  91. }