Example #1
0
        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");
        }
Example #2
0
        public void WriteObjects(TestType testType)
        {
            int         count1       = 100;
            int         count2       = 10;
            List <long> memoryUsages = new List <long>();
            List <Func <IDbContextConnectionClass> > contexts = PerformanceTestsHelper.GetMemoryTestsCollectionContextCreators();

            foreach (Func <IDbContextConnectionClass> createContext in contexts)
            {
                long initialUsedMemory = 0;
                long usedMemory        = 0;

                initialUsedMemory = PerformanceTestsHelper.GetCurrentUsedMemory();

                using (IDisposable disposableContextInterface = (IDisposable)createContext()) {
                    IDbContextConnectionClass contextInterface = (IDbContextConnectionClass)disposableContextInterface;
                    DbContext context = (DbContext)contextInterface;
                    context.ResetDatabase();

                    for (int companyIndex = 0; companyIndex < count1; companyIndex++)
                    {
                        string companySuffix = companyIndex.ToString();

                        Company company = new Company();
                        company.CompanyName = companySuffix;
                        company.Description = "Description" + companySuffix;

                        for (int officeIndex = 0; officeIndex < count2; officeIndex++)
                        {
                            string officeSuffix = officeIndex.ToString();
                            Office office       = new Office();
                            office.Name        = officeSuffix;
                            office.Description = "Description" + companySuffix;

                            company.Offices.Add(office);
                        }

                        contextInterface.Company.Add(company);
                    }
                    context.SaveChanges();
                }

                using (IDisposable disposableContextInterface = (IDisposable)createContext()) {
                    IDbContextConnectionClass contextInterface = (IDbContextConnectionClass)disposableContextInterface;
                    DbContext context = (DbContext)contextInterface;

                    if (testType == TestType.WithOnePermission)
                    {
                        SecurityDbContext securityDbContext = context as SecurityDbContext;
                        if (securityDbContext != null)
                        {
                            PerformanceTestsHelper.AddOneCollectionPermission(securityDbContext, SecurityOperation.Delete);
                        }
                    }

                    if (testType == TestType.WithMultiplePermissions)
                    {
                        SecurityDbContext securityDbContext = context as SecurityDbContext;
                        if (securityDbContext != null)
                        {
                            PerformanceTestsHelper.AddMultipleCollectionPermissions(securityDbContext, SecurityOperation.Delete);
                        }
                    }

                    List <Company> objects = contextInterface.Company.Select(obj => obj).Include(obj => obj.Offices).ToList();
                    Assert.AreEqual(count1, objects.Count);

                    for (int companyIndex = 0; companyIndex < count1; companyIndex++)
                    {
                        Company company = objects[companyIndex];

                        for (int officeIndex = 0; officeIndex < count2 - 1; officeIndex += 2)
                        {
                            Office curOffice  = company.Offices[officeIndex];
                            Office nextOffice = company.Offices[officeIndex + 1];

                            company.Offices[officeIndex]     = nextOffice;
                            company.Offices[officeIndex + 1] = curOffice;
                        }
                    }

                    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         count1 = 100;
            int         count2 = 10;
            List <long> times  = new List <long>();
            List <Func <IDbContextConnectionClass> > contexts = PerformanceTestsHelper.GetCollectionContextCreators();

            foreach (Func <IDbContextConnectionClass> createContext in contexts)
            {
                IDbContextConnectionClass contextInterface = createContext();
                DbContext context = (DbContext)contextInterface;
                context.ResetDatabase();

                if (testType == TestType.WithOnePermission)
                {
                    SecurityDbContext securityDbContext = context as SecurityDbContext;
                    if (securityDbContext != null)
                    {
                        PerformanceTestsHelper.AddOneCollectionPermission(securityDbContext, SecurityOperation.Create);
                    }
                }

                if (testType == TestType.WithMultiplePermissions)
                {
                    SecurityDbContext securityDbContext = context as SecurityDbContext;
                    if (securityDbContext != null)
                    {
                        PerformanceTestsHelper.AddMultipleCollectionPermissions(securityDbContext, SecurityOperation.Create);
                    }
                }

                Stopwatch watch = new Stopwatch();
                watch.Start();

                for (int companyIndex = 0; companyIndex < count1; companyIndex++)
                {
                    string companySuffix = companyIndex.ToString();

                    Company company = new Company();
                    company.CompanyName = companySuffix;
                    company.Description = "Description" + companySuffix;

                    for (int officeIndex = 0; officeIndex < count2; officeIndex++)
                    {
                        string officeSuffix = officeIndex.ToString();
                        Office office       = new Office();
                        office.Name        = officeSuffix;
                        office.Description = "Description" + companySuffix;

                        company.Offices.Add(office);
                    }

                    contextInterface.Company.Add(company);
                }
                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));
        }