Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

one-liner hogs

by jptxs (Curate)
on Feb 01, 2001 at 03:21 UTC ( [id://55592]=perlquestion: print w/replies, xml ) Need Help??

jptxs has asked for the wisdom of the Perl Monks concerning the following question:

The company I work for has asked me to come up with some neat perl 5.004_04 (or lower) one-liners that use no modules and eat a lot of resources. We sell performance management software and are seeking ways to create some artificial load. I hadn't thought to ask in here until i saw eval + no warnings = no memory.

They would need to be one-liners so they could be basically memorized by engineers who don't know perl and used to demo stuff on site. (we create the load to show our software can detect it and do something about it)

All the boxes we'd want to load would be Unix (Sun, Linux, HP, AIX in order of likelihood) and would already have agreed to grant us root access. Any good ones monks?

"A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche

Replies are listed 'Best First'.
(tye)Re: one-liner hogs
by tye (Sage) on Feb 01, 2001 at 04:16 UTC
    Memory: perl -e 'push @_,"x"x1024 while 1' CPU: perl -e '$_++ while 1' Floats: perl -e '$_= ($_*76543+23456789) % 1234567891 while 1' Procs: perl -e 'fork() while 1' Procs2: perl -e '$_=2;while(1){sleep($_+=fork?$_:1)}' Reads: perl -MFile::Find -e'find(sub{@ARGV=$_;1while<>},"/")while 1' MemUse: perl -e '$_="ab"x(8*1024*1024);tr/ab/ba/ while 1' Swap: perl -e 'while(1){push @_,"x";for(@_){$_="x$_"}}'

    To double the load, prepend "fork;" to the line. For example, four-times as much "read" load can be (attempted to be) generated via:

    perl -MFile::Find -e 'fork;fork;find(sub{@ARGV=$_;1while<>},"/")while +1'

    My favorite is "Procs2", which is the only one I tested; most of these you don't want to test when anyone else is using the computer. (:

            - tye (but my friends call me "Tye")
      Everyone's responses have been awesome, but there might be the winners because they're so simple. I need non-programmer types to be able to memorize and spit these out.

      actually Kanji's suggestion seemed so good there's a box 3000 miles away in California being rebooted as we speak =) oops.

      "A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche
        Sorry. Just saw your signature line and noticed it was from Nietzsche. I saw a pretty funny bumper sticker the other day that read:
        God is dead. - Nietzsche.
        Nietzsche is dead. - God.
      Nice Tye, but I honestly can't see myself memorising your floats one :)


        Okay, I spent a bit more time and found 3 prime numbers that are easier to remember. I updated the original message changing:

        Floats: perl -e '$_= ($_*36217+41081) % 492391271 while 1'

        to

        Floats: perl -e '$_= ($_*76543+23456789) % 1234567891 while 1'
        Now that is much easier to remember. Thanks!

                - tye (but my friends call me "Tye")
Re: one-liner hogs
by Trinary (Pilgrim) on Feb 01, 2001 at 03:37 UTC
    Very basic one:
    perl -e 'while (++$i) { $foo[$i] = $i; }'
    That'll chew memory like mad, and soon bomb out. CPU usage minimal.

    of course, you can make it use CPU...

    perl -e 'fork; fork; while (++$i) { $c .= crypt($i,$i); $c = substr($c +,4,128); }
    Ctrl-C kills all children, no root required! Load should fly up to around 4, give it a minute or two. To add mem usage, change the substr length.

    Fun request!

    Trinary

      My humble contribution: while (++$i) { $foo{$i}=[ @foo ]; $foo[$i]=[ %foo, $i ]; } bounces back and forth making new arrays of ever-increasing size. Allocated about 350MB in about 30 secs. Uses a lot of processor, though.

      Agreed. Quite fun deliberately pessimizing code.

Re: one-liner hogs
by Kanji (Parson) on Feb 01, 2001 at 03:32 UTC

    I was always rather fond of perl -e 'fork() while 1'.

        --k.


      I guy in my OS class ran something similar to that and logged out of the main CS server without killing it. A short time later, the whole lab wondered why they couldn't save their assignments . . .

      Needless to say he had a lot of explaining to do with the IT staff. And that was the LAST time the OS classes used the main CS server!

      Jeff

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      F--F--F--F--F--F--F--F--
      (the triplet paradiddle)
      
      Actually, there's a slight problem with the old fork() bomb: most OSes only allow so many running processes and there are a few that go so far as to limit the number of running processes an individual user can have, so you could, possibly, run out of processes before you run out of memory.
      ~Cybercosis
      Update: Actually, it is somewhat required for an OS to limit the number of possible processes, since you can only hold so many TSS templates to be moved into the TSS register in memory. Well, at least with i86 processors. =-)
Re: one-liner hogs
by arturo (Vicar) on Feb 01, 2001 at 03:24 UTC

    Well, eating memory's easy enough to do, figuring out how to do it efficiently (is that an oxymoron? No, we're talking about time efficiency) is another ...

    perl -e 'while (1) { push @stuff, "all work and no play makes arturo a + dull boy" }'

    What do ya wanna test, memory hoggage, CPU hoggage, filesystem hoggage?

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      good question =) I should have made that more clear.

      It would be great to have a different one-liner for each. The memory one you give is an awesome start =)

      "A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche
