How come my application calls are so very slow?
I'm creating an asp.net application (one of the many I've done).
But this is the first time I've encountered this specific problem.When I press a button on a page the first time (so the application is already loaded) this is very very slow.
After the first time I've clicked the button, all other calls on that page go really fast (as it should be).
I've opened sql profiler and charles (http debugger) to see what is happening and the funny part is: The page itself is slow, the queries that are executed take +- 50ms in total to be executed but it takes 18 seconds to initiate the call to the database.
So the .net code takes very very long before it starts his sql code. I really don't get it. I hope someone can help me out.
This is how my application layer is done:
Masterpage --> page --> usercontrol (within repeater) --> button.
The button just executes a service layer who will access the repository layer which will commence the call to the database.
Here is the logging that I got.
this is my application structure:
UserService --> UserRepository : RepositoryBase --> dataAccess. The code:
public User GetByEmailAndPassword(string email, string password)
{
_log.Info("UserRepository.GetByEmailAndPassword - Start");
this.Test();
User result = null;
List<DbParam> parameters = new List<DbParam>();
parameters.Add(new DbParam("@Email", email, DbType.String));
parameters.Add(new DbParam("@Password", password, DbType.String));
_log.Info("UserRepository.GetByEmailAndPassword - Entering reader");
var reader = ExecuteReader("User_GetByEmailAndPassword", parameters);
if (reader.Read())
{
result = _entityFactory.ConvertToModel(reader);
}
reader.Close();
_log.Info("UserRepository.GetByEmailAndPassword - End");
return result;
}
The repository base:
public abstract class RepositoryBase<T>
where T : IEntity
{
private static ILog _log = LogManager.GetLogger(typeof(RepositoryBase<T>));
protected IEntityFactory<T> _entityFactory;
protected RepositoryBase()
{
_log.Info("Constructor");
_entityFactory = EntityFactoryBuilder.BuildF开发者_运维百科actory<T>();
}
protected IDataReader ExecuteReader(string storedProc, List<DbParam> parameters)
{
_log.Info("ExecuteReader - Start");
_log.Info("Open connection");
var db = DatabaseFactory.CreateDatabase();
_log.Info("Open command");
var dbCommand = db.GetStoredProcCommand(storedProc);
foreach (DbParam param in parameters)
{
db.AddInParameter(dbCommand, param.Name, param.Type, param.Value);
}
_log.Info("Execute command");
var reader = db.ExecuteReader(dbCommand);
_log.Info("ExecuteReader - End");
return reader;
}
}
The logging shows that it takes 14 seconds just to enter this function.
var reader = ExecuteReader("User_GetByEmailAndPassword", parameters);
THIS ISN'T EXECUTING IT BUT JUST ENTERING THE FUNCTION.
I'm startled I really have no idea why calling upon a function in a base class is taking this long.
I could add some logging data but it just shows what I'm telling here.
Could it be that this is a property due to virtual servers?
Because testing the same application on a non virtual server doesn't have this problem at all.Cheers, M.
I have run into a similar issue at work. The cause was that the server the web site was running on was a virtual server. It doesn't look like you are doing anything CPU intensive (same as our application) and therefore there shouldn't be an issue. But, for us, putting the application on a dedicated server did solve the issue...
The problem of this answer is fully described here. So the anser to my problem is another question. Thx everyone!!
https://serverfault.com/questions/174236/iis-7-5-doesnt-cache-3rd-party-dll-on-recycle
Maybe on postback you are loading some control which is not loaded on initial GET and that control is not compiled yet, so it takes long time to compile the control (i.e. folder of Web site where it is)
Or maybe you're doing postback to a different page?
Can you describe what exactly you are doing on the postback?
My wild guess would be that CLR is loading (and maybe compiling) the DLLs and source code on the first launch, added to starting the sockets that are used to connect to the DB.
You might want to initialize the DB connection at launch, to warm up the CLR to avoid that.
Also I'd recommend http://msdn.microsoft.com/en-us/library/ff647787.aspx which is Kinda old, but you might want to give it a read.
Implement a Page_Init function and check your timing from there to LoginLinkButton_Click - you'll break the problem down to either inside your page or in your webservers plumbing - if it takes forever to reach Page_Init then the webserver is doing something odd (rebuilding dlls ?), if it takes forever from page_init then you've got the issue
精彩评论