Showing posts with label Sloth. Show all posts
Showing posts with label Sloth. Show all posts

Saturday, March 01, 2008

Sloth v0.1.1 beta released

Hey "all".

So it is time for another beta, this time we're talking about Sloth v0.1.1 - an exciting new version which again has improved a lot of the internal design in Sloth. This means more unit tests for verifying that code changes does not break existing stuff, optimized code, and a cleaner, more maintainable design. This is of course a great benefit as it makes adding new exciting features a lot easier. But what visible changes does Sloth v0.1.1 give you, the user?

Works on Vista "out of the box"

Vista has gotten a lot of heat, but it won't disappear any time soon, so I thought it was important to support it properly. Sloth now puts log- and configuration files in proper places so that Vista does not go ballistic because it tries to write to Program Files etc. This also means Sloth works well in terminal server environments.

Alert changes

You can now tell Sloth to alert you when a particular show is on - no more tagging or creating a rule manually. Just point to a show, right click, and choose "Notify me of this show". When the time comes, Sloth will let you know. Also, there is now an option (on by default) to tell Sloth not to display alerts while a full-screen application (presentation, video, game) is running.

If your screen resolution or screen working area changes, Sloth will now rearrange any open alerts to reflect this; No more alerts all over the place. When an alert is closed (automatically or manually by the user), any remaining open alerts will be moved to fill the open slot.

Rule handling changes

Rules are now handled in their own unit-tested section of the configuration. Quite a lot has been done internally to work with "delta rules" - i.e. when there is a rule change (a rule has been added, deleted or modified), Sloth will only update with the changes, not all rules as before. For users with significant amounts of rules, this should be noticeable. There is also a new search subsystem, which is currently only used for rule matching. Not yet optimized, but almost as fast as the old system, and vastly more flexible.

Community functionality

There is quite a lot being done to support community functionality; Show ratings, discussions etc. Not a lot of this is visible in the current beta, but more will be seen soon. It is now possible to rate shows in the community, but that won't show up for anyone else yet. The community functionality also includes a simplistic news system and a better Sloth Updater feature.

Better tools for helping me debug problems

A new program - SlothBetaInfo - is now included in the start menu. This will make it easier to locate logs etc. Also, if Sloth should crash somehow, you will now get a window with exception info that I would highly appreciate on the forum along with an explanation of what went wrong.

Other changes

Loading the XMLTV data file is now a lot faster. The notification and installation of newer versions is a lot more robust. A lot of old code has been cleaned up and refactored. TV data grabbing is less error-prone. Sloth also handles new TV channels showing up in data files without problems.

More statistics

As a follow-up to last time, where we had 92 passing unit tests, we now have 355. Does this guarantee that there are no bugs in Sloth? Of course not. But it means that if I change some code that is covered by a unit test, and break something, I'll know right away. And as I mentioned after v0.1, if a new bug is found in this code, I can add a unit test to check for it - if it comes back, I'll know immediately. Another important metric here is code coverage - how much of the library code that is actually tested by unit tests. The current numbers are:

SlothCommunity 40%
SlothCore 65%
SlothUtil 73%
SlothConfig 76%

While 100% code coverage would be nice, it's not necessarily realistic. However, I will work to improve the testing, making sure it is harder and harder for bugs to slip by.

Summary

Well, perhaps no revolutionary new features, but all of the above contributes to make Sloth a much more mature, stable application. I will try to move into quicker-iteration mode now, adding some nice new features and releasing both alphas and betas at a quicker pace. Happy testing!

Monday, September 24, 2007

Fun with optimalization

Sloth, being written in/towards the .Net Framework (currently 2.0), is an object oriented application. I try to follow a few general principles on how to do stuff. One of the things I try to avoid is premature optimalization - the idea being that you first get it to work, with unit tests as a backing confidence booster, and THEN you optimize.

Just for fun, I thought I would optimize something that has survived since the early days of Sloth v0.0.0.1 - it is well proven code, and it would be very easy to verify if my optimizations break anything, as Sloth would cease to function at all.

The area I chose for this experiment is the Channel object, more specifically the OnNow and OnNext properties. These basically tell the caller which show is on the channel right now, and which one is on next.

A run with a profiler shows me that while this does not affect overall application performance much, it is definately ready for some improvements. These properties have largely gone unchanged after being more or less among the first 20 or so things written in Sloth. Optimizing them should be very easy, but I was eager to see just how much I could squeeze out of it. The routines are used by the main window to determine which shows to list, as well as by the "early warning system" that scores OnNow/OnNext before anything else to get alerts launched ASAP after Sloth starts.

