Minimal code CPU stress-tester…

Bash and standard utilities, 36 31 22 29 28 26 bytes

yes :|sh&sleep $1;kill $!

Bash/iputils (Linux), 14 bytes

ping6 -fw$1 ::

Flood-pings the IPv6 null address, until the -w deadline timer expires

caveat - only consumes 55-60% CPU on my test VM

Edit: - I retract my caveat. While top reports the ping6 process only consumes 55-60% CPU, I see the total CPU idle percentage (2 core VM) approach zero. This presumably is because a good deal of the processing is going on in the kernel as it handles the packets.

Note - must be run as root. As @Tobia comments, this seems like a reasonable requirement for something that will hog the CPU. And the OP approved it in the comments.


Elf32 standalone binary - 86 bytes

I bet this is the smallest correctly formed Elf format binary that can be made to perform this function. This will execute without any additional support on any linux based platform, or potentially even without an operating system.

Binary download: http://ge.tt/3m6h2cK1/v/0?c

Hex dump:

0000000: 7f45 4c46 0101 0100 0000 0000 0000 0000  .ELF............
0000010: 0200 0300 0100 0000 5480 0408 3400 0000  ........T...4...
0000020: 0000 0000 0000 0000 3400 2000 0100 0000  ........4. .....
0000030: 0000 0000 0100 0000 0000 0000 0080 0408  ................
0000040: 0080 0408 5600 0000 5600 0000 0500 0000  ....V...V.......
0000050: 0010 0000 75fe                           ....u.

This is done by building an asm file with a minimal Elf header of its own, and skipping the use of ld altogether.

Assembly:

BITS 32

              org     0x08048000

ehdr:                                                 ; Elf32_Ehdr
              db      0x7F, "ELF", 1, 1, 1, 0         ;   e_ident
times 8       db      0
              dw      2                               ;   e_type
              dw      3                               ;   e_machine
              dd      1                               ;   e_version
              dd      _start                          ;   e_entry
              dd      phdr - $$                       ;   e_phoff
              dd      0                               ;   e_shoff
              dd      0                               ;   e_flags
              dw      ehdrsize                        ;   e_ehsize
              dw      phdrsize                        ;   e_phentsize
              dw      1                               ;   e_phnum
              dw      0                               ;   e_shentsize
              dw      0                               ;   e_shnum
              dw      0                               ;   e_shstrndx

ehdrsize      equ     $ - ehdr

phdr:                                                 ; Elf32_Phdr
              dd      1                               ;   p_type
              dd      0                               ;   p_offset
              dd      $$                              ;   p_vaddr
              dd      $$                              ;   p_paddr
              dd      filesize                        ;   p_filesz
              dd      filesize                        ;   p_memsz
              dd      5                               ;   p_flags
              dd      0x1000                          ;   p_align

phdrsize      equ     $ - phdr

section .text
global  _start
_start:       jnz     _start

filesize      equ     $ - $$

Built with nasm -f bin tiny_cpu_stresser_elf32.asm -o tiny_cpu_stresser_elf32

Tags:

Code Golf