简体   繁体   中英

CPU usage goes up because of Entity Framework (connect to database)

I have website (2 core-6gb ram), and for every request that goes to server cpu start to go up and when request is answered cpu came to normal

And I test that in simple console application, and again it has happened

I found out that is because of Entity Framework (add-update-delete-find ,..), so what should I do? I do everything right....

This my repository sample:

public class FactorRepository : IDisposable
{
    private DomainModels.AppEntities _dbDnt = null;

    public FactorRepository()
    {
        _dbDnt = new DomainModels.AppEntities();
    }
    public bool Add(DomainModels.Factor entity, bool autoSave = true)
    {
        try
        {
            _dbDnt.Factors.Add(entity);
            if (autoSave)
                return Convert.ToBoolean(_dbDnt.SaveChanges());
            else
                return false;
        }
        catch
        {
            return false;
        }
    }

    public bool Update(DomainModels.Factor entity, bool autoSave = true)
    {
        try
        {
            _dbDnt.Factors.Attach(entity);
            _dbDnt.Entry(entity).State = EntityState.Modified;
            if (autoSave)
                return Convert.ToBoolean(_dbDnt.SaveChanges());
            else
                return false;
        }
        catch
        {
            return false;
        }
    }

    public bool Delete(DomainModels.Factor entity, bool autoSave = true)
    {
        try
        {
            _dbDnt.Entry(entity).State = EntityState.Deleted;
            if (autoSave)
                return Convert.ToBoolean(_dbDnt.SaveChanges());
            else
                return false;
        }
        catch
        {
            return false;
        }
    }

    public bool Delete(long id, bool autoSave = true)
    {
        try
        {
            var entity = _dbDnt.Factors.Find(id);
            _dbDnt.Entry(entity).State = EntityState.Deleted;
            if (autoSave)
                return Convert.ToBoolean(_dbDnt.SaveChanges());
            else
                return false;
        }
        catch
        {
            return false;
        }
    }

    public DomainModels.Factor Find(long id)
    {
        try
        {
            return _dbDnt.Factors.Find(id);
        }
        catch
        {
            return null;
        }
    }

    public List<DomainModels.Factor> Where(Expression<Func<DomainModels.Factor, bool>> predicate)
    {
        try
        {
            return _dbDnt.Factors.Where(predicate).ToList();
        }
        catch
        {
            return null;
        }
    }

    public List<DomainModels.Factor> Select()
    {
        try
        {
            return _dbDnt.Factors.AsQueryable().ToList();
        }
        catch
        {
            return null;
        }
    }

    public List<TResult> Select<TResult>(Expression<Func<DomainModels.Factor, TResult>> selector)
    {
        try
        {
            return _dbDnt.Factors.Select(selector).ToList();
        }
        catch
        {
            return null;
        }
    }

    public long GetLastIdentity()
    {
        try
        {
            if (_dbDnt.Factors.Any())
                return _dbDnt.Factors.OrderByDescending(p => p.Id).First().Id;
            else
                return 0;
        }
        catch
        {
            return -1;
        }
    }

    public long Save()
    {
        try
        {
            return _dbDnt.SaveChanges();
        }
        catch
        {
            return -1;
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (this._dbDnt != null)
            {
                this._dbDnt.Dispose();
                this._dbDnt = null;
            }
        }
    }

    ~FactorRepository()
    {
        Dispose(false);
    }

Update : even when I use this class as repository problem still ongoing

    public class FactorRepository 
{
    public bool Add(DomainModels.Factor entity, bool autoSave = true)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                database.Factors.Add(entity);
                if (autoSave)
                    return Convert.ToBoolean(database.SaveChanges());
                else
                    return false;
            }
        }
        catch
        {
            return false;
        }
    }

    public bool Update(DomainModels.Factor entity, bool autoSave = true)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                database.Factors.Attach(entity);
                database.Entry(entity).State = EntityState.Modified;
                if (autoSave)
                    return Convert.ToBoolean(database.SaveChanges());
                else
                    return false;
            }
        }
        catch
        {
            return false;
        }
    }

    public bool Delete(DomainModels.Factor entity, bool autoSave = true)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                database.Entry(entity).State = EntityState.Deleted;
                if (autoSave)
                    return Convert.ToBoolean(database.SaveChanges());
                else
                    return false;
            }
        }
        catch
        {
            return false;
        }
    }

    public bool Delete(int id, bool autoSave = true)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                var entity = database.Factors.Find(id);
                database.Entry(entity).State = EntityState.Deleted;
                if (autoSave)
                    return Convert.ToBoolean(database.SaveChanges());
                else
                    return false;
            }
        }
        catch
        {
            return false;
        }
    }

    public DomainModels.Factor Find(long id)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                return database.Factors.Find(id);
            }
        }
        catch
        {
            return null;
        }
    }

    public DomainModels.Factor Find(string username)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                return database.Factors.Find(username);
            }
        }
        catch
        {
            return null;
        }
    }
    public List<DomainModels.Factor> Where(Expression<Func<DomainModels.Factor, bool>> predicate)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                return database.Factors.Where(predicate).ToList();
            }
        }
        catch
        {
            return null;
        }
    }

    public List<DomainModels.Factor> Select()
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                return database.Factors.AsQueryable().ToList();
            }
        }
        catch
        {
            return null;
        }
    }

    public List<TResult> Select<TResult>(Expression<Func<DomainModels.Factor, TResult>> selector)
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                return database.Factors.Select(selector).ToList();
            }
        }
        catch
        {
            return null;
        }
    }

    public long GetLastIdentity()
    {
        try
        {
            using (AppEntities database = new AppEntities())
            {
                if (database.Factors.Any())
                    return database.Factors.OrderByDescending(p => p.Id).First().Id;
                else
                    return 0;
            }

        }
        catch
        {
            return -1;
        }
    }
}

Update : usage

FactorRepository blFact = new FactorRepository();

var model1 = blFact.Where(p => p.UserId == userid).SingleOrDefault();

Finding performance issues is kinda hard, but I think that in your case the query compilation phase from an Entity Framework is a problem. You can very quickly verify it using a Performance Monitor tool from Windows system and PerfView (or any other memory profiler).

Open a performance monitor and add the following counters to your process: .NET CLR Memory (% Time in GC and Allocated Bytes/sec). If you have a lot of allocations or your time in GC is over > 10 then we are closer to confirm that fault is in the query compilation phase. Now execute PerfView to monitor your memory usage. If in the result you notice a lot of allocation from the entity framework namespace then yes it's a query compilation phase.

I have a similar issue, you can take a look here: Entity framework 6.1.3 high cpu usage for Insert, Update operations

Additionally, I wrote a blog post about the query compilation phase and problems I had on a read side: http://devmate.net/2017/10/entity-framework-the-query-plan-cache-story/ . Maybe it will be handy for you too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM