Comment On Getting Better Sleep

Reporting from the field, Zak tells us the sad story of a sysadmin who has been forced out of his comfort zone. When your sysadmin writes a bunch of scripts in Perl and then your company decides to switch to .NET, the job of converting the scripts will almost certainly fall to none other than the sysadmin. This will be the case even if said sysadmin doesn't know .NET. [expand full text]
« PrevPage 1 | Page 2Next »

Re: Getting Better Sleep

2007-03-01 09:05 • by Pawel (unregistered)
If the sysadmin knows perl, he will know the sleep operation.

Re: Getting Better Sleep

2007-03-01 09:06 • by Sid2K7 (unregistered)
Nice code. Next time, tell him to configure the "IdleProcess" to run for that amount of time ;)

On another note: What happened to TheDailyWTF? Is this the same site? The logo seems somewhat familiar, but the layout and such are different. Granted, I haven't been here for a while, but replacing something as "The Daily WTF" (which has a very clear meaning) with the phrase "Worse Than Failure" (which probably is not used in IT as much as "WTF") is a step in the wrong direction.

Then, again, the stories seem to be just as hilarious, so ill be back after my usual interval (1-2 months).

Captcha: bathe - I'd better ....

Re: Getting Better Sleep

2007-03-01 09:09 • by Tom (unregistered)
I think the wtf here would have to be converting server scripts to .NET.

Honestly, if perl is the best tool for the job, why re-write the scripts?

Re: Getting Better Sleep

2007-03-01 09:09 • by Jimmy (unregistered)
"the guy who coded this is technically not a developer"


If that's the case, then I will submit my grandma's VB.NET WTF's, as she's also technically not a developer.

Re: Getting Better Sleep

2007-03-01 09:16 • by Dr. Advil (unregistered)
Calling Thread.sleep() would allow other threads to run and may subject your current thread to undesired behavior via a scheduler that you have no control over.

Re: Getting Better Sleep

2007-03-01 09:17 • by Dr. Advil (unregistered)
123829 in reply to 123828
but why did he have to use a double? if you want fractions of seconds just go with the standard milliseconds approach.

Re: Getting Better Sleep

2007-03-01 09:18 • by Will Perdikakis (unregistered)
The code is actually pretty resourceful not a WTF, really.

He knew what he wanted to do and rather than using the build-in thread function, he used a spin-lock. Lots more wasted CPU cycles, but virtually the same end results.

How many times have you implemented something that already existed in a standard function library only to find out later?

Re: Getting Better Sleep

2007-03-01 09:32 • by nobody (unregistered)
Last time I looked, Perl was available for Windows, with a free version, and if you wanted to spend the money, a full Visual Studio plug-in language.

I haven't tried to use it in .NET, but I'd guess that it can be made to interoperate.

And this function would be bad even in perl.
"How come our server is so slow?"

Re: Getting Better Sleep

2007-03-01 09:36 • by Dave (unregistered)
Windows Script Host (WSH) or the new PowerShell would be the best way to handle something like this. In WSH ou can get to nearly all system functionality via WMI or FileSystemObject.

Re: Getting Better Sleep

2007-03-01 09:41 • by vt_mruhlin (unregistered)
Not knowing .NET that well, things like this confuse me:
Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString());

It seems intuitive that DateTime.Now.TimeOfDay might already be a DateTime object, so converting it to a String, then back to a DateTime..... ?

Re: Getting Better Sleep

2007-03-01 09:46 • by Will (unregistered)
123843 in reply to 123837
vt_mruhlin:
Not knowing .NET that well, things like this confuse me:
Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString());

It seems intuitive that DateTime.Now.TimeOfDay might already be a DateTime object, so converting it to a String, then back to a DateTime..... ?


Exactly, it's a waste of time...which is the point. It keeps the thread busy until the specified time has elapsed.

Re: Getting Better Sleep

2007-03-01 09:54 • by AbbydonKrafts
123844 in reply to 123832
Will Perdikakis:
The code is actually pretty resourceful not a WTF, really.

He knew what he wanted to do and rather than using the build-in thread function, he used a spin-lock. Lots more wasted CPU cycles, but virtually the same end results.

How many times have you implemented something that already existed in a standard function library only to find out later?


I remember doing that in VB6 until I learned about the Sleep API.

I did it in a server app that waited for files to process and the network admin asked me why my app was always taking 100% of the CPU. *oops*

Re: Getting Better Sleep

2007-03-01 09:58 • by George (unregistered)
123845 in reply to 123843
Will:
vt_mruhlin:
Not knowing .NET that well, things like this confuse me:
Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString());

It seems intuitive that DateTime.Now.TimeOfDay might already be a DateTime object, so converting it to a String, then back to a DateTime..... ?


Exactly, it's a waste of time...which is the point. It keeps the thread busy until the specified time has elapsed.


The converts make the code, um, inelegant. But other than that, I've been seeing sleep loops since:

10 FOR I = 1 TO 1000
20 NEXT I

At least this will sleep for the same amount of time regardless of hardware platform.

Re: Getting Better Sleep

2007-03-01 09:59 • by ssprencel
I don't think this is fair to this poor guy. This sysadmin had been asked to leave his comfort zone and learn a new language (a difficult language at first) and put it in production. To expect results other than messed up code is foolish. The management or development team should have worked with him to find out what it was he was trying to do and help him. At the least they should have reviewed his code and gave him some pointers.

Re: Getting Better Sleep

2007-03-01 09:59 • by RON (unregistered)
What I think is wrong:

1) using a double for passing a time value to a function. Come on people, .NET has a TimeSpan class specifically for this reason. Using a TimeSpan saves clients of the code time since they no longer have to look up the function def to figure out if the time is in seconds/milliseconds/etc.

2) Converting to/from strings when you already have DateTime's? WTF?

3) Using a 100% CPU utilization loop to "pause" the system. Hooray. (idiot)

Re: Getting Better Sleep

2007-03-01 10:02 • by RON (unregistered)
The entire function can be shrunk down to this, by the way:


Thread.Sleep( TimeSpan.FromSeconds( seconds ) );


*sigh*

Re: Getting Better Sleep

2007-03-01 10:03 • by ssprencel
123849 in reply to 123847
This guy doesn't know .NET. This guy isn't a developer. What seperates him from the other people we mock here on this site is that this guy is not pretending to be something he's not. I think you're wrong to call him an idiot.

EDIT: That's wierd. I could swear I hit the quote button.

Re: Getting Better Sleep

2007-03-01 10:07 • by Kerin
123851 in reply to 123849
ssprencel:
This guy doesn't know .NET. This guy isn't a developer. What seperates him from the other people we mock here on this site is that this guy is not pretending to be something he's not. I think you're wrong to call him an idiot.


Agreed. When we're forced to work with systems we do not understand, the most admirable action of all is to do the most we can with what we have. This guy did just that.

Re: Getting Better Sleep

2007-03-01 10:11 • by df5 (unregistered)
I concur, he his not a programmer, no WTF.

Just to be fair, I asked my wife, an english major, to write a program to make the computer pause for a few seconds. She said "Just start OpenOffice." <bada bing!>

Re: Getting Better Sleep

2007-03-01 10:17 • by Cheatz (unregistered)
Processors are bought with a purpose: Put them to work! Use those cycles!

Re: Getting Better Sleep

2007-03-01 10:22 • by TheMagic8Ball (unregistered)
123856 in reply to 123837
vt_mruhlin:
Not knowing .NET that well, things like this confuse me:
Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString());

It seems intuitive that DateTime.Now.TimeOfDay might already be a DateTime object, so converting it to a String, then back to a DateTime..... ?


Actually, TimeOfDay is a TimeSpan (since midnight). Since only the time is taken into account (and not the time AND date), it's possible to pause for an entire day. Granted the box would have to be slow (or slowed during the execution of that code..or if the seconds was 0 or possibly 1). In addition, this would not pause at all if the start time is before midnight and the end time is after midnight. It would just exit, in this case.

Re: Getting Better Sleep

2007-03-01 10:26 • by SomeCoder (unregistered)
The real WTF is that they insisted on moving this code from Perl to .NET.

I think this is a pretty good effort by someone who doesn't code for a living.

Re: Getting Better Sleep

