(counterset)
Explore performance counters.

    You'll need to install pywin32 manually beforehand.
Explore performance counters.
Explore performance counters .
def browse(counterset):
    """
    Explore performance counters.

    You'll need to install pywin32 manually beforehand.
    """
    # Leave imports in function to not add the dependencies
    import win32pdh

    if not counterset:
        echo_info('Searching for available countersets:')
        countersets = sorted(win32pdh.EnumObjects(None, None, win32pdh.PERF_DETAIL_WIZARD, True))
        for name in countersets:
            echo_info(name)

        return

    description_prefix = '    Description: '
    description_indent = ' ' * len(description_prefix)

    def display_counter(handle):
        counter_info = win32pdh.GetCounterInfo(handle, True)
        counter_description = counter_info[-1]

        counter_type = counter_info[0]
        if counter_type in COUNTER_TYPES:
            counter_type_name = COUNTER_TYPES[counter_type][0]
        else:
            counter_type_name = 'unknown'

        echo_info(f'--> {counter}')
        echo_info(f'    Type: {counter_type_name}')
        echo_info(description_prefix, nl=False)
        echo_info(textwrap.indent(textwrap.fill(counter_description), description_indent).lstrip())

    query_handle = win32pdh.OpenQuery()

    try:
        header = f'<<< {counterset} >>>'
        echo_info(header)
        echo_info('-' * len(header))
        echo_info('')
        counters, instances = win32pdh.EnumObjectItems(None, None, counterset, win32pdh.PERF_DETAIL_WIZARD)
        counters.sort()

        if instances:
            header = 'Instances'
            echo_info(header)
            echo_info('=' * len(header))
            instance_index = defaultdict(int)
            for instance in instances:
                instance_index[instance] += 1
                echo_info(instance)
            echo_info('')

            header = 'Counters'
            echo_info(header)
            echo_info('=' * len(header))

            for counter in counters:
                for instance, num_instances in instance_index.items():
                    for index in range(num_instances):
                        path = win32pdh.MakeCounterPath((None, counterset, instance, None, index, counter))
                        counter_handle = win32pdh.AddCounter(query_handle, path)
                        display_counter(counter_handle)

                        # Only need information from one instance
                        break
                    break
        else:
            header = 'Counters'
            echo_info(header)
            echo_info('=' * len(header))

            for counter in counters:
                path = win32pdh.MakeCounterPath((None, counterset, None, None, 0, counter))
                if win32pdh.ValidatePath(path) != 0:
                    echo_info(f'--> {counter}')
                    echo_info('    Error: no current instances')
                    continue

                counter_handle = win32pdh.AddCounter(query_handle, path)
                display_counter(counter_handle)
    finally:
        win32pdh.CloseQuery(query_handle)
def browse ( counterset ) : # Leave imports in function to not add the dependencies import win32pdh if not counterset : echo_info ( 'Searching for available countersets:' ) countersets = sorted ( win32pdh . EnumObjects ( None , None , win32pdh . PERF_DETAIL_WIZARD , True ) ) for name in countersets : echo_info ( name ) return description_prefix = '    Description: ' description_indent = ' ' * len ( description_prefix ) def display_counter ( handle ) : counter_info = win32pdh . GetCounterInfo ( handle , True ) counter_description = counter_info [ - 1 ] counter_type = counter_info [ 0 ] if counter_type in COUNTER_TYPES : counter_type_name = COUNTER_TYPES [ counter_type ] [ 0 ] else : counter_type_name = 'unknown' echo_info ( f'--> {counter}' ) echo_info ( f'    Type: {counter_type_name}' ) echo_info ( description_prefix , nl = False ) echo_info ( textwrap . indent ( textwrap . fill ( counter_description ) , description_indent ) . lstrip ( ) ) query_handle = win32pdh . OpenQuery ( ) try : header = f'<<< {counterset} >>>' echo_info ( header ) echo_info ( '-' * len ( header ) ) echo_info ( '' ) counters , instances = win32pdh . EnumObjectItems ( None , None , counterset , win32pdh . PERF_DETAIL_WIZARD ) counters . sort ( ) if instances : header = 'Instances' echo_info ( header ) echo_info ( '=' * len ( header ) ) instance_index = defaultdict ( int ) for instance in instances : instance_index [ instance ] += 1 echo_info ( instance ) echo_info ( '' ) header = 'Counters' echo_info ( header ) echo_info ( '=' * len ( header ) ) for counter in counters : for instance , num_instances in instance_index . items ( ) : for index in range ( num_instances ) : path = win32pdh . MakeCounterPath ( ( None , counterset , instance , None , index , counter ) ) counter_handle = win32pdh . AddCounter ( query_handle , path ) display_counter ( counter_handle ) # Only need information from one instance break break else : header = 'Counters' echo_info ( header ) echo_info ( '=' * len ( header ) ) for counter in counters : path = win32pdh . MakeCounterPath ( ( None , counterset , None , None , 0 , counter ) ) if win32pdh . ValidatePath ( path ) != 0 : echo_info ( f'--> {counter}' ) echo_info ( '    Error: no current instances' ) continue counter_handle = win32pdh . AddCounter ( query_handle , path ) display_counter ( counter_handle ) finally : win32pdh . CloseQuery ( query_handle )