Finding references to JavaScript objects that are hanging around after they have been removed

So this might be blindingly obvious and I just never noticed it before. I’d like to think it’s a new feature since last time I tried to debug this kind of thing, otherwise I took a whole lot more time than I needed to in the past.

It’s fairly easy in javascript when manipulating the DOM to accidentally reference a node after you’ve removed it from the page. Maybe an event handler is still attached, or a global array still points to it. In a small app it’s not a problem, but once the app gets big enough that memory is a concern there’s usually a significant amount of code to go through.

I was looking for ways to reduce the memory taken up by the page and at the same time make things faster. That’s when I found this on Stack Overflow: Finding JS memory leak in chrome dev tools. The top answer linked to this jsFiddle: jsfiddle.net/C5xCR/6/ which was a good place to experiment with the tools a bit.

The steps:

  1. Open the page and get to the point in the app where you think the object should be removed
  2. Open Chrome’s DevTools and go to the Profiles tab
  3. Take a heap snapshot
  4. Find the element in the heap
  5. Open the Object’s retaining tree

You should get something that looks a little bit like this:

It doesn’t link you directly to the source file, but you do get variable names which you can then find in your code. This works best if you can do it on a un-minimized, dev copy of the code.

Posted in Software Development | Tagged , , | Leave a comment

It Lives

Finally resolved an issue that was keeping my blog offline for several days. Apparently the WordPress update was not compatible with a plugin I had.

Unfortunately it took hours to discover that because php would give me no errors when it loaded. I’d get a http 500 and a blank page. No errors logs or anything. I should really setup a monitoring service so I find these things quicker in the future.

Posted in Meta | Leave a comment

30

So I turned 30 the other day. It sounded like a big deal, but now that it’s here it doesn’t seem like all that big of a number really. Maybe this is a good opportunity for me to write a bit and reflect on the last year.

I feel like I’ve been falling behind in the technology department a bit. Not that anything I’m working with is out of date at all, just that the brand new stuff doesn’t excite me as much as it used to. Like node.js for example, I know what it is and I understand how it works at a high level. I just don’t have a need for it now, so I’m not going to spend much time on it. I think that’s part of the reason I’m not writing as much as I used to.

I finally learned Git pretty thoroughly lately. I see so many large open source projects jumping to it that I figured I should learn it to stay relevant. It really helps that I can use it against a Subversion server too.

I’ve been spending more time on things that are a little outside the software world. I’ve been working out, cooking, taking pictures, etc. a lot. I do enjoy all these things. I’m also getting better at the boring stuff like getting organized and paying bills and saving money.

At the same time I miss being excited about new tech. Someday in the not too distant future I should start up some personal projects. I’m thinking I could make a phone app or a game sometime. Or even contribute a bug fix here and there. Maybe I’ll have a few more posts about patterns or strategies instead of technical howtos.

Posted in Meta | Leave a comment

Personal Information

I’ve been thinking to myself for a while now about adding more personal posts here. I’ve stayed away so far because I figure anyone that does happen to subscribe to my rss feed probably isn’t all that interested. Facebook has been where I have most of my online social interactions, but I’ve been wanting to tone that down some.

With the tragedies last week maybe it’s time to write something here. Some of them hit really close to home, I had friends over at Clackamas Town Center that day. I’ve also had some sad things happen in the family. My grandmother passed away and many of my childhood memories of her were from Christmas so this year has been kind of rough. And not long before that my parent’s had the dog put down.

I guess maybe what I’m trying to say is that our personal lives do affect our professional lives. Maybe keeping them separate isn’t what I should be doing. What I should be doing is writing more, because if nothing else I could really use the practice. And really, this information isn’t that private.

I’ve been doing some reading, most notably of Anil Dash’s The Web We Lost and I want to put more of my content on my own domain and then share it or copy it over to Facebook & Twitter (and I guess Google+ too.)

Oh, and we’re moving to a new house. What better time to start making some changes than with a complete change of environment? Maybe I’m just rambling here, but maybe that’s not a bad thing.

Posted in Uncategorized | Leave a comment

Selenium Page Objects for an AJAX Admin Page