2007-03-01 10:44 • by Kinglink (unregistered)
No, the real WTF is the people here thinking this is ok.

To explain why it is not ok, this code will use as much of the cpu cycles as it can, Thread.sleep uses minimal cycles. Unless you're running a system with out any other programs running on it. Thread.sleep is the only answer. This is a newbie mistake but it's a pretty major mistake.

Note: while (current_time > Time_to_sleep_to); where current time is a function call to get the current time in milliseconds is ALSO very wrong.

Re: Getting Better Sleep

2007-03-01 10:48 • by Mio
123868 in reply to 123848
RON:
The entire function can be shrunk down to this, by the way:


Thread.Sleep( TimeSpan.FromSeconds( seconds ) );


*sigh*


Or

System.Threading.Thread.Sleep(seconds * 1000);


Why bother instantiating a TimeSpan object?

Re: Getting Better Sleep

2007-03-01 10:50 • by ssprencel
123869 in reply to 123866
Kinglink:
No, the real WTF is the people here thinking this is ok.

To explain why it is not ok, this code will use as much of the cpu cycles as it can, Thread.sleep uses minimal cycles. Unless you're running a system with out any other programs running on it. Thread.sleep is the only answer. This is a newbie mistake but it's a pretty major mistake.

Note: while (current_time > Time_to_sleep_to); where current time is a function call to get the current time in milliseconds is ALSO very wrong.


No one said the code is ok. We are giving the author a pass because he is not expected to know .NET. He's a sysadmin, not a programmer. In other words, get off his back. :)

Re: Getting Better Sleep

2007-03-01 10:54 • by cheezedawg (unregistered)
123870 in reply to 123847
RON:
What I think is wrong:
snip...
3) Using a 100% CPU utilization loop to "pause" the system. Hooray. (idiot)

There are some very legitimate reasons to do it this way. I was working on some code a while back that had some tight timing requirements (I needed to sleep for 10-20 microseconds). There are several functions that appear to handle this on the surface (usleep, nanosleep, etc), but the problem is that if you are not using a real-time OS, they all yield to the scheduler. In the case of Linux, the scheduler operates on a 10ms timer, so the minimum that you can really sleep using usleep or nanosleep is 10ms. This was unacceptable for my application.

I ended up making a similar function to loop until the current time had reached the desired interval, albeit without the unnecessary conversion to a string. There still could be a context switch in the middle of the loop, but it was a lot better than using a sleep function.

Re: Getting Better Sleep

2007-03-01 11:02 • by City Developer (unregistered)
I think this is a relatively slight WTF on the developer's part - perhaps a WTH? or according to the new title "not quite as bad as failure"?

Its clear the "developer" here knew what what he knew (and I sense knew it well) and was asked to implement something important in a totally new framework / language / paradigm (which is of course the major WTF here). Given that, he did well to manage to implement something that compiled and did the job. But it doesn't excuse him not stopping and thinking "does what I want to do exist already either as a library function or a code snippet on the web".

I would suggest that should be every programmer's instinctive natural reaction when asked to implement something which "seems" as though its common and often-used - the chap here should have realised he is unlikely to be the first person to want to do what he had been asked to do, and should have therefore first checked if a CLR function existed to help him, and if it didnt, to see on the web if someone had submitted an existing implementation.

To do neither and instead to march off and write his own implementation on a totally new framework is a (relatively minor) WTF.

Re: Getting Better Sleep

2007-03-01 11:13 • by webzter
C'mon, asking a sysadmin to switch all of his maintenance scripts from stuff that he very well may have just brough with him from job to job to a new language is pretty dumb.

The non-CodeSOD version of this story would be a post talking about how company Z mandated standardizing on .NET, even the maintenance scripts that controlled the backup validation.

Re: Getting Better Sleep

2007-03-01 11:16 • by webzter
123874 in reply to 123872
City Developer:

To do neither and instead to march off and write his own implementation on a totally new framework is a (relatively minor) WTF.


If s/he's like any good sysadmin I've ever met, they're too busy supporting too many servers and trying to do too many things. They don't have time to learn the ins and outs of a new framework, they're too busy figuring out how to administer the thing the new framework runs on top of.

