Consistent CPU benchmarks in Linux provide a repeatable way to compare hardware, place virtual machines, and detect performance regressions after kernel, firmware, or configuration changes. Measurements based on real workloads and stable tools reveal whether available compute resources align with tasks such as compilation, database queries, and parallel build pipelines.

On Linux, benchmarking utilities generate controlled CPU load and collect metrics such as execution time, events per second, and latency. Sysbench focuses on synthetic tests, including a cpu mode that performs intensive prime-number calculations, making it straightforward to stress either a single core or all available threads while capturing reproducible results.

CPU benchmarks place sustained load on the processor, increase power draw, and can raise system temperature, especially on laptops or compact servers. Running benchmarks on production hosts can affect other workloads, so scheduling tests during maintenance windows and monitoring thermal sensors and fan behavior reduces risk. Typical Linux distributions provide the required tools via the terminal and expect basic familiarity with `sudo` and package managers.

Steps to benchmark CPU performance in Linux:

  1. Open a terminal on a Linux system with a user account suitable for running benchmarks.
  2. Install the sysbench package from the distribution package manager if it is not already present.
    $ sudo apt update && sudo apt install --assume-yes sysbench
    $ sudo zypper refresh && sudo zypper install --skip-interactive sysbench
    $ sudo dnf install sysbench
  3. Identify the number of CPU cores and threads in the system using the lscpu command.
    $ lscpu | grep "^CPU.s\|^Thread.s"
    CPU(s):                          8
    Thread(s) per core:              2
  4. Run a test to measure the performance 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

    The command tests the speed of a single CPU core by calculating prime numbers up to the configured limit.

  5. Run a test to measure the performance of multiple CPU cores using a thread count derived from the lscpu output.
    $ 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

    Replace 16 with the desired number of threads based on the available hardware threads reported by lscpu.

  6. Conduct a prolonged benchmark to simulate sustained CPU load over time with multiple threads.
    $ 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 reach thermal and power limits, which may trigger frequency scaling and throttling.

  7. Adjust sysbench options to refine benchmark tests for different scenarios, such as varying runtime, thread counts, or reporting detail.
    $ 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.
  8. Review the reported events per second, total number of events, and latency statistics from each run to compare CPU performance across different thread counts and durations.
Discuss the article:

Comment anonymously. Login not required.