So before I begin, I wanted to mention something about how I haven’t been keeping this site up to date much lately. And I think part of that has to do with getting tired of code and getting burned out on putting in development time after hours. I used to have a lot of free time and energy for that, but now it feels like I’m not learning much new and interesting. It gets tiring looking at the same tools and languages all the time. I’m just working day to day and trying to get some enthusiasm back. Hopefully writing more will get me looking at some new stuff and get me some practice organizing my thoughts.

On to the tests

Having said that, I’m spending a lot of time with Selenium lately and I love how much it’s grown up since the early days. I’m using Selenium WebDriver (aka Selenium 2.0) and Selenium Grid in C# with NUnit.

The tests I inherited were doing okay, but some were needlessly complex. Often looping through elements (which got very slow by the way) and not cleaning up after themselves. I cleaned up lot of that complexity with better selectors trimming out code that wasn’t really needed. Even after that many tests were still long and repetitive due to the complexity of working with a real application using AJAX and backed by a SQL database.

Page Objects

That’s when I happened on this pattern called Page Objects. Essentially the idea is to create a class representing your page with methods to interact with that specific page. You might create properties for page’s textboxes and a Submit() function to click the submit button. This Page Object Class would contain a reference to the driver instead of the test. Before I found this pattern, I had started pulling common operations out to functions that lived inside the test’s class.

Well I started experimenting with the idea, but my application under test uses one html page for admin. Data is loaded in dynamically and the user can navigate to other parts of admin by clicking tabs. So I came up with something that makes my test code look a bit like this:

var users = admin.GoToUsers();
var roles = users.GoToRoles();
roles.AddNewRole("test");

// Alternatively as a single line:
admin.GoToUsers().GoToRoles().AddNewRole("test");

I was going for a fluent api with this, where I could chain function calls one after another. Each function like GoToUsers() above is a few lines looking something like this:

public AdminUsers GoToUsers()
{
    Driver.FindElement(By.Id("usersTab")).Click();

    // Common utility function that waits until a loading image is gone from the page.
    WaitForAjax();

    return new AdminUsers(Driver);
}

It does just a little bit of work and then returns a new object. The test can use this new object to navigate farther into the page.

So far my implementations all assume that if you have a reference to the object, that you’ve already opened the page and navigated to the right area.

Thoughts

I think these utility functions/classes help keep the tests readable even if you don’t know anything about the page or much about Selenium at all. And I’m hoping once there is enough to cover most of the page, that it’ll be a simple process for anyone to follow the examples already there and add another one if it’s missing.

I’m still trying this out and only a handful of tests are using it now. I think it has a lot of potential. Do you have any experience with Page Objects in your tests? Or do you see any potential issues with the direction I’m pushing these in?

Posted in Software Development | Tagged , , | 3 Comments

Trying to get back to writing

So my blog has been pretty dead lately. It’s been over 3 months since my last post. I want to do something about that and I could make a bunch of excuses: I’ve been too busy, I don’t know what to write, etc., etc.

I’m going to try an experiment. I’ve been trying to use the Pomodoro technique at work when I get stuck. If you haven’t heard of it, all you do is set a timer for 25 minutes and try to focus without getting distracted. Then take a 5 minute break to get all your distractions out of the way. And repeat. I like using Focus Booster for my timer.

Anyway, what I want to do is spend at least one 25 minute block a week writing something here. It might be the most insightful or in depth writing ever, but at least I’ll get some momentum going. That’s the important thing I think: is starting and then keeping at it until it becomes habit.

Also as part of this experiment, I may start writing about some new topics. So far this blog has been almost entirely about my work and the tools and languages I use there. If I start branching out, I’m hoping I’ll have more to say. And I may put up a few posts completely unrelated to programming or computers altogether. We’ll see.

If I get really off topic, I could start posting some recipes that I find tasty or some pictures from Comicon. For a while I avoided stuff like that because I thought it might take away from the programming/work related posts. In the past I kept that kind of stuff on Facebook or Twitter, but I think my own site is a better long term home for some of it.

And no, I’m not participating in NaNoWriMo, but I would like to write more. I could always use some improvement to my communication skills. That’s really the original reason I started blogging way back when.

Posted in Uncategorized | Leave a comment

Intro to performance troubleshooting for programmers

Performance is something that I’ve spent some time with and I find quite interesting. Troubleshooting slow code seems like a straightforward process to me, but I find many times I get ahead of myself and confuse other developers. Sometimes they aren’t aware of the many tricks and tools available to make their code faster. I figured I’d write up some introductory material.

