I would like to check the load speed of each page in a particular asp.net website (based on C#). I thought of creating a class for timing the loading and execution of a certain element. I'd like to create a stopwatch object on each user control and on the page itself to get diagnostics on how long the page takes to load altogether. When debug is enabled, we could then see the load times of each of the pages, as well as each of the elements.
I don't want to save it in session, but I really can't think of a way around it.
I was thinking that a nice implementation would be to include a class that I write which will track the stopwatch values for each of the master page, page, and user controls as they load, but where could I save each execution time when it was done? I suppose I could use Session, but is this the only way to save data across user controls and pages?
What would be the best way to do this?
Besides using a logging framework you could use the tracing you need by using the ASP.nets out of the box tracing and diagnostic features [http://msdn.microsoft.com/en-us/library/bb386420.aspx] or you could use something like elmah for other needs. Try not to re-invent the wheel but make a car out of it that flies. Good luck!!
I would use a logging framework like log4net etc... Especially since you're only doing it for debug mode. You can define warning levels to match your loading speeds if something is slower than expected...
I would agree with other answers in that leveraging a logging implementation would be sufficient while doing related logic, such as writing to the log, on page Unload().
If this is an MVC site, you could check out the MVC Mini Profiler. It is available as a Nuget package.
Related
We have a asp.net web form developed in C#. This form has around 100 fields and it's very dynamic form. It's dynamic in the sense, when you first start you see only about 20 fields,but as you go on making differnt selctions in the forms, panels containging fields are displayed or hidden. Almost each field is validated on the client side using .net validators. Javascript is used to turn on/off validators depending on if the panel they are in is visiable or not.
This form has some bugs and it's pain in the rear to debug this form because almos every field has at least 2 validators - require and/regular experssion. Also some of the fields when selected do autopostback. most often users cannot submit the form because one/more validators are still enable but the fields that they are attached to are invisible so it's hard to debug as which validtor is causing the problem.
So here my design question.
What is the easier way to debug validators in this case? How to find which validtors are still on?
Instead of using asp.net validators, Should I just recode the form and
use JavaScript for validation, at least that way it will be esier
to debug validtors.
Any design consideration? Since this form is such a pain, we won't
mind redesigning it, and doing it the right way.
I was thinking what if I create a single JavaScript function that is called every time a form field is either clicked or changed, this JavaScript function will be called and it will check all 100 form fields. If the form field is visible, the corresponding validator is enabled, otherwise disabled.
What do you think of the approach?
Thanks for your time and advice in advance.
Just from a Usability point of view, I'd strongly recommend to keep forms as short as possible and to have a list of forms to fill on an overview page. Users need to pause now and then, and to save work mid-way.
There are reasons to not subdivide a form (surveys from anonymous users, for example). But if users are authenticated and forced to fill out the forms (by laws or other real-world constraints) then you should really have 5 forms of 20 fields or so.
Once the complexity is lower, you'll have an easier time approaching those other problems.
For your questions:
I'd advise to use ASP.NET validators. They are declined into two worlds: client-side for Usability (reactivity) and server-side for security. You'll have a hard time to have the same ease of use and consistency without ASP.NET validators.
The regular expression SHOULD NOT be debugged in the validator but in a separate moment (unit testing or a simple script with tons of test strings) and then dropped-in with little testing afterwards.
On autopostback: why? Try to avoid it or use AJAX.
You'll have a lot of work to do, to follow these advises, but be confident that they'll be worth it. Especially for new projects.
I'm writing this application where I want the (Admin - Developer) have more control of the app than the "regular" users. Let's say, a button will only be shown to the Admin if a setting is enabled.
So I was wondering about how to make 2 different versions? Only way I can think of right now is to first write the "regular" and release it, then re-add the code/features I want the admin to have.
Maybe a setting somewhere or something that can be enabled/disabled during publish that can make life easier? Any help/tips will be greatly appreciated.
Edit: I've just set a setting in the Settings panel (Setting True/False) and using the Properties to allow/disallow features. If anyone knows of a better way please reply!
First you should clearly identify the tasks of the administrators for example user related tasks like creating users, deleting users, giving permissions, resetting passwords and configuring database connections may fall in to this category. Then you can create a separate set of interfaces/ windows to handle this tasks, you should enable these only when an admin is logged in. That is the easiest way to do it. After you have identified the admin features you have the option of creating a separate app for the admins as well.
And if you have some features which you can't separate out of the regular forms like if you have button in a regular form which only gets enabled for specific user I think you should use settings based approach.
It depends on how significant the UI differences are between the two user groups.
Generally, you want to maintain the least amount of code, so the drive is to avoid code duplication, so you won't have to maintain three files with the same logic or ui elements in them. (I assume ASP.NET: User controls are really good to deal with this.)
Second, do not make logical decisions on who the user is, but rather abstract it out and base logic on what a role can do (i.e. permissions). For instance: when processing a page and hiding/showing elements, base the decision to show the "delete" link if the user has the "DELETE" permission.
I apologize in advance for the generic nature of my question, but I was unable to find any helpful advice from people trying to do the same thing as me on the web. Let me describe my scenario:
I am providing end users/designers of a website the ability to customize their views by storing the views (using Razor) in the database. I have all of this working, but my question is the following; From a security standpoint, how can I ensure and enforce that unwanted code doesn't get executed in the user-defined view? There are two basic approaches that I think will work conceptually, but am not sure which one is more possible or feasible.
Option 1: Create a validation method in the administration tool that allows the user to input the view code. This would need to either take a whitelist or blacklist approach to what is allowable or not.
Option 2: Prevent unwanted code from being able to execute when rendering of the view occurs.
As a quick example of something that would need to be blocked, we wouldn't want to allow access to read or write files, access any data access functions, or even access configuration settings, etc. in the web.config. There will likely be a decently-sized list of things that probably shouldn't be allowable, but I'll need to sit down and try to think of as many security-related concerns as possible.
My question then is, which method would be the best bet? Also, can any direction be provided on how to go about either? I thought I might be able to make trust-level based change which would be Option 2, but couldn't find any way to make that work in a per-view based manor (the administration code is allowed to execute whatever it wants). I'm thinking Option 1 will end up being the best bet and I'll have to check for the input of certain framework functions that shouldn't be allowed. Does anyone have any experience doing anything like what I'm trying to do? ANY feedback is much appreciated!
This would be extremely difficult.
You could run the the template through the Razor preprocessor, then use Roslyn (still in early beta) to parse the generated file and look through all method calls (or constructors) and return an error if it calls something you don't like.
I strongly recommend that you use a whitelist for that, since the .Net framework is big enough that you are bound to overlook something in a blacklist.
However, I would instead recommend that you not use Razor at all and instead use a templating engine that does not allow real C# code.
I'm currently building a single page AJAX application. It's a large "sign-up form" that's been built as a multi-step wizard with multiple branches and different verbiage based on what choices the user makes. At the end of the form is an editable review page. Once the user submits the form, it sends a rather large email to us, and a small email to them. It's sort of like a very boring choose your own adventure book.
Feature creep has pushed the size of this app beyond the abilities of the current architecture, and it's too slow to work in any slower computers (not good for a web app), especially those using Internet Explorer. It currently has 64 individual steps, 5400 DOM elements and the .aspx file alone weighs in at 300kb (4206 LOC). Loading the app takes anywhere from 1.5 seconds on a fast machine running FireFox 3, to 20 seconds on a slower machine running IE7. Moving between steps takes about the same amount of time.
So let's recap the features:
Multi-Step, multi-path wizard style
form (64 steps)
Current step is shown in a fashion similar to this: http://codylindley.com/CSS/325/css-step-menu
Multiple validated fields
Changing verbiage based on user
choices
Final, editable review page
I'm using jQuery 1.3.2 and the following plugins:
jQuery Form Wizard Plugin
jQuery clueTip plugin
jQuery sexycombo
jQuery meioMask plugin
As well as some custom script for loading the verbiage from an XML file, running the review page and some aesthetic accoutrements.
I don't have this posted anywhere public, but I'm mostly looking for some tips on how to approach this sort of project and make it light weight and extensible. If anyone has any ideas as far as tools, tutorials or technologies, that's what I'm looking for. I'm a pretty novice programmer (I'm mostly a CSS/xHTML/Design guy), so speak gently. I just need a good plan of attack to make this app faster. Any ideas?
One way would be to break apart the steps into multiple pages / requests. To do this you would have to store the state of the previous pages somewhere. You could use a database to do this or some other method.
Another way would be to dynamically load the parts you need via AJAX. This won't help with the 54000 DOM elements though, but it would help with the initial page load.
Based on the question comments a quick way to "solve" this problem is to make a C# class that mirrors all the fields in your question. Something like this:
public class MySurvey
{
public string FirsName { get; set; }
public string LastName { get; set; }
// and so on...
}
Then you would store this in the session (too keep it easy... I know it's not the "best" way) like this
public MySurvey Survey
{
get
{
var survey = Session["MySurvey"] as MySurvey;
if (survey == null)
{
survey = new MySurvey();
Session["MySurvey"] = survey;
}
return survey;
}
}
This way you'll always have a non-null Survey object you can work with.
The next step would be to break that big form into smaller pages, let's say: step1.aspx, step2.aspx, step3.aspx etc. All these pages would inherit from a common base page that would include the property above. After this all you'd need to do is send the request from step1.aspx back and save it to Survey, similar to what you're doing now but for each small piece. When done redirect (Response.Redirect("~/stepX.aspx")) to the next page. The info from the previous page would be saved in the session object. If they close the browser page they won't be able to get back though.
Rather than saving it to the session you could save it in a database or in a cookie, but you're limited to 4K for cookies so it may not fit.
I agree with PBZ, saving the individual steps would be ideal. You can, however, do this with AJAX. If you did, though, it'd require some stuff that sounds like it might be outside of your skillset of mostly front-end development, you'd need to probably create a new database row and tie it to the user's session ID, and every time they click to the next step have it update that row. Possibly even tie it to their IP address so if the whole thing blows up they can come back and hit "remember me?" for your application to retrieve it.
As far as optimizing the existing structure, jQuery is fairly heavy when it comes to optimization, and adding a lot of jQuery modules doesn't help that. I'm not saying it's bad, because it saves you a lot of time, but there are some instances where you are using a module for one of its many functionalities, and you can replace that entire module with a few lines of jQuery enabled javascript.
As far as minimizing the individual DOM elements, the step above I mentioned could help slim that down, because you're probably loading a lot of extensible functions for those modules that you may or may not need.
On the back end, I'd have to see the source to see how to tell you to optimize it, but it sounds like there's a lot of redundancy in individual steps, some of that can probably be trimmed down into functions that include a little recursion, or at the least delegate some of the tasks to one another.
I wish I could help more but without digging through your source I can only suggest basic strategies. Best of luck, though!
Agree, break up the steps. 5400 elements is too many.
There are a few options if you need to keep it on one page.
AJAX requests to get back either raw HTML, or an array of objects to parse into HTML or DOM
Frames or Iframes
JavaScript to set innerHTML or manipulate the DOM based on the current step. Note with this option IE7 and especially IE6 will have memory leaks. Google IE6 JavaScript memory leaks for more info.
Use document.write to include only the .js file(s) needed for the current step.
HTH.
Sounds like mostly a JQuery optimization problem.
First suggestion would be switch as many selects into ID selectors as you can. I've had speedups of over 200-300x by being able to move to id attribute selection only.
Second suggestion is more of a plan of attack. Since IE is your main problem area, I suggest using the IE8 debugger. You just need to hit f12 in IE8... Tabs 3 and 4 are script and profiler respectively.
Once you've done as much of #1 as you think you can, to get a starting point, just go to profiler, hit start profiling, do some slow action on the webpage, and then stop profiling. You will see your longest method calls, and just work your way through it.
For finer testing/dev, go to the script tab. Breakpoints locals etc are there for analysis. You can dev/test changes via the immediate window... i.e. put a break point where you want to change a function, trigger the function, execute your javascript instead of the defined javascript in the immediate window.
When you think you have something figured out, profile your changes to make sure they are really improvements. Just start the profiler, run the old code, stop it and note your benchmark. Then re-start the profiler and use the immediate window to execute your altered function.
That's about it. If that flow can't take you far enough, as mentioned above, JQuery itself (and hence its plugins) are not terribly performant, and replacing with standard javascript will speed everything up. If your plugins benchmark slow, look at replacing them with other plugins.
I am currently working on a large-scale website, that is very dynamic, and so needs to store a large volume of information in memory on a near-permanent basis (things like configuration settings for the checkout, or the tree used to implement the menu structure).
This information is not session-specific, it is consistent for every thread using the website.
What is the best way to hold this data globally within ASP, so it can be accessed when needed, instead of re-loaded on each use?
Any AppSettings in web.config are automatically cached (i.e., they aren't read from the XML every time you need to use them).
You could also manually manipulate the cache yourself.
Edit: Better links...
Add items to the cache
Retrieve items from the cache
Caching Application Data
It's not precisely clear whether your information is session specific or not...if it is, then use the ASP Session object. Given your description of the scale, you probably want to look at storing the state in Sql Server:
http://support.microsoft.com/kb/317604
That's the 101 approach. If you're looking for something a little beefier, then check out memcached (that's pronounced Mem-Cache-Dee):
http://www.danga.com/memcached/
That's the system that apps like Facebook and Twitter use.
Good luck!
Using ASP.NET caching feature is a good option I think. In addition to John's answer, you can use Microsoft's Patterns & Practices team's Caching Application Block.
This is a good video exploring the different ways to can retain application state.
http://www.asp.net/learn/3.5-videos/video-11.aspx
It brushes on the Application object which is global for the whole application, for all users and shows you how to create a hit counter (obviously instead of storing an integer you could store objects). If you need to make changes, you do need to use a lock for concurrency, and I'm not sure how it handles LARGE amounts of data because I've never had to keep that much there.
I usually keep things like that in the Application object.
If the pages are dependent upon one another and they post to one another, you could use the page's request object. Probably not the answer you're looking for, but definitely one of the smallest in memory to use.
I have run into the same situation in the past and found an interface to be the most scalable solution. Application cache may be the answer today, but will it scale to meet your needs?
If you need to scale up, you may find cookies, or some type of temp database storage to be the trick. Simply add a new method to your interface, and set the interface to choose the "mode" from web.config.