measure the performance of pymutex and pythreadtypelock locks with short critical sections usage python toolslockbenchlockbench py criticalsectionlength how to interpret the results acquisitions khz reports the total number of lock acquisitions in thousands of acquisitions per second this is the most important metric particularly for the 1 thread case because even in multithreaded programs most locks acquisitions are not contended values for 2 threads are only meaningful for disablegil builds because the gil prevents most situations where there is lock contention with short critical sections fairness a measure of how evenly the lock acquisitions are distributed a fairness of 1 0 means that all threads acquired the lock the same number of times a fairness of 1n means that only one thread ever acquired the lock see https en wikipedia orgwikifairnessmeasurejain sfairnessindex max number of threads to test how much work to do while holding the lock jain s fairness index see https en wikipedia orgwikifairnessmeasure measure the performance of pymutex and pythread_type_lock locks with short critical sections usage python tools lockbench lockbench py critical_section_length how to interpret the results acquisitions khz reports the total number of lock acquisitions in thousands of acquisitions per second this is the most important metric particularly for the 1 thread case because even in multithreaded programs most locks acquisitions are not contended values for 2 threads are only meaningful for disable gil builds because the gil prevents most situations where there is lock contention with short critical sections fairness a measure of how evenly the lock acquisitions are distributed a fairness of 1 0 means that all threads acquired the lock the same number of times a fairness of 1 n means that only one thread ever acquired the lock see https en wikipedia org wiki fairness_measure jain s_fairness_index max number of threads to test how much work to do while holding the lock jain s fairness index see https en wikipedia org wiki fairness_measure report in khz for readability
from _testinternalcapi import benchmark_locks
import sys


MAX_THREADS = 10


CRITICAL_SECTION_LENGTH = 1


def jains_fairness(values):
    
    
    return (sum(values) ** 2) / (len(values) * sum(x ** 2 for x in values))

def main():
    print("Lock Type           Threads           Acquisitions (kHz)   Fairness")
    for lock_type in ["PyMutex", "PyThread_type_lock"]:
        use_pymutex = (lock_type == "PyMutex")
        for num_threads in range(1, MAX_THREADS + 1):
            acquisitions, thread_iters = benchmark_locks(
                num_threads, use_pymutex, CRITICAL_SECTION_LENGTH)

            acquisitions /= 1000  
            fairness = jains_fairness(thread_iters)

            print(f"{lock_type: <20}{num_threads: <18}{acquisitions: >5.0f}{fairness: >20.2f}")


if __name__ == "__main__":
    if len(sys.argv) > 1:
        CRITICAL_SECTION_LENGTH = int(sys.argv[1])
    main()