Skip to content

Commit 9ec4777

Browse files
authored
Add benchmark
* add benchmark option * updated changelog * added benchmark info to readme * add bench folder to format.py
1 parent 2a00c9f commit 9ec4777

File tree

7 files changed

+152
-2
lines changed

7 files changed

+152
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ maddy uses [semver versioning](https://semver.org/).
1313
* ![**REMOVED**](https://img.shields.io/badge/-REMOVED-%23900) for now removed features.
1414

1515
## Upcoming
16+
1617
* ![**ADDED**](https://img.shields.io/badge/-ADDED-%23099) Correctly parse links with title text, i.e. `[link](http://example.com "example")`.
1718
* ![**FIXED**](https://img.shields.io/badge/-FIXED-%23090) Do not create invalid URLs from links with spaces, i.e. `[link](/ABC/some file)`.
1819
* ![**FIXED**](https://img.shields.io/badge/-FIXED-%23090) Do not create invalid HTML from links with quotes, i.e. `[link](/ABC/some"file)`.
19-
20+
* ![**ADDED**](https://img.shields.io/badge/-ADDED-%23099) benchmarks.
2021

2122
## version 1.4.0 2025-03-28
2223

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ if(${MADDY_BUILD_WITH_TESTS})
1919
enable_testing()
2020
endif()
2121

22+
option(MADDY_BUILD_WITH_BENCH "enable benchmarks" OFF)
2223
option(MADDY_CREATE_PACKAGE "create a package for a version release" OFF)
2324

2425
# ------------------------------------------------------------------------------
@@ -49,10 +50,14 @@ if(${MADDY_BUILD_WITH_TESTS})
4950
add_subdirectory(tests)
5051
endif()
5152

53+
if(${MADDY_BUILD_WITH_BENCH})
54+
add_subdirectory(bench)
55+
endif()
56+
5257
# ------------------------------------------------------------------------------
5358

5459
if(${MADDY_CREATE_PACKAGE})
55-
set(MADDY_PACKAGE_FILES include/ CMakeLists.txt LICENSE)
60+
set(MADDY_PACKAGE_FILES include/ CMakeLists.txt LICENSE AUTHORS)
5661
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-src.zip
5762
COMMAND ${CMAKE_COMMAND} -E tar c ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-src.zip --format=zip -- ${MADDY_PACKAGE_FILES}
5863
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ make
9898
make test # or run the executable in ../build/MaddyTests
9999
```
100100

101+
## How to run the benchmarks
102+
103+
To get proper test results, the benchmarks should always be compiled as
104+
release build.
105+
106+
```shell
107+
git clone https://github.com/progsource/maddy.git
108+
cd maddy
109+
mkdir tmp
110+
cd tmp
111+
cmake -DMADDY_BUILD_WITH_BENCH=ON -DCMAKE_BUILD_TYPE=Release ..
112+
make BUILD_TYPE=Release
113+
../build/maddy_benchmark
114+
```
115+
101116
## How to contribute
102117

103118
There are different possibilities:

bench/CMakeLists.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# This project is licensed under the MIT license. For more information see the
2+
# LICENSE file.
3+
4+
if (UNIX AND NOT APPLE)
5+
execute_process(COMMAND ${CMAKE_CXX_COMPILER}
6+
-fuse-ld=gold -Wl,--version
7+
ERROR_QUIET OUTPUT_VARIABLE ld_version)
8+
if ("${ld_version}" MATCHES "GNU gold")
9+
message(STATUS "Found Gold linker, use faster linker")
10+
set(CMAKE_EXE_LINKER_FLAGS
11+
"${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold")
12+
set(CMAKE_SHARED_LINKER_FLAGS
13+
"${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=gold ")
14+
endif()
15+
endif()
16+
17+
# ------------------------------------------------------------------------------
18+
19+
include(FetchContent)
20+
21+
FetchContent_Declare(
22+
nanobench
23+
GIT_REPOSITORY https://github.com/martinus/nanobench.git
24+
GIT_TAG v4.3.11
25+
GIT_SHALLOW TRUE
26+
)
27+
28+
FetchContent_MakeAvailable(nanobench)
29+
30+
# ------------------------------------------------------------------------------
31+
32+
file(GLOB_RECURSE MADDY_BENCHMARK_FILES
33+
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
34+
)
35+
36+
# ------------------------------------------------------------------------------
37+
38+
add_executable(
39+
maddy_benchmark
40+
${MADDY_BENCHMARK_FILES}
41+
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
42+
)
43+
target_include_directories(maddy_benchmark PUBLIC
44+
${CMAKE_CURRENT_SOURCE_DIR}
45+
)
46+
target_link_libraries(maddy_benchmark maddy nanobench::nanobench)
47+
set_target_properties(maddy_benchmark PROPERTIES
48+
CMAKE_CXX_FLAGS
49+
"${CMAKE_CXX_FLAGS} -O2 -Wall -Wno-ignored-qualifiers -Wpedantic -Wextra -Wno-deprecated -fno-exceptions -fno-rtti"
50+
)

bench/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# maddy benchmarks
2+
3+
## How to run
4+
5+
The benchmarks have to be run in release mode to give proper results.
6+
7+
```shell
8+
# in main folder
9+
mkdir tmp
10+
cd tmp
11+
cmake -DMADDY_BUILD_WITH_BENCH=ON -DCMAKE_BUILD_TYPE=Release ..
12+
make BUILD_TYPE=Release
13+
../build/maddy_benchmark
14+
```

bench/main.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This project is licensed under the MIT license. For more information see the
3+
* LICENSE file.
4+
*/
5+
#include <fstream>
6+
#include <iostream>
7+
#include <memory>
8+
#include <sstream>
9+
#include <string>
10+
11+
#define ANKERL_NANOBENCH_IMPLEMENT
12+
#include <nanobench.h>
13+
14+
#include "maddy/parser.h"
15+
16+
int main()
17+
{
18+
static const std::string markdownFile = "../docs/definitions.md";
19+
std::stringstream buffer;
20+
21+
{
22+
std::ifstream file(markdownFile);
23+
24+
if (!file.good() || !file.is_open())
25+
{
26+
std::cout << "could not read file at " << markdownFile << std::endl;
27+
return 1;
28+
}
29+
30+
buffer << file.rdbuf();
31+
32+
file.close();
33+
}
34+
35+
if (!buffer.good() || buffer.str().empty())
36+
{
37+
std::cout << "buffer is invalid" << std::endl;
38+
return 2;
39+
}
40+
41+
// maddy 1.*
42+
std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>();
43+
44+
// This is the place in the future to compare maddy with other libraries.
45+
// For now it can be used to check if changes in maddy code result in better
46+
// performance.
47+
48+
ankerl::nanobench::Bench()
49+
.title("maddy test")
50+
.warmup(100)
51+
.relative(true)
52+
.run(
53+
"maddy 1.x",
54+
[&]()
55+
{
56+
buffer.clear(); // clear any error flags
57+
buffer.seekg(0, buffer.beg);
58+
ankerl::nanobench::doNotOptimizeAway(parser->Parse(buffer));
59+
}
60+
);
61+
62+
return 0;
63+
}

tools/format.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def format_files(dry_run):
6060
during the actual formatting process.
6161
"""
6262
patterns = [
63+
"bench/**/*.h",
64+
"bench/**/*.cpp",
6365
"include/**/*.h",
6466
"tests/**/*.h",
6567
"tests/**/*.cpp",

0 commit comments

Comments
 (0)