From 8a6e4a40c1699337d5fd70d5f058f7024a5c4ba0 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 8 Mar 2011 16:05:08 +0000 Subject: [PATCH 2/4] dbus-spam: add support for sending pseudorandomly-sized messages --- tools/dbus-spam.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 84 insertions(+), 1 deletions(-) diff --git a/tools/dbus-spam.c b/tools/dbus-spam.c index 3476f30..ab88635 100644 --- a/tools/dbus-spam.c +++ b/tools/dbus-spam.c @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -61,6 +62,12 @@ usage (int ecode) " --payload=S use S as payload (default \"hello, world!\")\n" " --stdin read payload from stdin, until EOF\n" " --message-stdin read a complete D-Bus message from stdin\n" + " --random-size read whitespace-separated ASCII decimal\n" + " payload sizes from stdin and pick one randomly\n" + " for each message\n" + "\n" + " --seed=SEED seed for srand (default is time())\n" + "\n" ); exit (ecode); } @@ -144,6 +151,9 @@ main (int argc, char **argv) int payload_type = DBUS_TYPE_STRING; DBusMessage *template = NULL; dbus_bool_t no_reply = FALSE; + unsigned int seed = time (NULL); + int n_random_sizes = 0; + unsigned int *random_sizes = NULL; for (i = 1; i < argc; i++) { @@ -196,6 +206,50 @@ main (int argc, char **argv) exit (1); } } + else if (strcmp (arg, "--random-size") == 0) + { + unsigned int len, max = 0; + int consumed = 0; + const char *p; + + consume_stdin (&payload_buf, &payload_len); + + for (p = payload_buf; p < payload_buf + payload_len; p += consumed) + { + /* the space character matches any (or no) whitespace */ + if (sscanf (p, " %u %n", &len, &consumed) == 0) + break; + + n_random_sizes++; + } + + random_sizes = dbus_new0 (int, n_random_sizes); + + if (random_sizes == NULL) + oom ("allocating array of message lengths"); + + for (p = payload_buf, i = 0; + p < payload_buf + payload_len && i < n_random_sizes; + p += consumed, i++) + { + sscanf (p, " %u %n", &len, &consumed); + random_sizes[i] = len; + + if (len > max) + max = len; + } + + dbus_free (payload_buf); + payload_len = max + 1; + payload_buf = dbus_new (char, payload_len); + payload = payload_buf; + + if (payload_buf == NULL) + oom ("allocating maximum-sized payload"); + + memset (payload_buf, 'X', payload_len); + payload_buf[payload_len - 1] = '\0'; + } else if (strcmp (arg, "--empty") == 0) { payload_type = DBUS_TYPE_INVALID; @@ -224,6 +278,10 @@ main (int argc, char **argv) if (queue_len < 1) usage (2); } + else if (strstr (arg, "--seed=") == arg) + { + seed = strtoul (arg + strlen ("--seed="), NULL, 10); + } else { usage (2); @@ -268,6 +326,8 @@ main (int argc, char **argv) else { dbus_bool_t mem; + int chosen = -1; + unsigned int len = 0; message = dbus_message_new_method_call (destination, "/", @@ -282,17 +342,40 @@ main (int argc, char **argv) switch (payload_type) { case DBUS_TYPE_STRING: + if (random_sizes != NULL) + { + /* this isn't fair, strictly speaking - the first few + * are a bit more likely to be chosen, unless + * RAND_MAX is divisible by n_random_sizes - but it's + * good enough for traffic-generation */ + len = random_sizes[rand () % n_random_sizes]; + payload_buf[len] = '\0'; + } + mem = dbus_message_append_args (message, DBUS_TYPE_STRING, &payload, DBUS_TYPE_INVALID); + + if (random_sizes != NULL) + { + /* undo the truncation above */ + payload_buf[len] = 'X'; + } + break; case DBUS_TYPE_ARRAY: + len = payload_len; + + /* as above, not strictly fair, but close enough */ + if (random_sizes != NULL) + len = random_sizes[rand () % n_random_sizes]; + mem = dbus_message_append_args (message, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &payload, - (dbus_uint32_t) payload_len, + (dbus_uint32_t) len, DBUS_TYPE_INVALID); break; -- 1.7.5.4