The original OnNow would loop through the collection of shows until it found one whose StartTime was less than/equal to DateTime.Now, and whose StopTime was larger than/equal to DateTime.Now. If a match was found, it was returned. Since Sloth never adds shows that end before the current day to the list, at most this would mean having to traverse a whole day of shows before finding the right one. Didn't take long, but it took long enough - there was a definitive room for improvement here!

The implementation of OnNext was way worse. Keep in mind that this was written in the days where Sloth made no warranty that the list of shows was sorted in chronological order. So basically, OnNext would loop through the collection, and if it found any show with a StartTime larger than DateTime.Now, check if it was lower than what it already had (if anything), and if so, grab it. This meant looping through the whole collection. Large room for improvement.

While these numbers are taking from a single profiling session, and in no way represent any real benchmarks, they give you a good indication of the "savings" possible. What I did was basically profile these two properties from Sloth startup until the UpdateScores routine was finished and all alerts fired.

Original OnNow 67 ms
Original OnNext 82 ms

Wow - together they consume a whopping 0.15 seconds on my PC! Way to pick an important target for optimization! ;)

Well, I'm in it for the lesson, not the ... eh ... something.

So time to try to be clever.

Optimization 1 - Cached objects

So how about we cache the show found to be OnNow, the show to be OnNext, and that way, we don't have to look through the list all the time? Excellent idea! How do we know when we actually have to go looking again? Easy - OnNow.StopTime should tell us when the show currently on is over and the next one is ready. So I added two local variables for onNow and onNext, and a DateTime called cachedOnNowSwitch (yeah, clever name) which would be set to the time it was necessary to run the list again.

OnNow simple optimization 19 ms
OnNext simple optimization 42 ms

That's nice - reducing OnNow by a third and halving OnNext shows that caching the objects was a good plan. If only we could tighten the list traversal a little..

Optimization 2 - Cached DateTime.Now

In the profiler, DateTime.Now suddenly stuck out as a sore thumb. It seems like fetching this from the OS takes a relatively long time. So how about instead of calling DateTime.Now all the time I do date comparison in the list, I create a local DateTime called now that I set at the beginning, and then compare to it instead?

OnNow medium optimization 6,8 ms
OnNext medium optimization 28 ms

Okey, now we're talking! But it should be possible to optimize even further...

Optimization 3 - No more OnNext list walking

Since the original code was written, Sloth has started ensuring that the show lists are sorted at all times. It's just more efficient that way. So is it really necessary for OnNext to run through the whole list of shows to find its' target? Of course not.

Since OnNow is cached and these two are always called near each other, lets have OnNext just call showlist.IndexOf(OnNow) and add 1 - that should be our show!

OnNow cool optimization 6,1 ms
OnNext cool optimization 3,2 ms

No special reward for OnNow this time around, just an insignificant brain fart probably. But OnNext is starting to look real good!

Optimization 4 - Cache the IndexOf result

Since we already know via the cached DateTime when our OnNow will be outdated, why don't we just cache the result of the IndexOf call as well? Also, let's add some checking for empty show lists and not doing anything when hitting out-of-bounds conditions.

OnNow super optimization 2,9 ms
OnNext super optimization 0,2 ms

Nice numbers!

So there we have it - I'm not going to try to go further, as there is probably nowhere left to optimize for a developer of my skill level. Most of the fat now is in the looping and date comparison stuff, which I can do very little about. So I am plenty happy with the outcome.

I'm looking forward to seeing what can be done in other time-intensive Sloth loops!

Until next time...

Sunday, August 05, 2007

Sloth v0.1 works in Windows Vista after all

Hey.

So I was fiddling around a little on my Virtual PC with Vista installed - a strange thing that happened was that when you installed Sloth, it would work. However, if you restarted your machine or otherwise stopped Sloth and restarted it, it would no longer work. No specific error message, just a generic "Well, it just stopped working, see..." thingy.