Re: Getting Better Sleep

2007-03-01 11:20 • by poochner
123876 in reply to 123846
ssprencel:
The management or development team should have worked with him to find out what it was he was trying to do and help him. At the least they should have reviewed his code and gave him some pointers.


No! No! No! This is .NET, not C. Show him the manuals and give him some books. Thus giving him references.

(sorry :-)

Re: Getting Better Sleep

2007-03-01 11:22 • by poochner
123877 in reply to 123855
Cheatz:
Processors are bought with a purpose: Put them to work! Use those cycles!


Q: Why are you running {Seti, or whatever} on this box?

A: Idle cycles never come back.

Re: Getting Better Sleep

2007-03-01 11:25 • by Anonymous (unregistered)
123879 in reply to 123869
ssprencel:
Kinglink:
No, the real WTF is the people here thinking this is ok.

To explain why it is not ok, this code will use as much of the cpu cycles as it can, Thread.sleep uses minimal cycles. Unless you're running a system with out any other programs running on it. Thread.sleep is the only answer. This is a newbie mistake but it's a pretty major mistake.

Note: while (current_time > Time_to_sleep_to); where current time is a function call to get the current time in milliseconds is ALSO very wrong.


No one said the code is ok. We are giving the author a pass because he is not expected to know .NET. He's a sysadmin, not a programmer. In other words, get off his back. :)


Sorry, but it's still poor for a sysadmin. Any decent windows sysadmin would know there are umpteen pre-built sleep implementations already available. Microsoft has various server resource kits that come with a variety of handy tools, one of which is sleep. The popular UnixUtils package is a set of unix tools ported to native Win32... one of which is sleep. And (as others have pointed out) Perl itself is also available for Windows platforms in numerous guises.

Re: Getting Better Sleep

2007-03-01 11:28 • by James (unregistered)
The real WTF hasn't happened yet -- it will show up in a few years when the next build magically stops behaving as expected, and after a solid month of troubleshooting somebody discovered that the latest Visual Studio service pack made the compiler smart enough to optimize his spin-lock out of existence.

People, there's a reason we're given a "right" way to do things -- find it and use it!

Re: Getting Better Sleep

2007-03-01 11:35 • by non-judgemental (unregistered)
123885 in reply to 123879
But wait, the cpu needs to have its purpose in life fulfilled; to leave cpu cycles unused is somehow - harmful to the universe.

