Skip to content
Browse files

Added benchmarking option

  • Loading branch information...
1 parent c4f989a commit 40a24d929d0e6d21efdd8e2a371ee2d9e261487f Caleb James DeLisle committed
Showing with 190 additions and 3 deletions.
  1. +14 −1 cjdroute.c
  2. +3 −0 crypto/CMakeLists.txt
  3. +1 −1 crypto/CryptoAuth.c
  4. +1 −1 crypto/CryptoAuth.h
  5. +144 −0 crypto/CryptoAuth_benchmark.c
  6. +27 −0 crypto/CryptoAuth_benchmark.h
View
15 cjdroute.c
@@ -18,6 +18,7 @@
#include "crypto/AddressCalc.h"
#include "crypto/Crypto.h"
#include "crypto/CryptoAuth.h"
+#include "crypto/CryptoAuth_benchmark.h"
#include "dht/CJDHTConstants.h"
#include "dht/ReplyModule.h"
#include "dht/SerializationModule.h"
@@ -268,7 +269,7 @@ static void parsePrivateKey(Dict* config, struct Address* addr, uint8_t privateK
static int usage(char* appName)
{
- printf("Usage: %s [--help] [--genconf] [--getcmds]\n"
+ printf("Usage: %s [--help] [--genconf] [--getcmds] [--bench]\n"
"\n"
"To get the router up and running.\n"
"Step 1:\n"
@@ -496,6 +497,16 @@ static void pidfile(Dict* config)
}
}
+static int benchmark()
+{
+ struct Allocator* alloc = MallocAllocator_new(1<<22);
+ struct event_base* base = event_base_new();
+ struct Writer* logwriter = FileWriter_new(stdout, alloc);
+ struct Log logger = { .writer = logwriter };
+ CryptoAuth_benchmark(base, &logger, alloc);
+ return 0;
+}
+
int main(int argc, char** argv)
{
#ifdef Log_KEYS
@@ -515,6 +526,8 @@ int main(int argc, char** argv)
// Performed after reading the configuration
} else if (strcmp(argv[1], "--reconf") == 0) {
// Performed after reading the configuration
+ } else if (strcmp(argv[1], "--bench") == 0) {
+ return benchmark();
} else {
fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], argv[1]);
fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
View
3 crypto/CMakeLists.txt
@@ -29,6 +29,9 @@ target_link_libraries(crypto
util
)
+add_library(crypto_benchmark CryptoAuth_benchmark.c)
+target_link_libraries(crypto_benchmark crypto)
+
# Everything must be tested.
enable_testing()
add_subdirectory(test)
View
2 crypto/CryptoAuth.c
@@ -965,7 +965,7 @@ void* CryptoAuth_getUser(struct Interface* interface)
}
struct Interface* CryptoAuth_wrapInterface(struct Interface* toWrap,
- uint8_t herPublicKey[32],
+ const uint8_t herPublicKey[32],
const bool requireAuth,
bool authenticatePackets,
struct CryptoAuth* context)
View
2 crypto/CryptoAuth.h
@@ -102,7 +102,7 @@ struct CryptoAuth* CryptoAuth_new(Dict* config,
* @param context the CryptoAuth context.
*/
struct Interface* CryptoAuth_wrapInterface(struct Interface* toWrap,
- uint8_t herPublicKey[32],
+ const uint8_t herPublicKey[32],
const bool requireAuth,
bool authenticatePackets,
struct CryptoAuth* context);
View
144 crypto/CryptoAuth_benchmark.c
@@ -0,0 +1,144 @@
+/* vim: set expandtab ts=4 sw=4: */
+/*
+ * You may redistribute this program and/or modify it under the terms of
+ * the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "crypto/CryptoAuth.h"
+#include "crypto/test/Exports.h"
+#include "io/FileWriter.h"
+#include "benc/Object.h"
+#include "memory/MallocAllocator.h"
+#include "util/Bits.h"
+#include "util/Hex.h"
+#include "util/Endian.h"
+#include "util/Time.h"
+#include "wire/Error.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <event2/event.h>
+
+static const uint8_t* privateKey = (uint8_t*)
+ "\x20\xca\x45\xd9\x5b\xbf\xca\xe7\x35\x3c\xd2\xdf\xfa\x12\x84\x4b"
+ "\x4e\xff\xbe\x7d\x39\xd8\x4d\x8e\x14\x2b\x9d\x21\x89\x5b\x38\x09";
+
+static const uint8_t* publicKey = (uint8_t*)
+ "\x51\xaf\x8d\xd9\x35\xe8\x61\x86\x3e\x94\x2b\x1b\x6d\x21\x22\xe0"
+ "\x2f\xb2\xd0\x88\x20\xbb\xf3\xf0\x6f\xcd\xe5\x85\x30\xe0\x08\x34";
+
+struct Context
+{
+ uint8_t padding[256];
+ uint8_t buffer[2048];
+ struct Message message;
+ struct CryptoAuth* ca1;
+ struct CryptoAuth* ca2;
+
+ struct Interface if1;
+ struct Interface* cif1;
+ struct Message* if1Incoming;
+
+ struct Interface if2;
+ struct Interface* cif2;
+ struct Message* if2Incoming;
+
+ struct event_base* base;
+};
+
+static uint8_t transferMessage(struct Message* message, struct Interface* iface)
+{
+ struct Interface* otherIface = iface->senderContext;
+ return otherIface->receiveMessage(message, otherIface);
+}
+
+static inline void setupMessage(struct Context* ctx, uint16_t length)
+{
+ ctx->message.bytes = ctx->buffer;
+ ctx->message.padding = 256;
+ ctx->message.length = length;
+}
+
+static const char* KEY = "key";
+static const char* HELLO = "hello";
+static const char* TRAFFIC = "traffic";
+
+static inline void sendMessages(struct Context* ctx,
+ struct Log* logger,
+ int count,
+ int size,
+ const char* type,
+ bool authPackets)
+{
+ Log_info4(logger, "Test sending %d %s packets of %d size. packet auth %s",
+ count, type, size, ((authPackets) ? "enabled." : "disabled."));
+
+ ctx->cif2 = CryptoAuth_wrapInterface(&ctx->if2, NULL, false, authPackets, ctx->ca2);
+ ctx->cif1 = CryptoAuth_wrapInterface(&ctx->if1, publicKey, false, authPackets, ctx->ca1);
+ uint64_t startTime = Time_currentTimeMilliseconds(ctx->base);
+ if (type != HELLO) {
+ setupMessage(ctx, size);
+ ctx->cif1->sendMessage(&ctx->message, ctx->cif1);
+ if (type == TRAFFIC) {
+ setupMessage(ctx, size);
+ ctx->cif2->sendMessage(&ctx->message, ctx->cif2);
+ }
+ }
+ for (int i = 0; i < count; i++) {
+ setupMessage(ctx, size);
+ if (type == KEY) {
+ ctx->cif2->sendMessage(&ctx->message, ctx->cif2);
+ } else {
+ ctx->cif1->sendMessage(&ctx->message, ctx->cif1);
+ }
+ }
+ uint64_t endTimes = Time_currentTimeMilliseconds(ctx->base);
+ uint64_t time = endTimes - startTime;
+ uint64_t kbSent = (size * count * 8) / 1024;
+
+ // same as kbSent / (time / 1024) (converting time to seconds)
+ uint64_t kbps = (kbSent * 1024) / time;
+
+ Log_info2(logger, "Finished in %dms. %d Kb/s\n\n", (int)time, (int)kbps);
+}
+
+void CryptoAuth_benchmark(struct event_base* base,
+ struct Log* logger,
+ struct Allocator* alloc)
+{
+ struct Context ctx = {
+ .ca1 = CryptoAuth_new(NULL, alloc, NULL, base, NULL),
+ .ca2 = CryptoAuth_new(NULL, alloc, privateKey, base, NULL),
+ .if1 = {
+ .sendMessage = transferMessage,
+ .senderContext = &ctx.if2,
+ .allocator = alloc
+ },
+ .if2 = {
+ .sendMessage = transferMessage,
+ .senderContext = &ctx.if1,
+ .allocator = alloc
+ },
+ .base = base
+ };
+ sendMessages(&ctx, logger, 1000, 64, HELLO, true);
+ sendMessages(&ctx, logger, 1000, 1500, HELLO, true);
+
+ sendMessages(&ctx, logger, 1000, 64, KEY, true);
+ sendMessages(&ctx, logger, 1000, 1500, KEY, true);
+
+ sendMessages(&ctx, logger, 100000, 64, TRAFFIC, false);
+ sendMessages(&ctx, logger, 100000, 1500, TRAFFIC, false);
+
+ sendMessages(&ctx, logger, 100000, 64, TRAFFIC, true);
+ Log_info(logger, "This is the most important metric:");
+ sendMessages(&ctx, logger, 100000, 1500, TRAFFIC, true);
+}
View
27 crypto/CryptoAuth_benchmark.h
@@ -0,0 +1,27 @@
+/* vim: set expandtab ts=4 sw=4: */
+/*
+ * You may redistribute this program and/or modify it under the terms of
+ * the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef CryptoAuth_benchmark_H
+#define CryptoAuth_benchmark_H
+
+#include "memory/Allocator.h"
+#include "util/Log.h"
+
+#include <event2/event.h>
+
+void CryptoAuth_benchmark(struct event_base* base,
+ struct Log* logger,
+ struct Allocator* alloc);
+
+#endif

0 comments on commit 40a24d9

Please sign in to comment.
Something went wrong with that request. Please try again.