After reading up a bit online (I don't really use Vista myself yet), I found that the new UAC "feature" of Vista was probably to blame. What happened is that when you started the installer, it asked you for permissions; When you give it the goahead, it installs, then runs Sloth. Sloth sees no config and runs the Wizard. The wizard does it's thing and then starts Sloth. All of this still happening with the permissions you gave the installer that you presumably downloaded off the net.

So, you get sick of Sloth and shut it down, then decide you need to see what's on tonight. You double click it.

"Well, it just stopped working, see..."

Yeah, annoying as hell.

However, if you right click Sloth, and choose "Run as administrator", then it works. What Einstein decided that this was the correct way to handle this issue!? No meaningful error message at all! If it had told me "Well, your program tried to write a log file to \Program files\, which is bad", then at least I could do something about it.

But no, just dying and claiming it was the programs' fault seems like a much better approach.

Anyway, until I get Sloth to behave nicer according to Vista's unknown rules, run it as an admin.

Wednesday, June 20, 2007

Sloth v0.1 beta released

This is the first beta that is announced on this blog - and it is also the first beta that uses the .Net Framework v2.0. All beta testers up to and including v0.0.25 got away with just using .Net 1.1, but now you will all need to upgrade. You can download the .Net 2.0 framework installation here if you don't have it (the Sloth installation will tell you if you need it).

Apart from the move to .Net 2.0, there are a number of changes, fixes and additions to Sloth this time around too. A massive job of refactoring has been undertaken as well - this will not really be visible to users (perhaps except in the form of new bugs), but it has really cleaned up the codebase and will make it a lot easier to add exciting features in the future. The job is not finished, however, so a lot of time will be spent on refactoring for the next betas as well. This really should be reflected in the overall quality of the application.

So apart from boring stuff, what's new this time?

New features in the search window

Instead of just searching for phrases, you can now also perform "all words" searches, as well as search for regular expressions and tags.

image

In this example we have searched for anything that contains both of the tags "babe" and "star" (these tags are defined by my personal rule set, and may or may not be anything like the ones you're using).

As you can see, there is also now a Search button in place. This means no more "I'll start searching if you haven't typed anything for 0.8s" behaviour.

Tags are now clickable in ShowInfo windows

A consequence of the above is that it is now also possible to click the tags displayed in ShowInfo windows such as this:

ShowInfo window for Star Wars III with clickable tags

Clicking a tag would open a search window like in the last paragraph, showing all shows containing the same tag.

Programmable hotkeys

In older versions of Sloth, there were just two hard-coded hotkeys; Win-S for opening the main window, and Win-K for killing all open alerts. It is now possible to define as many of these as you want. The config window GUI is pretty ugly still, but it mostly works.

image

I'm guessing there's room for improvement here. Just like in every single other piece of Sloth GUI. :)

Keep in mind that this means that the two hardcoded hotkeys are no longer there! If you want to keep them, you'll have to add them yourself!

New context menu

Sloth now features a global, semi-intelligent context menu. This menu formats itself according to what the user is clicking. In the future it will also be user extendible, meaning you can add your own options to it. Want to search a certain website for actor information? Or perhaps launch VLC with the IP address of the channel you're clicking? The possibilities are.. eh.. well, several.

Grid view enhancements

The grid view is coming along nicely, although it is still a performance slug and there are a number of bugs living large.

image

You can now right click on individual shows and get the same context menu as in the rest of Sloth (see above).

Find pictures of actors and directors

Don't you just hate it when you recognize a name but you just can't place them? Well, I do, so I made this silly little thing which locates pictures of people off the net and shows them to you. Like this here:

image

You get this function by right clicking on an actor or a director in the ShowInfo window, and then choosing "Find picture(s)..." - please note that this is a web search, not in any way under my control, so the pictures you get may not even be remotely related to the person you're looking for. It could be them, or it could be someone they worked with, a movie poster without them featured, or their stalker.

Alerts rewritten

Alerts have been completely rewritten from the ground up. They may still look almost the same, but they now use much less resources, the close button is bigger, they can be used for much more, and they don't steal focus from other applications when they appear. They also react better when scores change.

Download and install new Sloth from inside Sloth

I know a lot of you - and myself - have wanted this feature for a long time. And more software really should have it. Remember the old, annoying window that would pop up with New Sloth information and not close until you went to the forum? Well, here's the new version:

image

Check out the ugly blue progress bar at the bottom. This appears after the user presses the Install-button. Once it is done downloading, it will start the installer automatically. The installer will in turn shut down Sloth and optionally restart it after the upgrade. All you have to do is click "Next".

This feature will of course not be seen by anyone until it is time to download the next beta after this one.

Minor changes to GUI and other stuff

There is a new date picker control in windows where you can choose what day to watch (individual channel windows, Interesting Shows window, and Grid window). This is currently a simple dropdown list showing the list of dates we have TV data for.

Bug fixes

Unsurprisingly, a number of these bug thingies have also been fixed. The fixes include:

  • Even if Sloth was configured not to play an alert sound, it would try to, throwing an exception in the process.
  • Tags are now case insensitive.
  • In certain conditions, if you added a new rule, alerts would not show up for shows matched by this rule (even if their score was high enough) unless you restarted Sloth.
  • When rule changes led a show with an open alert to drop beneath the alert threshold, the alert was not removed.
  • If the config window was minimized, pressing the Config button (main window) or menu item (systray context menu) did not "unminimize" it.
  • If shows in the XMLTV file are overlapping, Sloth will now clip them so that the first show stops when the second begins.
  • If rule changes cause tags to be added/removed for a show with an open ShowInfo window, the tags display will now be updated properly.
  • If you delete a rule in the config window, the rule list/categories are no longer reset. This caused GUI annoyance.
  • A bug that caused Sloth to screw up config window GUI related to online file checking has been fixed.
  • The wizard could overwrite your rule file if you ran it after the initial install.
  • The wizard could in some cases put the main window outside the screen on low-resolution systems.

Supported operating systems

Currently, Sloth is only verified to work on these versions of Windows:

  • Windows 2000
  • Windows XP

It also installs and runs fine on Windows Vista, but if you end Sloth and then restart it, it won't run. It also won't run after a reboot - in fact the only time you can get it to run is right after the installation. This is being looked into.

Statistics

I am slowly implementing unit testing for the libraries (SlothConfig, SlothCore, SlothUtil) to make sure I can refactor code without breaking existing functionality. That is the idea, anyway. Basically, I am supposed to write a test that runs every piece of the library code with every possible piece of input, and report if it breaks. I am not there yet. But for each test I add, it is less likely something will break as a result of change. And every time I fix a bug, I can write a test to make sure it doesn't come back.

As of Sloth v0.1 beta, there are 92 unit tests passing, and none failing. If some failed, I couldn't really release it.

 Availability

If you're a beta tester, you know where to get it. If not, send me an email, and we'll see about making you one. :)

