diff options
author | Michael Brown | 2012-09-26 15:54:02 +0200 |
---|---|---|
committer | Michael Brown | 2012-09-26 15:54:02 +0200 |
commit | 681a219caaa5f9379641232157cc0b38e64a839d (patch) | |
tree | 3fa186320f8f1a6bdee04edfb2fd42a09666c515 /src/tests/digest_test.c | |
parent | [efi] Provide guaranteed space in transmitted packets (diff) | |
download | ipxe-681a219caaa5f9379641232157cc0b38e64a839d.tar.gz ipxe-681a219caaa5f9379641232157cc0b38e64a839d.tar.xz ipxe-681a219caaa5f9379641232157cc0b38e64a839d.zip |
[test] Add speed tests for digest algorithms
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/tests/digest_test.c')
-rw-r--r-- | src/tests/digest_test.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/tests/digest_test.c b/src/tests/digest_test.c index ccec0e9a..6428cc72 100644 --- a/src/tests/digest_test.c +++ b/src/tests/digest_test.c @@ -25,8 +25,10 @@ FILE_LICENCE ( GPL2_OR_LATER ); * */ +#include <stdlib.h> #include <string.h> #include <ipxe/crypto.h> +#include <ipxe/profile.h> #include "digest_test.h" /** @@ -68,3 +70,36 @@ int digest_test ( struct digest_algorithm *digest, /* Compare against expected output */ return ( memcmp ( expected, out, sizeof ( out ) ) == 0 ); } + +/** + * Calculate digest algorithm cost + * + * @v digest Digest algorithm + * @ret cost Cost (in cycles per byte) + */ +unsigned long digest_cost ( struct digest_algorithm *digest ) { + static uint8_t random[8192]; /* Too large for stack */ + uint8_t ctx[digest->ctxsize]; + uint8_t out[digest->digestsize]; + union profiler profiler; + unsigned long long elapsed; + unsigned long cost; + unsigned int i; + + /* Fill buffer with pseudo-random data */ + srand ( 0x1234568 ); + for ( i = 0 ; i < sizeof ( random ) ; i++ ) + random[i] = rand(); + + /* Time digest calculation */ + profile ( &profiler ); + digest_init ( digest, ctx ); + digest_update ( digest, ctx, random, sizeof ( random ) ); + digest_final ( digest, ctx, out ); + elapsed = profile ( &profiler ); + + /* Round to nearest whole number of cycles per byte */ + cost = ( ( elapsed + ( sizeof ( random ) / 2 ) ) / sizeof ( random ) ); + + return cost; +} |