Your code needs to actually be slow to do this

Maybe your code is already fast enough. Maybe it doesn’t make much difference to you if a page takes one or two seconds to render, or maybe you only have a few hundred rows in that database table. If that’s the case don’t worry about it. Trying to make code fast without knowing where it will slow down can make it ugly and difficult to maintain.

Once you do have a problem, the first step is to determine where it is


You can’t start troubleshooting slowness without first having at least a general idea of where the bottleneck is. That’s a common thread I’ll probably mention a few more times, but to start with try to figure out if you’re getting hung up at the UI end, the DB end, or somewhere in the middle.

Sometimes this is as easy as watching your code run. Other times you need some tools. If your code powers a web page, open up Firebug (or your browser’s equivalent) and look for where the time is spent.

If you have a database, see if you have any tracing or profiling tools. For SQL Server: SQL Profiler is useful.

If your database is slow

I’m not going to go into much depth here, there are many sources that can give you pages and pages of pointers. However, there are a few easy things to check for: 1) Open up Windows Task Manager and see if the CPU or RAM are maxed out 2) Look at the tables being queries, are they huge? Do they have indexes?

If Task Manager doesn’t give you enough details, there’s always Performance Monitor. Or the modern SQL Server Management Studio has it’s own Activity Monitor, there should be a button on the toolbar.

Try running the queries directly against the database, instead of through your code. Are they still slow? Great, start tweaking the query or the table until it gets better. Do this on a backup copy of the data and not your live production data!

If your code is slow

It may be that you get the data quickly, but then for some reason the code to process that data is slow. There are usually only a few bottlenecks, and it usually comes down to a single piece of code that runs over and over hundreds or thousands of times.

I like to start with the IDE’s debugger. Just start up the code and step over the lines one by one. You can usually “feel” when one of them is taking significantly longer. If that happens, restart the process, but this time step into that function. Eventually that should lead you to the problem.

If attaching a debugger isn’t an option, logging or print statements with a basic timer can do the trick.

When neither of those tricks works, it’s time to look for more specialized tools. There are code profilers out there that will tell you exactly how many milliseconds are spent on each line of code. On the downside they are almost as intrusive as attaching a debugger, so don’t use them in production. I like dotTrace, but there are others out there for most environments.

Wrapping up

So I don’t know how helpful this will be to those of you that read this. For me performance issues have always been a matter of identifying the problem, and then applying a fix. Often the identification process is where I spend the most time (just like fixing a bug really.) Sometimes developers want to jump straight to a solution, but you can’t do that without more information. The whole thing is often a process of elimination. Once you have a small piece of the app to focus on, and you can ignore everything else, it becomes much easer to come up with ideas to fix it.

As Sherlock says: “‘Data! Data! Data!’ he cried impatiently. ‘I can’t make bricks without clay.’”

Posted in Software Development | Tagged , | Leave a comment

How do you implement a feature when there are concerns about potential future problems?

Lets say you’re tasked with writing some code to meet some requirements. Now either because of your past experience in the area, or because the requirements outline potential difficulties, you have some concerns. You might know that adding the feature has the potential to slow things down and you’re already sensitive to this because users have complained in the past about things taking too long.

The way I see it, there are two schools of thought for how to handle this. The first option is to write simple, clean code while keeping the landmines in mind. Then testing to see how it goes. The second option is to bulk up the code making it more complex and possibly more resistant to the perceived problems.

I’ve tried for a while now to make my code simple and understandable. I feel that’s the best way to make sure it works as intended and is easy to modify by me or anyone else down the road. I’ve noticed lately that when I go for the simpler implementation of a feature, I get asked a lot questions.

Implementation one: Minimal, with tests

I much prefer the route with the minimalistic clean code. Then through testing and profiling tools I can pinpoint exactly where problems are, if there are any. If there aren’t, I saved myself some time. If there are bottlenecks or the code isn’t flexible enough, I can build on top of the clean code.

I know the simple implementation works because I spent time testing it and I find it makes a pretty good guide for a more complex implementation. I can see where some people might say that I took more time than necessary by writing it twice. In my experience it feels like it’s just as fast if not faster to write it once and rewrite it.