Wednesday, May 30, 2007

Retro-fitting design, unit tests and coverage

Blog is back in business, after a good run of dormancy. Or is it? We'll see.

A while back, I got caught up in the hype surrounding test driven development. I read a bit about it, and while the whole Agile religion got to be a little too much for my tastes, I've tried to adopt a few of the practices. Of course, I see the point of writing the test first since it would be easy to get a unit test for an existing piece of code to verify that it does what it does. However, when you're aware of this, I've found that it forces you to put more thought into how you implement the test for existing code, which in turn can open your eyes to some good refactoring opportunities.

However, some other parts of the mantra (like pair-programming and their seemingly obsessive attitude about snacks) are simply not for me; It's hard to work in pairs when you're alone, and snacks might be the best way to treat my newly discovered diabetes. Well, I guess I can eat some fruit.

However, I can easily see the benefits of Test Driven Developement and refactoring, even in a relatively simple desktop gadget like Sloth. Keep in mind that I've been learning .NET and C# along the way here; Object oriented programming is something that has been completely new to me until recently. I guess you could say I'm the worst kind of self-taught waterfall-cowboy. :)

So, what happens when you mix a little theory on OOD, refactoring and unit testing with the spaghetti code from hell that is your existing project?

Well, at first I was very happy that these new ways of thinking helped me do a lot of clever refactoring. Code was optimized (no, not performance optimized), moved around, things are only done once, in smaller units.. The libraries have become leaner and more maintainable. That's for sure.

Then came the time for getting rid of some serious code smells. Almost all of the configuration related code has been moved to the designated library - except for the ancient configuration window, where a LOT of nasty code duplication is happening, and the main Session object, a textbook example of a God object if I ever saw one.

One of the problems with the fact that I was trying to apply the proper Object Oriented Design after the fact, is that the configuration window had no concept of the SlothConfiguration object. This object (via it's subobjects) contains everything you need in a configuration, and both the Wizard and Sloth itself uses it for everything config-related. Except the config window. It just copies out the bits and pieces it likes, and then leaves the session object to figure out what to do with a zillion properties. Apart from the maintenance horror, there are just so many things wrong with this that the few hairs I have left turn gray just looking at the code.

So after two weeks of boring code-monkey grunt work, I am almost to the point where I can start testing again; The configuration window knows nothing of the underlying logic, it just clones itself a config object and uses it. Then, if the user is happy and closes with the OK button, this config is made active. Sounds simple, right? Well, NOW it is.

So what I have left now is a God-object (10% smaller than when I started) that still needs heavy refactoring, a relatively flexible config solution, and utility libraries that keep growing. What's next?

Well, for a desktop application like this, the cool thing is of course adding new features. And I will do that. However I will spend half the coding time refactoring and testing until I can show the Sloth source code to someone's grandmother and not be embarassed. That'll take a while.

I must add that the process of learning to utilize unit testing (a process which I am nowhere near mastering) has been quite painless thanks to three very nice projects: TestDriven .NET, MbUnit and NCover. I love using them, and can recommend them to anyone wanting to give unit testing/test driven developement in Visual Studio a try.

All right, that was my first attempt at a blog rant. I already see that this was pretty boring, so if anyone by chance stumbles on to this blog, thanks, I'm not expecting to see you again. :)