Re: one-liner hogs
by extremely (Priest) on Feb 01, 2001 at 03:59 UTC
    How about this for processor churn?
    perl -e '$_="abcd"x100000;while(1){y/abcd/bcda/}'
    perl -e '@_=0..1000000;while(1){foreach(@_){++$_,--$_}}'
    perl -e '@_=0..100000;while(1){foreach(@_){$a=$_/++$_;--$_}}'

    The first exercises about about 8MB of memory fairly heavily.
    The second beats up 64MB of memory with light mathops.
    The third eats 9MB with a heavier math op.

    Obviously you can tweak how much memory is eaten with monster number in there... HTH.

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re: one-liner hogs
by jepri (Parson) on Feb 01, 2001 at 10:24 UTC
    Probably a bit late now, but

    perl -e'print "a".."ZZZZZZZZZZZZ"'
    will drop a Linux box most times. It's deceptive too. You get just enough time to say "That's stupid, it didn't work at all" and then it bites... :)

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

      Nice. I like it! ++jepri. I couldn't help golfing it down to:

      perl -e"@_=a..z.~0"
              - tye (but my friends call me "Tye")
      I like it, it maxed out both my CPU and memory more than some of the others. Oops.. wasn't I meant to try? =)

**Palindromic** one-liner hog :)
by MeowChow (Vicar) on Feb 01, 2001 at 12:36 UTC
    lave,,,,s,,reverse;eval;lave;esrever,,s,,,,eval
    Ok, so the reverse doesn't really have to be in there, but i think it's cheating if any part of the palindromic code isn't actually executed :-)
       MeowChow                                               
                    print $/='"',(`$^X\144oc $^X\146aq1`)[-2]
      Funny, I just realized I've written, like, two other nodes to do this very thing within the last two weeks...

      We Perl Monks sure know how to abuse a language, eh?

Re: one-liner hogs
by jynx (Priest) on Feb 01, 2001 at 04:01 UTC

    And how about one for disk hoggage:
    perl -e '(open T "/tmp/f.$t" and print T "disk\n"x256; close T) while +(++$t);'
    That should work. If not a little variation should be close enough. There are probably more efficient ways to do this, but that's the first thing off the top of my head...

    HTH,
    jynx

      For disc overflows:

      not perl, but works:

      cat /dev/zero > /tmp/.#overflow#

      Of course, could also be

      perl -e 'open T,">/tmp/.#overflow#";open F,"</dev/zero";while(<F>){pri +nt T;};'

      Or the 'smarter'(?) way to completely overflow a machine (but probably only as root):

      perl -e 'open M,"/sbin/mount|grep -v':'";while(<M>){($m)=split($_);if( +fork){open Z,"</dev/zero";open F,">$m/.#overflow#";while(<Z>){print F +}}};'

      But that's WAY too much to memorize, da?

        Ah, I think you've accidentally stumbled upon an obfuscated hog of a different kind. Consider:

        open(ZERO,"</dev/zero") or die $!; my $x= <ZERO>;
        I think I'll let someone else explain what that does so people have a little time to think about it. (:

                - tye (but my friends call me "Tye")
Re: one-liner hogs
by quidity (Pilgrim) on Feb 01, 2001 at 17:25 UTC

    You could always eat all your file descriptors, not quite a one liner, but try running a web server at the same time as this little sucker. This is why clever persons use per process limitations.

    #!/usr/bin/perl for (;;) { my $fh; eval {open($fh, "foo.pl")}; last if $!; push @foo, $fh; } print "all out $!\n"; sleep(1000);
Re: one-liner hogs
by ftforger (Sexton) on Feb 01, 2001 at 21:23 UTC
    jptxs: Your suggestion of "one liners" seems suspect to me. Why would these be better than having a perl programmer write several of these into a small program. Your engineers run one program and select the type load they want to test, or demo. If done properly you could combine several of these to run at the same time, while keeping the ability to kill it at will. (That would be a good idea in any event).
      this would be ideal if these could be put on some sort of media or on and ftp site for the engineers to get to. However, the people who will use this will be sales-engineers who are not always garunteed access to the outside world in the environments they work in and many times find the client unwilling to mount anything but specified disks with the company logo and all.

      basically, the reason i wanted "one-liners" was so they could be commited to memory or even carried around on a palm or index card with ease. This way there is no network or hardware access issues that could hamper them.

      "A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche
        I realized what you were getting at originally, but I would suggest putting this kind of a test/demo suite together on the same disk you give the sales people. It can carry the company logo etc. Its NOT identical to the disk you sell, of course the only real difference is the test/demo suite. Another option would be to create a specific sales/demo disk with the full company logo on it.
Re: one-liner hogs
by unimatrix (Initiate) on Feb 01, 2001 at 15:01 UTC
    perl -e'fork while fork'
Re: one-liner hogs
by Ranna (Acolyte) on Oct 22, 2001 at 11:02 UTC
    Windows is mean, it runs all of the memory-hog one-liners and spits out a message saying 'Out of memory!' without dying fantastically like I've come to expect from it (such as when VMWare took over my computer...)
Re: one-liner hogs
by letual (Initiate) on Feb 02, 2001 at 02:26 UTC
    Memory hog: perl -e '$foo[10000000]="hog"; sleep(10)'

    increase the array index to something bigger to take more (or less memory)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://55592]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found