[FFmpeg-devel] [PATCH] tools: add benchmark for hash functions.
Nicolas George
nicolas.george at normalesup.org
Fri May 17 17:44:49 CEST 2013
Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
.gitignore | 1 +
libavutil/Makefile | 4 +-
tools/hash_bench.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 151 insertions(+), 1 deletion(-)
create mode 100644 tools/hash_bench.c
Mostly for reference, I have no idea whether it deserves to be included.
Note that the resulting binary is unredistributable.
diff --git a/.gitignore b/.gitignore
index 5c90bfb..3e30399 100644
--- a/.gitignore
+++ b/.gitignore
@@ -69,6 +69,7 @@
/tools/ffeval
/tools/ffhash
/tools/graph2dot
+/tools/hash_bench
/tools/ismindex
/tools/pktdumper
/tools/probetest
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 5dc61e1..89d70b9 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -152,6 +152,8 @@ TESTPROGS = adler32 \
TESTPROGS-$(HAVE_LZO1X_999_COMPRESS) += lzo
-TOOLS = ffhash ffeval ffescape
+TOOLS = ffhash ffeval ffescape hash_bench
+
+tools/hash_bench: ELIBS = -lcrypto
$(SUBDIR)lzo-test$(EXESUF): ELIBS = -llzo2
diff --git a/tools/hash_bench.c b/tools/hash_bench.c
new file mode 100644
index 0000000..030004a
--- /dev/null
+++ b/tools/hash_bench.c
@@ -0,0 +1,147 @@
+/*
+ * Copyright (c) 2013 Nicolas George
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdlib.h>
+#include <math.h>
+
+#include "libavutil/avutil.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/timer.h"
+
+#include "libavutil/md5.h"
+#include "libavutil/sha.h"
+
+#include <openssl/md5.h>
+#include <openssl/sha.h>
+
+#define MAX_INPUT_SIZE 1048576
+#define MAX_OUTPUT_SIZE 128
+
+static void fatal_error(const char *tag)
+{
+ av_log(NULL, AV_LOG_ERROR, "Fatal error: %s\n", tag);
+ exit(1);
+}
+
+struct hash_impl {
+ const char *name;
+ void (*run)(uint8_t *output, const uint8_t *input, unsigned size);
+ const char *output;
+};
+
+/* MD5 MD5 MD5_ */
+/* SHA-1 SHA160 SHA1_ */
+/* SHA-256 SHA256 SHA224_ */
+
+static void run_hash_lavu_md5(uint8_t *output,
+ const uint8_t *input, unsigned size)
+{
+ av_md5_sum(output, input, size);
+}
+
+#define DEFINE_LAVU_SHA(suffix, hsize) \
+static void run_hash_lavu_ ## suffix(uint8_t *output, \
+ const uint8_t *input, unsigned size) \
+{ \
+ static struct AVSHA *h; \
+ if (!h && !(h = av_sha_alloc())) \
+ fatal_error("out of memory"); \
+ av_sha_init(h, hsize); \
+ av_sha_update(h, input, size); \
+ av_sha_final(h, output); \
+}
+
+DEFINE_LAVU_SHA(sha1, 160);
+DEFINE_LAVU_SHA(sha224, 224);
+DEFINE_LAVU_SHA(sha256, 256);
+
+#define DEFINE_OPENSSL_WRAPPER(suffix, function) \
+static void run_hash_openssl_ ## suffix(uint8_t *output, \
+ const uint8_t *input, unsigned size) \
+{ \
+ function(input, size, output); \
+}
+
+DEFINE_OPENSSL_WRAPPER(md5, MD5)
+DEFINE_OPENSSL_WRAPPER(sha1, SHA1)
+DEFINE_OPENSSL_WRAPPER(sha224, SHA224)
+DEFINE_OPENSSL_WRAPPER(sha256, SHA256)
+
+static void run_implementation(const uint8_t *input, struct hash_impl *impl,
+ unsigned size)
+{
+ uint64_t t0, t1;
+ unsigned nruns = (1 << 30) / size;
+ unsigned outlen = strlen(impl->output) / 2;
+ unsigned i, val;
+ double mtime, ttime = 0, ttime2 = 0, stime;
+ uint8_t out[MAX_OUTPUT_SIZE], outref[MAX_OUTPUT_SIZE];
+
+ for (i = 0; i < outlen; i++) {
+ sscanf(impl->output + i * 2, "%02x", &val);
+ outref[i] = val;
+ }
+ for (i = 0; i < 8; i++) /* heat caches */
+ impl->run(out, input, size);
+ for (i = 0; i < nruns; i++) {
+ t0 = AV_READ_TIME();
+ impl->run(out, input, size);
+ t1 = AV_READ_TIME();
+ if (memcmp(out, outref, outlen))
+ fatal_error("output mismatch");
+ mtime = (double)(t1 - t0) / size;
+ ttime += mtime;
+ ttime2 += mtime * mtime;
+ }
+
+ ttime /= nruns;
+ ttime2 /= nruns;
+ stime = sqrt(ttime2 - ttime * ttime);
+ printf("%-16s size: %8d runs: %8d time: %8.3f +- %.3f\n",
+ impl->name, size, nruns, ttime, stime);
+}
+
+struct hash_impl implementations[] = {
+ { "lavu MD5", run_hash_lavu_md5, "aa26ff5b895356bcffd9292ba9f89e66" },
+ { "openssl MD5", run_hash_openssl_md5, "aa26ff5b895356bcffd9292ba9f89e66" },
+ { "lavu SHA-1", run_hash_lavu_sha1, "1fd8bd1fa02f5b0fe916b0d71750726b096c5744" },
+ { "openssl SHA-1", run_hash_openssl_sha1, "1fd8bd1fa02f5b0fe916b0d71750726b096c5744" },
+ { "lavu SHA-224", run_hash_lavu_sha224, "a62ff2737bff9f78440e8a763b2c1bf66e4472d0efa5d37797238696" },
+ { "openssl SHA-224", run_hash_openssl_sha224, "a62ff2737bff9f78440e8a763b2c1bf66e4472d0efa5d37797238696" },
+ { "lavu SHA-256", run_hash_lavu_sha256, "14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5" },
+ { "openssl SHA-256", run_hash_openssl_sha256, "14028ac673b3087e51a1d407fbf0df4deeec8f217119e13b07bf2138f93db8c5" },
+};
+
+int main(void)
+{
+ uint8_t *input = av_malloc(MAX_INPUT_SIZE);
+ unsigned i, impl, size;
+
+ if (!input)
+ fatal_error("out of memory");
+ for (i = 0; i < MAX_INPUT_SIZE; i += 4)
+ AV_WB32(input + i, i);
+
+ size = MAX_INPUT_SIZE;
+ for (impl = 0; impl < FF_ARRAY_ELEMS(implementations); impl++)
+ run_implementation(input, &implementations[impl], size);
+
+ return 0;
+}
--
1.7.10.4
More information about the ffmpeg-devel
mailing list