Seriously, the guy was forced to work with a tool with which he had little or no familiarity. Assuming he was given some sort of deadline (who isn't?), it's probably fair to assume he didn't have much time to go exploring to look up how stuff should be done, or figuring out existing system calls.

We expect programmers to know to look up stuff in code libraries. This guy wasn't a programmer. A management WTF, maybe.

Re: Getting Better Sleep

2007-03-01 11:47 • by ahem (unregistered)
123897 in reply to 123826
If you make a few substitutions in the story, you can see that this is clearly a management WTF of assigning the wrong person to a task, without the proper training:

Reporting from the field, Zak tells us the sad story of a podiatrist forced out of his comfort zone. When you need open heart sugury and your company decides not to hire the correct doctor, the job of performing the operation will almost certainly fall to none other than the podiatrist. This will be the case even if said podiatrist doesn't know cardiac surgury.

When this happens, you will learn two things. The first is that the new heart will be structured as though it were a foot, even where inappropriate. The second is that some body parts that the podiatrist is used to will be unavailable, or simply not appropriate for the task at hand

Re: Getting Better Sleep

2007-03-01 11:59 • by ssprencel
123902 in reply to 123897
ahem:
If you make a few substitutions in the story, you can see that this is clearly a management WTF of assigning the wrong person to a task, without the proper training:

Reporting from the field, Zak tells us the sad story of a podiatrist forced out of his comfort zone. When you need open heart sugury and your company decides not to hire the correct doctor, the job of performing the operation will almost certainly fall to none other than the podiatrist. This will be the case even if said podiatrist doesn't know cardiac surgury.

When this happens, you will learn two things. The first is that the new heart will be structured as though it were a foot, even where inappropriate. The second is that some body parts that the podiatrist is used to will be unavailable, or simply not appropriate for the task at hand


Brilliant. Just brilliant. I don't mean that in a Paula kind of way either.

Re: Getting Better Sleep

2007-03-01 12:01 • by Rich (unregistered)
123903 in reply to 123879
Agreed. There's no way that he should have at least done a quick search on "C# sleep", which brings back a number of results that very plainly show to use Thread.Sleep

Regardless of your level of competency, there's no excuse for not doing really basic research.

Re: Getting Better Sleep

2007-03-01 12:05 • by cklam
123910 in reply to 123826
Tom:
I think the wtf here would have to be converting server scripts to .NET.

Honestly, if perl is the best tool for the job, why re-write the scripts?


Company policy, meddling of eager-beaver consultants, MS-crazy management ... ( many more reasons I'm too lazy to list )

Re: Getting Better Sleep

2007-03-01 12:07 • by PS (unregistered)
123912 in reply to 123825
Sid2K7:
On another note: What happened to TheDailyWTF? Is this the same site? Granted, I haven't been here for a while, but replacing something as "The Daily WTF" (which has a very clear meaning) with the phrase "Worse Than Failure" is a step in the wrong direction.

Then, again, the stories seem to be just as hilarious, so ill be back after my usual interval (1-2 months)


The irony is that you liked the name "The Daily WTF" but then you only visit the site every 1 or 2 months.

Re: Getting Better Sleep

2007-03-01 12:12 • by City Developer (unregistered)
123915 in reply to 123912
PS:
Sid2K7:
On another note: What happened to TheDailyWTF? Is this the same site? Granted, I haven't been here for a while, but replacing something as "The Daily WTF" (which has a very clear meaning) with the phrase "Worse Than Failure" is a step in the wrong direction.

Then, again, the stories seem to be just as hilarious, so ill be back after my usual interval (1-2 months)


The irony is that you liked the name "The Daily WTF" but then you only visit the site every 1 or 2 months.


I like the Daily Telegraph here in the UK but I only buy it on Mondays (for the Sport) and Friday (for the Crossword)

Re: Getting Better Sleep

2007-03-01 12:15 • by Whitey (unregistered)
123917 in reply to 123847
"3) Using a 100% CPU utilization loop to "pause" the system. Hooray. (idiot) "

To me, this is the real WTF. Lets ask the system repeatedly:
what time is it?convert to double, compare.. what time is it?convert to double, compare.. what time is it?convert to double, compare.. what time is it?convert to double, compare..

Remember people, your computer will do everything you ask, as fast as it can, no matter how dumb it is...

captcha: ninjas ...cool...

Re: Getting Better Sleep

2007-03-01 12:26 • by Sam (unregistered)
123926 in reply to 123897
If you make a few substitutions in the story, you can see that this is clearly a management WTF of assigning the wrong person to a task, without the proper training:

Reporting from the field, Zak tells us the sad story of a podiatrist forced out of his comfort zone. When you need open heart sugury and your company decides not to hire the correct doctor, the job of performing the operation will almost certainly fall to none other than the podiatrist. This will be the case even if said podiatrist doesn't know cardiac surgury.

This is a terrible analogy. Writing a system administration script is not akin open heart surgery. A better analogy would be asking a podiatrist to do a routine suture and finding the patient dead of an infection two weeks later.

Part of being a system administrator is writing scripts to manage the system. Don't believe me? Look at the SAGE job descriptions for a Junior System Administrator at http://www.sage.org/field/jobs-descriptions.html Note where it says, "Ability to write scripts in some administrative language."

This is, quite frankly, a stupid script. It would have been a stupid script had it been written similarly in Perl. The correct function to call, "sleep," is the same in both Perl and .NET. And a search on Google for "sleep vb.net" brings up a code sample for the first link, just as doing a search for "sleep perl" brings up the definition of the sleep function in Perl! Finally, after running this script the sysadmin should have seen the CPU peg and realized that it was a stupid script.

I would hope this is a junior sysadmin that was suitably educated in how to use library functions and Google.

Of course, I'm curious as to why there was a requirement to move all system administration scripts from Perl to .NET in the first place. Why change a working a system?

Re: Getting Better Sleep

2007-03-01 12:30 • by Whitey (unregistered)
123932 in reply to 123847
"3) Using a 100% CPU utilization loop to "pause" the system. Hooray. (idiot) "

To me, this is the real WTF. Lets ask the system repeatedly:
what time is it?convert to double, compare.. what time is it?convert to double, compare.. what time is it?convert to double, compare.. what time is it?convert to double, compare..

Remember people, your computer will do everything you ask, as fast as it can, no matter how dumb it is...

captcha: ninjas ...cool...

Re: Getting Better Sleep

2007-03-01 12:39 • by It's a Feature
I hope the CPU doesn't melt running like that!

Re: Getting Better Sleep

2007-03-01 13:18 • by Natrone (unregistered)
123965 in reply to 123827
Good thing VB doesn't stand for Vagina Bitchslap, or you would have to go through the pain of renaming it to something weak.

Re: Getting Better Sleep

2007-03-01 13:28 • by RON (unregistered)
123973 in reply to 123868
Mio:
RON:
The entire function can be shrunk down to this, by the way:


Thread.Sleep( TimeSpan.FromSeconds( seconds ) );


*sigh*


Or

System.Threading.Thread.Sleep(seconds * 1000);


Why bother instantiating a TimeSpan object?


Because

A) it's more readable
B) it doesn't require you to memorize the fact that Sleep requires milliseconds
C) If you're concerned about micro-optimizing a call that will subsequently put the system to sleep for a long time, your priorities are out of whack,
D) and finally, it's a structure type whose only internal value is a single 64-bit integer, and the act of instantiating it is roughly the same amount of work as multiplying a time value by a magic number (1000).

