Skip to content

Commit 53568b7

Browse files
rfvirgilshuahkh
authored andcommitted
kunit: string-stream: Test performance of string_stream
Add a test of the speed and memory use of string_stream. string_stream_performance_test() doesn't actually "test" anything (it cannot fail unless the system has run out of allocatable memory) but it measures the speed and memory consumption of the string_stream and reports the result. This allows changes in the string_stream implementation to be compared. Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 05e2006 commit 53568b7

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

lib/kunit/string-stream-test.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
#include <kunit/static_stub.h>
1010
#include <kunit/test.h>
11+
#include <linux/ktime.h>
1112
#include <linux/slab.h>
13+
#include <linux/timekeeping.h>
1214

1315
#include "string-stream.h"
1416

@@ -454,6 +456,57 @@ static void string_stream_auto_newline_test(struct kunit *test)
454456
"One\nTwo\nThree\nFour\nFive\nSix\nSeven\n\nEight\n");
455457
}
456458

459+
/*
460+
* This doesn't actually "test" anything. It reports time taken
461+
* and memory used for logging a large number of lines.
462+
*/
463+
static void string_stream_performance_test(struct kunit *test)
464+
{
465+
struct string_stream_fragment *frag_container;
466+
struct string_stream *stream;
467+
char test_line[101];
468+
ktime_t start_time, end_time;
469+
size_t len, bytes_requested, actual_bytes_used, total_string_length;
470+
int offset, i;
471+
472+
stream = kunit_alloc_string_stream(test, GFP_KERNEL);
473+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
474+
475+
memset(test_line, 'x', sizeof(test_line) - 1);
476+
test_line[sizeof(test_line) - 1] = '\0';
477+
478+
start_time = ktime_get();
479+
for (i = 0; i < 10000; i++) {
480+
offset = i % (sizeof(test_line) - 1);
481+
string_stream_add(stream, "%s: %d\n", &test_line[offset], i);
482+
}
483+
end_time = ktime_get();
484+
485+
/*
486+
* Calculate memory used. This doesn't include invisible
487+
* overhead due to kernel allocator fragment size rounding.
488+
*/
489+
bytes_requested = sizeof(*stream);
490+
actual_bytes_used = ksize(stream);
491+
total_string_length = 0;
492+
493+
list_for_each_entry(frag_container, &stream->fragments, node) {
494+
bytes_requested += sizeof(*frag_container);
495+
actual_bytes_used += ksize(frag_container);
496+
497+
len = strlen(frag_container->fragment);
498+
total_string_length += len;
499+
bytes_requested += len + 1; /* +1 for '\0' */
500+
actual_bytes_used += ksize(frag_container->fragment);
501+
}
502+
503+
kunit_info(test, "Time elapsed: %lld us\n",
504+
ktime_us_delta(end_time, start_time));
505+
kunit_info(test, "Total string length: %zu\n", total_string_length);
506+
kunit_info(test, "Bytes requested: %zu\n", bytes_requested);
507+
kunit_info(test, "Actual bytes allocated: %zu\n", actual_bytes_used);
508+
}
509+
457510
static int string_stream_test_init(struct kunit *test)
458511
{
459512
struct string_stream_test_priv *priv;
@@ -479,6 +532,7 @@ static struct kunit_case string_stream_test_cases[] = {
479532
KUNIT_CASE(string_stream_append_empty_string_test),
480533
KUNIT_CASE(string_stream_no_auto_newline_test),
481534
KUNIT_CASE(string_stream_auto_newline_test),
535+
KUNIT_CASE(string_stream_performance_test),
482536
{}
483537
};
484538

0 commit comments

Comments
 (0)