engine: common: base_cmd: add a simple benchmark within basecmd_test command

This commit is contained in:
Alibek Omarov 2023-03-13 02:40:48 +03:00
parent 8e45a43ad2
commit bcbd1a59c6

View File

@ -238,23 +238,27 @@ void BaseCmd_Stats_f( void )
if( len > maxsize ) if( len > maxsize )
maxsize = len; maxsize = len;
} }
Con_Printf( "Base command stats:\n"); Con_Printf( "min length: %d, max length: %d, empty: %d\n", minsize, maxsize, empty );
Con_Printf( "Bucket minimal length: %d\n", minsize );
Con_Printf( "Bucket maximum length: %d\n", maxsize );
Con_Printf( "Empty buckets: %d\n", empty );
} }
typedef struct
{
qboolean valid;
int lookups;
} basecmd_test_stats_t;
static void BaseCmd_CheckCvars( const char *key, const char *value, const void *unused, void *ptr ) static void BaseCmd_CheckCvars( const char *key, const char *value, const void *unused, void *ptr )
{ {
base_command_t *v = BaseCmd_Find( HM_CVAR, key ); basecmd_test_stats_t *stats = ptr;
qboolean *invalid = ptr;
if( !v ) stats->lookups++;
if( !BaseCmd_Find( HM_CVAR, key ))
{ {
Con_Printf( "Cvar %s is missing in basecmd\n", key ); Con_Printf( "Cvar %s is missing in basecmd\n", key );
*invalid = true; stats->valid = false;
} }
} }
@ -267,38 +271,51 @@ testing order matches cbuf execute
*/ */
void BaseCmd_Test_f( void ) void BaseCmd_Test_f( void )
{ {
void *cmd; basecmd_test_stats_t stats;
double start, end, dt;
int i;
stats.valid = true;
stats.lookups = 0;
start = Sys_DoubleTime() * 1000;
for( i = 0; i < 1000; i++ )
{
cmdalias_t *a; cmdalias_t *a;
qboolean invalid = false; void *cmd;
// Cmd_LookupCmds don't allows to check alias, so just iterate // Cmd_LookupCmds don't allows to check alias, so just iterate
for( a = Cmd_AliasGetList(); a; a = a->next ) for( a = Cmd_AliasGetList(); a; a = a->next, stats.lookups++ )
{ {
base_command_t *v = BaseCmd_Find( HM_CMDALIAS, a->name ); if( !BaseCmd_Find( HM_CMDALIAS, a->name ))
if( !v )
{ {
Con_Printf( "Alias %s is missing in basecmd\n", a->name ); Con_Printf( "Alias %s is missing in basecmd\n", a->name );
invalid = true; stats.valid = false;
} }
} }
for( cmd = Cmd_GetFirstFunctionHandle(); cmd; for( cmd = Cmd_GetFirstFunctionHandle(); cmd;
cmd = Cmd_GetNextFunctionHandle( cmd ) ) cmd = Cmd_GetNextFunctionHandle( cmd ), stats.lookups++ )
{ {
base_command_t *v = BaseCmd_Find( HM_CMD, Cmd_GetName( cmd ) ); if( !BaseCmd_Find( HM_CMD, Cmd_GetName( cmd )))
if( !v )
{ {
Con_Printf( "Command %s is missing in basecmd\n", Cmd_GetName( cmd )); Con_Printf( "Command %s is missing in basecmd\n", Cmd_GetName( cmd ));
invalid = true; stats.valid = false;
} }
} }
Cvar_LookupVars( 0, NULL, &invalid, (setpair_t)BaseCmd_CheckCvars ); Cvar_LookupVars( 0, NULL, &stats.valid, (setpair_t)BaseCmd_CheckCvars );
}
if( !invalid ) end = Sys_DoubleTime() * 1000;
{
dt = end - start;
if( !stats.valid )
Con_Printf( "BaseCmd is valid\n" ); Con_Printf( "BaseCmd is valid\n" );
}
Con_Printf( "Test took %.3f ms, %d lookups, %.3f us/lookup\n", dt, stats.lookups, dt / stats.lookups * 1000 );
BaseCmd_Stats_f();
} }