Implementation two: Robust, but complex

This route would require more design and code up front. The idea being that it would be able to handle the potential problems better.

There’s several things I don’t like about this approach. First of all you’re spending a lot of time and energy focusing on things like threading and caching when that could be better spent looking at the problem the code is trying to solve. Secondly, often times these concerns turn out to be non-issues, but the code now has all this complexity that’s going to make future changes more error prone.

Implementation three: something else?

Is there another approach I’m overlooking? I feel like the correct route is obvious, but I regularly questions or confusion. I’m not on the same page with everyone. Maybe I just need to spend more time discussing options and less time in Visual Studio.

So which seems like the safest bet to you? How do you handle a situation such as this?

Posted in Software Development | Leave a comment

Reclaiming my work day

So this isn’t a technical post, but still applies to developers. I have had trouble managing my time and the amount of work coming in. I’m sure most people that work at a desk run into this every now and then.

Here’s four things I try to do that help:

  • I try to keep my inbox empty. See http://inboxzero.com/.
  • I try not to check my email all the time. This is hard for me.
  • I try to make a note of anything that’s nagging at the back of my mind while I’m trying to focus.
  • I drink better coffee now that I’m much better at making it. :)

They don’t make things perfect, but at least I feel like I’m in control now.

Email

I lose a lot of time to things like emails coming in, IM conversations. Just one email or IM discussion can easily eat an hour. I really try to be as helpful as I can, but sometimes I don’t have time left at the end of the day.

I’ve been pretty good about implementing Inbox Zero. I rarely have any emails left in my inbox by the end of the day and I have filters for mailing lists and CC’s. However I’d like to start processing my email in batches, so I don’t have to deal with it first thing in the day. The problem is that as soon as Outlook opens and I glance at that number, I feel compelled to take a few minutes and at least sort them. And then if there’s just one that doesn’t make sense or asks me to do something I can’t get done, it nags at me the rest of the day.

Notes

Another thing that’s helping a lot is just writing down anything I can’t get to right at the moment. Sometimes I’ll be debugging some problem and I’ll notice code that could be improved, or I get an idea that would make error logging easier, or whatever. Rather than trying to make the change right away and get sidetracked, I used to try to remember that until I got done. The problem was that I would either forget about it or I’d keep thinking about it when I needed to focus on other things.

Now I just create a one line note in Evernote. I can move it and tag it later when I have time. Or sometimes I’ll just delete it outright because it doesn’t seem like such a hot idea anymore.

Posted in Software Development | Tagged , , | 1 Comment

Plugging a ConnectionProvider in to NHibernate to connect to multiple databases

I recently had a scenario where I needed to connect to a variety of databases depending on context. The databases all shared the same schema, the contents were just partitioned out based on customer.

I’d originally done this with many SessionFactories (like this post does.)

After a while I ran into memory usage issues (See this post about debugging with WinDbg.)

So that’s when I started looking for another option. What I wanted to do was use one factory and supply it with connections to the right databases.

The Provider

/// <summary>
/// Connection provider that will override the default connection string and use one that's
/// been specified for this thread.
/// </summary>
public class DbSpecificConnectionProvider : DriverConnectionProvider
{
    /// <summary>
    /// Returns a new connection using the connection string set in ThreadConnectionString.
    /// </summary>
    public override IDbConnection GetConnection()
    {
        var connection = Driver.CreateConnection();
        try
        {
            connection.ConnectionString = GetConnectionStringFromContext();
            connection.Open();
        }
        catch (DbException)
        {
            connection.Dispose();
            throw;
        }
        return connection;
    }
}

This isn’t too complex really. It inherits from the default provider of DriverConnectionProvider and overrides the GetConnection function. It then gets a connection from the driver, sets the connection string and opens the connection. If there are any errors it’ll cleanup the connection before exiting.

Additional bits

You’ll also need to edit your configuration to tell it about the new provider:

                                    
<property name="connection.provider">
  My.Namespace.DbSpecificConnectionProvider, MyAssembly
</property>

And if you are using second level caching, you might need to implement a caching provider so that the cache doesn’t get cross-contaminated.

Now this hasn’t had that much testing yet, so I’m still being a little cautious with it. But if it works, it’s a nice clean solution to my specific problem.

Posted in Software Development | Tagged | 2 Comments