Skip to content

Commit

Permalink
feat benchmark: added crc bench
Browse files Browse the repository at this point in the history
Signed-off-by: John Sanpe <[email protected]>
  • Loading branch information
sanpeqf committed Jan 22, 2024
1 parent 6c9b7d2 commit 08b01e4
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bfdev
Submodule bfdev updated 117 files
2 changes: 1 addition & 1 deletion examples/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright(c) 2023 ffashion <[email protected]>
#

add_executable(benchmark main.c rbtree.c)
add_executable(benchmark main.c crc.c rbtree.c)
target_link_libraries(benchmark ${CMAKE_PROJECT_NAME})
target_link_libraries(benchmark bfdev)

Expand Down
56 changes: 56 additions & 0 deletions examples/benchmark/crc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright(c) 2021-2022 John Sanpe <[email protected]>
*/

#define MODULE_NAME "crc-benchmark"
#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <bfdev/crc.h>
#include <bfdev/prandom.h>
#include <bfdev/log.h>
#include <bfdev/size.h>
#include "py32f0xx_hal.h"

#define TEST_SIZE BFDEV_SZ_1KiB
#define TEST_LOOP 3

#define GENERIC_CRC_BANDWIDTH(func, name, size) \
for (count = 0; count < TEST_LOOP; ++count) { \
uint32_t start = HAL_GetTick(); \
loop = 0; \
do { \
func(buff, size, 0); \
loop++; \
} while (start + 1000 > HAL_GetTick()); \
bfdev_log_notice( \
name " bandwidth %u: %uKiB/s\n", \
count, loop \
); \
}

int crc_benchmark(int argc, char const *argv[])
{
bfdev_prandom_state_t pstate;
unsigned int count, loop;
uint8_t *buff;
size_t index;

buff = malloc(TEST_SIZE);
if (!buff)
return 1;

bfdev_prandom_seed(&pstate, HAL_GetTick());
for (count = 0; count < TEST_SIZE; ++count)
buff[count] = bfdev_prandom_value(&pstate);

GENERIC_CRC_BANDWIDTH(bfdev_crc7, "crc7", TEST_SIZE)
GENERIC_CRC_BANDWIDTH(bfdev_crc8, "crc8", TEST_SIZE)
free(buff);

return 0;
}
27 changes: 20 additions & 7 deletions examples/benchmark/main.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#include <stdio.h>
#include <bfdev/config.h>
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright(c) 2021-2022 John Sanpe <[email protected]>
*/

#define MODULE_NAME "benchmark"
#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt

#include <bfdev/log.h>
#include "py32f0xx_hal.h"

UART_HandleTypeDef huart1;
extern int benchmark(void);
extern int crc_benchmark(void);
extern int rbtree_benchmark(void);

int __io_putchar(int ch)
{
Expand Down Expand Up @@ -49,10 +57,15 @@ int main(void)
__HAL_RCC_USART1_CLK_ENABLE();
HAL_UART_Init(&huart1);

printf("Benchmark for PY32F0xx.\n");
printf("Bfdev version: %s\n", __bfdev_stringify(BFDEV_VERSION));
printf("This may take a few minutes...\n\n");
benchmark();
bfdev_log_info("Benchmark for PY32F0xx.\n");
bfdev_log_info("Bfdev version: %s\n", __bfdev_stringify(BFDEV_VERSION));
bfdev_log_info("This may take a few minutes...\n");

puts(""); /* '\n' */
crc_benchmark();

puts(""); /* '\n' */
rbtree_benchmark();

return 0;
}
14 changes: 9 additions & 5 deletions examples/benchmark/rbtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
* Copyright(c) 2021-2022 John Sanpe <[email protected]>
*/

#define MODULE_NAME "rbtree-benchmark"
#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <bfdev/rbtree.h>
#include <bfdev/log.h>
#include "py32f0xx_hal.h"

#define TEST_LEN 50
Expand All @@ -31,7 +35,7 @@ demo_cmp(const struct bfdev_rb_node *a,
return demo_a->data - demo_b->data;
}

int benchmark(void)
int rbtree_benchmark(void)
{
struct bench_node *node;
unsigned int count, loop;
Expand All @@ -40,15 +44,15 @@ int benchmark(void)

node = block = malloc(sizeof(*node) * TEST_LEN);
if (!block) {
printf("Insufficient Memory!\n");
bfdev_log_err("Insufficient Memory!\n");
return -ENOMEM;
}

printf("Generate Node: %u\n", TEST_LEN);
bfdev_log_notice("Generate Node: %u\n", TEST_LEN);
for (count = 0; count < TEST_LEN; ++count)
node[count].data = count;

printf("Insert Nodes: %u\n", TEST_LOOP * TEST_LEN);
bfdev_log_notice("Insert Nodes: %u\n", TEST_LOOP * TEST_LEN);
start = HAL_GetTick();

for (loop = 0; loop < TEST_LOOP; ++loop) {
Expand All @@ -58,7 +62,7 @@ int benchmark(void)
}

time = HAL_GetTick() - start;
printf("Total time: %lu.%lus\n", time / 1000, time % 1000);
bfdev_log_notice("Total time: %lu.%lus\n", time / 1000, time % 1000);
free(block);

return 0;
Expand Down

0 comments on commit 08b01e4

Please sign in to comment.