CPU cycle speed used to define how fast a CPU is. Nowadays, it's no longer a perfect measure as different CPU architectures and manufacturers add optimizations to their CPUs that benchmarks are required to test how fast the CPU actually is.

CPU benchmark programs are created to run tasks on the CPUs and measure how long it takes for the CPUs to finish running. These tasks sometimes are purely synthetic, while some try to emulate real-world usage. In any case, it could never fully reflect how a regular person uses the CPUs.

While many third-party, commercial applications are available to perform CPU benchmarks, sysbench is a standard tool to run benchmarks in a Linux system where it's available in most distribution's default package management system's repository.

Steps to benchmark CPU speed in Linux:

  1. Launch terminal application.
  2. Install sysbench package for your system.
    $ sudo apt update && sudo apt install --assume-yes sysbench #Ubuntu and variants
    > sudo zypper refresh && sudo zypper install --skip-interactive sysbench
    $ sudo dnf install sysbench
  3. Check the number of CPU cores and threads are available in the system.
    $ lscpu | grep "^CPU.s\|^Thread.s"
    CPU(s):                          8
    Thread(s) per core:              2
  4. Benchmark speed of a single CPU core.
    $ sysbench cpu run
    sysbench 1.0.20 (using system LuaJIT 2.1.0-beta3)
    
    Running the test with following options:
    Number of threads: 1
    Initializing random number generator from current time
    
    
    Prime numbers limit: 10000
    
    Initializing worker threads...
    
    Threads started!
    
    CPU speed:
        events per second:  8651.52
    
    General statistics:
        total time:                          10.0002s
        total number of events:              86521
    
    Latency (ms):
             min:                                    0.11
             avg:                                    0.12
             max:                                    1.21
             95th percentile:                        0.12
             sum:                                 9987.69
    
    Threads fairness:
        events (avg/stddev):           86521.0000/0.00
        execution time (avg/stddev):   9.9877/0.00
  5. Benchmark speed of multiple CPU cores or thread.
    $ sysbench cpu --threads=16 run
    sysbench 1.0.20 (using system LuaJIT 2.1.0-beta3)
    
    Running the test with following options:
    Number of threads: 16
    Initializing random number generator from current time
    
    
    Prime numbers limit: 10000
    
    Initializing worker threads...
    
    Threads started!
    
    CPU speed:
        events per second: 16608.45
    
    General statistics:
        total time:                          10.0006s
        total number of events:              166101
    
    Latency (ms):
             min:                                    0.11
             avg:                                    0.96
             max:                                   68.15
             95th percentile:                        0.13
             sum:                               159504.98
    
    Threads fairness:
        events (avg/stddev):           10381.3125/69.32
        execution time (avg/stddev):   9.9691/0.02
  6. Run benchmark for longer time to cause sustained CPU load.
    $ sysbench cpu --threads=16 --time=60 run
    sysbench 1.0.20 (using system LuaJIT 2.1.0-beta3)
    
    Running the test with following options:
    Number of threads: 16
    Initializing random number generator from current time
    
    
    Prime numbers limit: 10000
    
    Initializing worker threads...
    
    Threads started!
    
    CPU speed:
        events per second: 16649.33
    
    General statistics:
        total time:                          60.0007s
        total number of events:              998978
    
    Latency (ms):
             min:                                    0.11
             avg:                                    0.96
             max:                                  101.62
             95th percentile:                        0.13
             sum:                               958670.12
    
    Threads fairness:
        events (avg/stddev):           62436.1250/236.58
        execution time (avg/stddev):   59.9169/0.03

    Longer benchmarks can cause the CPU to hit and emulate frequency scaling and thermal limitations.

  7. Optimize testing by utilizing more options for sysbench.
    $ sysbench --help
    Usage:
      sysbench [options]... [testname] [command]
    
    Commands implemented by most tests: prepare run cleanup help
    
    General options:
      --threads=N                     number of threads to use [1]
      --events=N                      limit for total number of events [0]
      --time=N                        limit for total execution time in seconds [10]
      --forced-shutdown=STRING        number of seconds to wait after the --time limit before forcing shutdown, or 'off' to disable [off]
      --thread-stack-size=SIZE        size of stack per thread [64K]
      --rate=N                        average transactions rate. 0 for unlimited rate [0]
      --report-interval=N             periodically report intermediate statistics with a specified interval in seconds. 0 disables intermediate reports [0]
      --report-checkpoints=[LIST,...] dump full statistics and reset all counters at specified points in time. The argument is a list of comma-separated values representing the amount of time in seconds elapsed from start of test when report checkpoint(s) must be performed. Report checkpoints are off by default. []
      --debug[=on|off]                print more debugging info [off]
      --validate[=on|off]             perform validation checks where possible [off]
      --help[=on|off]                 print help and exit [off]
      --version[=on|off]              print version and exit [off]
      --config-file=FILENAME          File containing command line options
      --tx-rate=N                     deprecated alias for --rate [0]
      --max-requests=N                deprecated alias for --events [0]
      --max-time=N                    deprecated alias for --time [0]
      --num-threads=N                 deprecated alias for --threads [1]
    
    Pseudo-Random Numbers Generator options:
      --rand-type=STRING random numbers distribution {uniform,gaussian,special,pareto} [special]
      --rand-spec-iter=N number of iterations used for numbers generation [12]
      --rand-spec-pct=N  percentage of values to be treated as 'special' (for special distribution) [1]
      --rand-spec-res=N  percentage of 'special' values to use (for special distribution) [75]
      --rand-seed=N      seed for random number generator. When 0, the current time is used as a RNG seed. [0]
      --rand-pareto-h=N  parameter h for pareto distribution [0.2]
    
    Log options:
      --verbosity=N verbosity level {5 - debug, 0 - only critical messages} [3]
    
      --percentile=N       percentile to calculate in latency statistics (1-100). Use the special value of 0 to disable percentile calculations [95]
      --histogram[=on|off] print latency histogram in report [off]
    
    General database options:
    
      --db-driver=STRING  specifies database driver to use ('help' to get list of available drivers) [mysql]
      --db-ps-mode=STRING prepared statements usage mode {auto, disable} [auto]
      --db-debug[=on|off] print database-specific debug information [off]
    
    
    Compiled-in database drivers:
      mysql - MySQL driver
      pgsql - PostgreSQL driver
    
    mysql options:
      --mysql-host=[LIST,...]          MySQL server host [localhost]
      --mysql-port=[LIST,...]          MySQL server port [3306]
      --mysql-socket=[LIST,...]        MySQL socket
      --mysql-user=STRING              MySQL user [sbtest]
      --mysql-password=STRING          MySQL password []
      --mysql-db=STRING                MySQL database name [sbtest]
      --mysql-ssl[=on|off]             use SSL connections, if available in the client library [off]
      --mysql-ssl-cipher=STRING        use specific cipher for SSL connections []
      --mysql-compression[=on|off]     use compression, if available in the client library [off]
      --mysql-debug[=on|off]           trace all client library calls [off]
      --mysql-ignore-errors=[LIST,...] list of errors to ignore, or "all" [1213,1020,1205]
      --mysql-dry-run[=on|off]         Dry run, pretend that all MySQL client API calls are successful without executing them [off]
    
    pgsql options:
      --pgsql-host=STRING     PostgreSQL server host [localhost]
      --pgsql-port=N          PostgreSQL server port [5432]
      --pgsql-user=STRING     PostgreSQL user [sbtest]
      --pgsql-password=STRING PostgreSQL password []
      --pgsql-db=STRING       PostgreSQL database name [sbtest]
    
    Compiled-in tests:
      fileio - File I/O test
      cpu - CPU performance test
      memory - Memory functions speed test
      threads - Threads subsystem performance test
      mutex - Mutex performance test
    
    See 'sysbench <testname> help' for a list of options for each test.
Discuss the article:

Comment anonymously. Login not required.