Advertisement
Guest User

asp.net web/app.config read/write performance

a guest
Apr 26th, 2016
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1.     public class HomeController : Controller
  2.     {
  3.         const int IterationCount = 1000;
  4.         private Stopwatch stopwatch = new Stopwatch();
  5.         public ActionResult Index()
  6.         {
  7.             if(Session["mykey"] == null)
  8.                 Session["mykey"] = Guid.NewGuid();
  9.  
  10.             ViewBag.FirstWrite = TestWritePerformance();
  11.             ViewBag.FirstReadAfterWrite = TestReadPerformance();
  12.  
  13.             TestWriteAverage();
  14.             TestReadAverage();
  15.  
  16.             return View();
  17.         }
  18.  
  19.         private void TestReadAverage()
  20.         {
  21.             long readSum = 0;
  22.             for (int i = 0; i < IterationCount; i++)
  23.             {
  24.                 readSum += TestReadPerformance();
  25.             }
  26.             ViewBag.AverageRead = readSum/IterationCount;
  27.         }
  28.  
  29.         private void TestWriteAverage()
  30.         {
  31.             long writeSum = 0;
  32.             for (int i = 0; i < IterationCount; i++)
  33.             {
  34.                 writeSum += TestWritePerformance();
  35.             }
  36.             ViewBag.AverageWrite = writeSum/IterationCount;
  37.         }
  38.  
  39.         private long TestReadPerformance()
  40.         {
  41.             stopwatch.Restart();
  42.             var configFile = WebConfigurationManager.OpenWebConfiguration("~");
  43.  
  44.             var readAfterWrite = configFile.AppSettings.Settings["MYKEY"].Value;
  45.             stopwatch.Stop();
  46.  
  47.             return stopwatch.ElapsedMilliseconds;
  48.         }
  49.  
  50.         private long TestWritePerformance()
  51.         {
  52.             var guid = Guid.NewGuid().ToString();
  53.  
  54.             var configFile = WebConfigurationManager.OpenWebConfiguration("~");
  55.             var settings = configFile.AppSettings.Settings;
  56.  
  57.             stopwatch.Restart();
  58.             settings["MYKEY"].Value = guid;
  59.             configFile.Save(ConfigurationSaveMode.Minimal);
  60.             ConfigurationManager.RefreshSection("appSettings");
  61.             stopwatch.Stop();
  62.  
  63.             return stopwatch.ElapsedMilliseconds;
  64.         }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement