David's phone was ringing as he walked in the door, and there were four voice mails on his phone already. Before sitting down, he scooped up the phone. "G'morning, this is David."

"Yeah, hi David, this is Jan. I'm sure you're already aware of the intranet issues?" He listened idly to her description of the issue while he opened Outlook, seeing several new emails popping into his inbox. The subject lines were troubling.

URGENT: HR/Financials Reporting System Severe Slowdowns

NOREPLY TrackBugs #105482 is now a HIGH priority issue

NOREPLY TrackBugs #105482 MEDIUM has been assigned to you; "Intranet general issues"

she just cant get Enough ofmy MEET HYRDRANT --- 3nhanc3ment medz!!

Are you guys doing maintenance?

Fwd: Re: Intranet slowness?

"Uh, yeah, I'm aware, Janice. We're working on it. Could be a network issue," he said, massaging his temples.

Slowdown Showdown

Slowness and intermittent crashes generally indicate a web server buckling under the pressure of too much traffic, so before even logging into the server to check it out, David checked the log files; there were tens of thousands of lines, many clustered each second, trying to access a file called "update_clock.php".

update_clock.php? David opened the main template file and found a line he hadn't seen before:

<body onload="updateClock();">

The updateClock method, as David discovered, was defined in the main template. It was designed to retrieve data from update_clock.php via ajax:

<script type="text/javascript">
function updateClock()
{
    var url = 'update_clock.php';
    url = url + '?employee_id=' + <?=$employeeID;?>;
    if (document.getElementById('clockDiv').innerHTML != 'Error')
    {
        document.getElementById('clockDiv').value = '';
        ajaxFunction(url, 'clockDiv');
	setInterval('updateClock()', 1000);
    }
}
</script>

Comments left by someone with the initials "BM" peppered the code near these updates. "Barry?" David yelled over the cube wall.

"Yeah?"

"Do you know what 'update_clock.php' is?"

"Uh, yeah, that updates the clock on the home page, we just got that feature in."

Barry went on to explain how it worked, but it still seemed like the intranet shouldn't be as slow as it was; there had to be more to the story. "This was fully tested? And the change was approved by management?"

"Yeah, of course!"

Time is the Enemy

The reason that they were doing server-side date/time processing for the clock on the home page eluded David, but maybe he'd see a valid reason for it. Tracing the logic, he saw where the date was loaded and output into a text format that the front end would receive via the ajax call. That is, way at the bottom of the code file, after the complete employee record was loaded from the database, and other bits of data were loaded as well, each time with a new DB connection. Reason being, the employee data contained time zone information, and they'd wanted the clock on the page to reflect the correct time. There was a ton of logic before it did anything with the dates, and, in fact, even then, it was just writing out a date and time.

Taken alone, these operations took less than a second, which explained why no issues were discovered in the basic testing that was done. But with dozens of users with the page (or several pages) open in browser windows and tabs, each request to update_clock.php started taking longer and longer, generating an unreasonable amount of traffic and database calls. A single employee in an eight hour shift with the intranet page open all day would generate roughly 30,000 calls to update_clock.php.

Time is of the Essence

"Barry," yelled out over the cube wall again, "you need to get this fixed and deployed to production. Like now!"

Jumping up, Barry responded, "this is going to take time. I mean... I mean, I spent a while just getting it working. Now I need to optimize it?"

"I'm not a web developer," David asked, "but why didn't you use JavaScript's built-in Date object?"

Barry paused for a moment. "JavaScript has a built-in Date object!?"

David helped Barry out, and within minutes they'd refactored the code. And this time, management was even happier to do a quick signoff on the move to production.