AssetStatsReporter.cs 4.6 KB

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