stat speed

Mark Bucciarelli mark at gaiahost.coop
Sat Feb 18 20:06:56 PST 2006


I'm curious how fast stat is.

I generated a list of 200,000 file names

    # find / | head -200000 > files.statspeed

then ran a million iterations of randomly picking a file name and
stating it (see attached program).

The run times were pretty consistent:

    187,422 stats/second
    189,059
    189,567
    189,894

Are these numbers a meaningful measure of stat speed on my particular
machine?  If not, how can I improve the test?

m
-------------- next part --------------
// statspeed.c
//
// Mark Bucciarelli 
// 2006-02-18
//
// Read in a long list of file names.
// Randomly pick one, stat it, randomly pick another, stat 
// it, etc.
//
// Use a big list of files to try and avoid file system caching.
//

#include <sys/time.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>


// $ find / | head 200000 > files.statspeed
#define fname "files.statspeed"
#define maxfiles 200000
#define maxchars maxfiles * FILENAME_MAX  // 1024 on FreeBSD
#define statcalln 1000000

#define timeval2double(tv) (double)tv.tv_sec + (double)tv.tv_usec/1000000.0;


char space[ maxchars ];

int main( long arvc, char* argv[] )
{
    struct timeval t1, t2;
    struct timezone tz;
    double t1d, t2d;
    char *files[maxfiles];
    FILE *f;

    gettimeofday(&t1, &tz);

    // load list of file names
    f = fopen( fname, "r" );
    if ( !f )
    {
        printf( "Couldn't open file '%s'.\n", fname );
        return 1;
    }
    char *p = space;
    long charn = 0;
    long filen = 0;
    while ( (charn + FILENAME_MAX ) < maxchars 
            && filen < maxfiles 
            && fgets( p, FILENAME_MAX, f ) ) 
    {
        if ( p[strlen(p)-1] == '\n' ) p[strlen(p)-1]='\0';
        charn += strlen( p );
        files[filen++] = p;
        p += strlen(p);
    }
    fclose( f );

    // msg if too many file names
    if ( (charn + FILENAME_MAX) >= maxchars )
    {
        printf( "%s has too many characters (%d > %d)\n", fname, charn, maxchars );
        return 1;
    }
    if ( filen > maxfiles )
    {
        printf( "%s has too many files (%d > %d)\n", fname, filen, maxfiles );
        return 1;
    }
    gettimeofday(&t2, &tz);
    t1d = timeval2double( t1 );
    t2d = timeval2double( t2 );
    printf( "%5.2f seconds for setup\n", t2d - t1d );

    // stat files
    double r;
    long filei;
    long i;
    struct stat sb;
    gettimeofday(&t1, &tz);
    for ( i = 0; i < statcalln; i++ )
    {
        r = ( (double) rand()) / ((double) RAND_MAX );
        filei = (long) ( r * filen );
        stat( files[filei], &sb );
    }
    gettimeofday(&t2, &tz);
    t1d = timeval2double( t1 );
    t2d = timeval2double( t2 );

    // output results
    printf( "%5.2f seconds for %d stat calls\n", t2d - t1d, statcalln );
    if ( t2d - t1d > 0 )
        printf( "%d calls/second\n", (long) (statcalln / ( t2d - t1d ) ) );
    else
        printf( "Cain't touch this, da da da dum, da DUM, da DUM cain't touch this.\n" );

    return 0;
}


More information about the freebsd-performance mailing list