Re: Getting Better Sleep

2007-03-01 13:31 • by RON (unregistered)
123975 in reply to 123869
ssprencel:
Kinglink:
No, the real WTF is the people here thinking this is ok.

To explain why it is not ok, this code will use as much of the cpu cycles as it can, Thread.sleep uses minimal cycles. Unless you're running a system with out any other programs running on it. Thread.sleep is the only answer. This is a newbie mistake but it's a pretty major mistake.

Note: while (current_time > Time_to_sleep_to); where current time is a function call to get the current time in milliseconds is ALSO very wrong.


No one said the code is ok. We are giving the author a pass because he is not expected to know .NET. He's a sysadmin, not a programmer. In other words, get off his back. :)


Normally, when I'm required to work on a system that I have no familiarity with, I ask people who DO know the system, and if I can't find any of those, I use google.

There is no excuse for "but I didn't know!".

The term "C# Pause" in google returned a working solution in the very first link.

Re: Getting Better Sleep

2007-03-01 13:41 • by Edowyth
Ok, so the management shouldn't have asked someone who knew nothing about .NET to start utilizing it without giving them training or earmarked time for learning the environment...

and the sysadmin should have taken a second to google 'C# sleep'...the first result is: http://www.thescripts.com/forum/thread225748.html which answers the question any decent programmer should have asked (which, if he's writing code for the company he should be): "Can I use a sleep command in c#?"

All in all a minor wtf and not one in the code at all (which is what Code Snippet of the Day is supposed to be about!) but, rather, in the story.

This entry is a sad one.

Re: Getting Better Sleep

2007-03-01 13:45 • by Joseph Newton (unregistered)
So, where did this sysadmin learn Perl? Who did he network with? I can't imagine anyone coming from the Perl culture and using that much CamelBack in his code. though the language itself does not place any restrictions on case, fellow Perlites will usually give one a heads-up. The standard--if Perl can be said to have any standard--is that camelback should be reserved for package names.

And yes...Perl does have a sleep function in its core library.

[Well, podna', we gots a right friendly captcha today...]

Re: Getting Better Sleep

2007-03-01 14:15 • by Patrick (unregistered)
124003 in reply to 123832
Will Perdikakis:
The code is actually pretty resourceful not a WTF, really.

He knew what he wanted to do and rather than using the build-in thread function, he used a spin-lock. Lots more wasted CPU cycles, but virtually the same end results.

How many times have you implemented something that already existed in a standard function library only to find out later?


Every day.
« PrevPage 1 | Page 2Next »

Add Comment