SHA* digest function speed test.
BUG=1210
TEST=This is a test,
Review URL: http://codereview.chromium.org/596079
diff --git a/tests/Makefile b/tests/Makefile
index 4d5f969..04df8b1 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -3,16 +3,19 @@
# found in the LICENSE file.
CC ?= gcc
-CFLAGS = -Wall -ansi
+CFLAGS = -Wall
INCLUDES ?= -I../include/
TOP ?= ../
-LIBS = $(TOP)/crypto/libcrypto.a $(TOP)/common/libcommon.a
+LIBS = $(TOP)/crypto/libcrypto.a $(TOP)/common/libcommon.a -lrt
-tests: sha_tests
+tests: sha_tests sha_benchmark
sha_tests: sha_tests.c
$(CC) $(CFLAGS) -DNDEBUG $(INCLUDES) $< -o $@ $(LIBS)
+sha_benchmark: sha_benchmark.c timer_utils.c
+ $(CC) $(CFLAGS) -DNDEBUG $(INCLUDES) $^ -o $@ $(LIBS)
+
clean:
- rm -f sha_tests
+ rm -f sha_tests sha_benchmark
diff --git a/tests/sha_benchmark.c b/tests/sha_benchmark.c
new file mode 100644
index 0000000..bc712ad
--- /dev/null
+++ b/tests/sha_benchmark.c
@@ -0,0 +1,55 @@
+/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "sha.h"
+#include "timer_utils.h"
+#include "utility.h"
+
+#define NUM_HASH_ALGORITHMS 3
+#define TEST_BUFFER_SIZE 4000000
+
+/* Table of hash function pointers and their description. */
+typedef uint8_t* (*Hashptr) (const uint8_t*, int, uint8_t*);
+typedef struct HashFxTable {
+ Hashptr hash;
+ char* description;
+} HashFxTable;
+
+HashFxTable hash_functions[NUM_HASH_ALGORITHMS] = {
+ {SHA1, "SHA1"},
+ {SHA256, "SHA256"},
+ {SHA512, "SHA512"}
+};
+
+int main(int argc, char* argv[]) {
+ int i;
+ double speed;
+ uint32_t msecs;
+ uint8_t* buffer = (uint8_t*) Malloc(TEST_BUFFER_SIZE);
+ uint8_t* digest = (uint8_t*) Malloc(SHA512_DIGEST_SIZE); /* Maximum size of
+ * the digest. */
+ ClockTimerState ct;
+
+ /* Iterate through all the hash functions. */
+ for(i = 0; i < NUM_HASH_ALGORITHMS; i++) {
+ StartTimer(&ct);
+ hash_functions[i].hash(buffer, TEST_BUFFER_SIZE, digest);
+ StopTimer(&ct);
+
+ msecs = GetDurationMsecs(&ct);
+ speed = ((TEST_BUFFER_SIZE / 10e6)
+ / (msecs / 10e3)); /* Mbytes/sec */
+
+ fprintf(stderr, "%s Time taken = %u ms, Speed = %f Mbytes/sec\n",
+ hash_functions[i].description, msecs, speed);
+ }
+
+ Free(digest);
+ Free(buffer);
+ return 0;
+}
diff --git a/tests/timer_utils.c b/tests/timer_utils.c
new file mode 100644
index 0000000..015f6db
--- /dev/null
+++ b/tests/timer_utils.c
@@ -0,0 +1,24 @@
+/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "timer_utils.h"
+
+void StartTimer(ClockTimerState* ct) {
+ clock_gettime(CLOCK_REALTIME, &ct->start_time);
+}
+
+void StopTimer(ClockTimerState* ct) {
+ clock_gettime(CLOCK_REALTIME, &ct->end_time);
+}
+
+uint32_t GetDurationMsecs(ClockTimerState* ct) {
+ uint64_t start = ((uint64_t) ct->start_time.tv_sec * 1000000000 +
+ (uint64_t) ct->start_time.tv_nsec);
+ uint64_t end = ((uint64_t) ct->end_time.tv_sec * 1000000000 +
+ (uint64_t) ct->end_time.tv_nsec);
+ uint64_t duration_msecs = (end - start) / 1000000U; /* Nanoseconds ->
+ * Milliseconds. */
+ return (uint32_t) duration_msecs;
+}
diff --git a/tests/timer_utils.h b/tests/timer_utils.h
new file mode 100644
index 0000000..95eb8c7
--- /dev/null
+++ b/tests/timer_utils.h
@@ -0,0 +1,26 @@
+/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef VBOOT_REFERENCE_TIMER_UTILS_H_
+#define VBOOT_REFERENCE_TIMER_UTILS_H_
+
+#include <inttypes.h>
+#include <time.h>
+
+typedef struct ClockTimer {
+ struct timespec start_time;
+ struct timespec end_time;
+} ClockTimerState;
+
+/* Start timer and update [ct]. */
+void StartTimer(ClockTimerState* ct);
+
+/* Stop timer and update [ct]. */
+void StopTimer(ClockTimerState* ct);
+
+/* Get duration in milliseconds. */
+uint32_t GetDurationMsecs(ClockTimerState* ct);
+
+#endif /* VBOOT_REFERENCE_TIMER_UTILS_H_ */