CacheStress.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // CacheStress
  3. //
  4. // Authors:
  5. // Eyal Alayuf (Mainsoft)
  6. //
  7. // (c) 2005 Mainsoft, Inc. (http://www.mainsoft.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Web.Caching;
  31. using System.Threading;
  32. public class CacheStress
  33. {
  34. static int threads = 0;
  35. static int KeyStart = 0;
  36. static long SlidingWindow = 0;
  37. static bool UseAbsoluteTime = false;
  38. static int Modulo = 71;
  39. static Cache c;
  40. static SafeSum Sum = new SafeSum();
  41. static void Main(string[] args)
  42. {
  43. if (args.Length < 2) {
  44. Console.WriteLine("Usage: CacheStress <#threads> <#millis> [UseAbsoluteTime]");
  45. return;
  46. }
  47. c = new Cache();
  48. threads = System.Int32.Parse(args[0]);
  49. SlidingWindow = System.Int64.Parse(args[1]);
  50. UseAbsoluteTime = (args.Length > 2);
  51. for (int i = 0; i < threads; i++)
  52. {
  53. Thread th = new Thread(new ThreadStart(RunCycle));
  54. th.Start();
  55. }
  56. int secs = 10;
  57. for (int j = secs; ;j += secs)
  58. {
  59. Thread.Sleep(1000 * secs);
  60. Console.WriteLine("Executed {0} transactions in {1} seconds", Sum.Value, j);
  61. }
  62. }
  63. static void RunCycle()
  64. {
  65. int n = Interlocked.Increment(ref KeyStart);
  66. for (int i = 1; ; i++) {
  67. try
  68. {
  69. string key = "stam" + n;
  70. object o2 = c.Get(key);
  71. if (o2 == null)
  72. {
  73. if (UseAbsoluteTime)
  74. c.Insert(key, 1, null, DateTime.Now.AddTicks(SlidingWindow), Cache.NoSlidingExpiration);
  75. else
  76. c.Insert(key, 1, null, Cache.NoAbsoluteExpiration, new TimeSpan(SlidingWindow));
  77. }
  78. n = (n * 2 + i) % Modulo;
  79. }
  80. catch (Exception e)
  81. {
  82. Console.WriteLine("Caught exception " + e.GetType().ToString() + ": " + e.Message + e.StackTrace);
  83. }
  84. if (i == 100)
  85. {
  86. Sum.Add(i);
  87. i = 0;
  88. }
  89. }
  90. }
  91. class SafeSum
  92. {
  93. public SafeSum()
  94. {
  95. _value = 0;
  96. }
  97. public int Value { get { lock(this) { return _value; } } }
  98. public void Add(int i) { lock(this) { _value += i; } }
  99. private int _value;
  100. }
  101. }