public void WriteTest() { int count = 100; bool firstStart = true; List <long> times = new List <long>(); List <Func <DbContextMultiClass> > contexts = new List <Func <DbContextMultiClass> >(); for (int i = 0; i < 5; i++) { contexts.Add(() => new DbContextMultiClass()); } foreach (Func <DbContextMultiClass> createContext in contexts) { using (IDisposable disposableInterface = (IDisposable)createContext()) { IDbContextMultiClass contextInterface = (IDbContextMultiClass)disposableInterface; DbContext context = (DbContext)contextInterface; context.ResetDatabase(); for (int i = 0; i < count; i++) { DbContextObject1 obj = new DbContextObject1(); obj.Description = "Description " + i.ToString(); context.Add(obj); } context.SaveChanges(); } using (DbContextMultiClass context = createContext()) { List <DbContextObject1> objects = context.dbContextDbSet1.Select(obj => obj).ToList(); Assert.AreEqual(count, objects.Count); SecurityDbContext securityDbContext = context as SecurityDbContext; if (securityDbContext != null) { PerformanceTestsHelper.AddMultiplePermissions(securityDbContext, SecurityOperation.Write); } Stopwatch watch = new Stopwatch(); watch.Start(); foreach (DbContextObject1 obj in objects) { context.Security.PermissionProcessor.IsGranted(typeof(DbContextObject1), SecurityOperation.Write, obj); } watch.Stop(); if (firstStart) { firstStart = false; } else { times.Add(watch.ElapsedMilliseconds); } } } Assert.LessOrEqual(times.Average(), 1); }
public void CreateObjects(TestType testType) { int count = 1000; List <long> memoryUsages = new List <long>(); List <Func <IDbContextMultiClass> > contexts = PerformanceTestsHelper.GetMemoryTestsContextCreators(); foreach (Func <IDbContextMultiClass> createContext in contexts) { long initialUsedMemory = 0; long usedMemory = 0; initialUsedMemory = PerformanceTestsHelper.GetCurrentUsedMemory(); using (IDisposable disposableContextInterface = (IDisposable)createContext()) { IDbContextMultiClass contextInterface = (IDbContextMultiClass)disposableContextInterface; DbContext context = (DbContext)contextInterface; context.ResetDatabase(); if (testType == TestType.WithOnePermission) { SecurityDbContext securityDbContext = context as SecurityDbContext; if (securityDbContext != null) { PerformanceTestsHelper.AddOnePermission(securityDbContext, SecurityOperation.Create); } } if (testType == TestType.WithMultiplePermissions) { SecurityDbContext securityDbContext = context as SecurityDbContext; if (securityDbContext != null) { PerformanceTestsHelper.AddMultiplePermissions(securityDbContext, SecurityOperation.Create); } } for (int i = 0; i < count; i++) { DbContextObject1 obj = new DbContextObject1(); obj.Description = "Description " + i.ToString(); context.Add(obj); } context.SaveChanges(); } long beforeCollect = GC.GetTotalMemory(true); usedMemory = PerformanceTestsHelper.GetCurrentUsedMemory(); memoryUsages.Add(usedMemory - initialUsedMemory); } double securedContextBytesGrow = PerformanceTestsHelper.GetSecuredContextValue(memoryUsages); double nativeContextBytesGrow = PerformanceTestsHelper.GetNativeContextValue(memoryUsages); Assert.IsTrue(false, "our: " + securedContextBytesGrow.ToString() + " bytes, native: " + nativeContextBytesGrow.ToString() + " bytes"); }
public void CreateObjects(TestType testType) { int count = 1000; List <long> times = new List <long>(); List <Func <IDbContextMultiClass> > contexts = PerformanceTestsHelper.GetContextCreators(); foreach (Func <IDbContextMultiClass> createContext in contexts) { IDbContextMultiClass contextInterface = createContext(); DbContext context = (DbContext)contextInterface; context.ResetDatabase(); if (testType == TestType.WithOnePermission) { SecurityDbContext securityDbContext = context as SecurityDbContext; if (securityDbContext != null) { PerformanceTestsHelper.AddOnePermission(securityDbContext, SecurityOperation.Create); } } if (testType == TestType.WithMultiplePermissions) { SecurityDbContext securityDbContext = context as SecurityDbContext; if (securityDbContext != null) { PerformanceTestsHelper.AddMultiplePermissions(securityDbContext, SecurityOperation.Create); } } Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < count; i++) { DbContextObject1 obj = new DbContextObject1(); obj.Description = "Description " + i.ToString(); context.Add(obj); } context.SaveChanges(); watch.Stop(); times.Add(watch.ElapsedMilliseconds); } double securedContextTime = PerformanceTestsHelper.GetSecuredContextValue(times); double nativeContextTime = PerformanceTestsHelper.GetNativeContextValue(times); double nominalTimeDifference = GetTimeDifference(testType); double timeDifference = securedContextTime - nativeContextTime; Assert.IsTrue(timeDifference <= nominalTimeDifference, GetTimeDifferenceErrorString(timeDifference, nominalTimeDifference)); Debug.WriteLine(GetDebugTimeString(securedContextTime, nativeContextTime)); }