Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
spdlog
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
spdlog
Commits
8e429ae5
Commit
8e429ae5
authored
May 20, 2018
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
async test
parent
e2bf00ad
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
145 additions
and
5 deletions
+145
-5
README.md
README.md
+1
-1
bench/spdlog-async.cpp
bench/spdlog-async.cpp
+2
-2
example/bench.cpp
example/bench.cpp
+1
-1
example/example.cpp
example/example.cpp
+1
-1
tests/test_async.cpp
tests/test_async.cpp
+140
-0
No files found.
README.md
View file @
8e429ae5
...
@@ -170,7 +170,7 @@ int main(int, char*[])
...
@@ -170,7 +170,7 @@ int main(int, char*[])
void
async_example
()
void
async_example
()
{
{
size_t
q_size
=
4096
;
//queue size must be power of 2
size_t
q_size
=
4096
;
spd
::
set_async_mode
(
q_size
);
spd
::
set_async_mode
(
q_size
);
auto
async_file
=
spd
::
daily_logger_st
(
"async_file_logger"
,
"logs/async_log.txt"
);
auto
async_file
=
spd
::
daily_logger_st
(
"async_file_logger"
,
"logs/async_log.txt"
);
for
(
int
i
=
0
;
i
<
100
;
++
i
)
for
(
int
i
=
0
;
i
<
100
;
++
i
)
...
...
bench/spdlog-async.cpp
View file @
8e429ae5
...
@@ -25,8 +25,8 @@ int main(int argc, char *argv[])
...
@@ -25,8 +25,8 @@ int main(int argc, char *argv[])
int
howmany
=
1000000
;
int
howmany
=
1000000
;
spdlog
::
set_async_mode
(
10
48576
);
spdlog
::
set_async_mode
(
10
00000
);
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"file_logger"
,
"logs/spdlog-bench-async.log"
,
fals
e
);
auto
logger
=
spdlog
::
create
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"file_logger"
,
"logs/spdlog-bench-async.log"
,
tru
e
);
logger
->
set_pattern
(
"[%Y-%m-%d %T.%F]: %L %t %v"
);
logger
->
set_pattern
(
"[%Y-%m-%d %T.%F]: %L %t %v"
);
std
::
cout
<<
"To stop, press <Enter>"
<<
std
::
endl
;
std
::
cout
<<
"To stop, press <Enter>"
<<
std
::
endl
;
...
...
example/bench.cpp
View file @
8e429ae5
example/example.cpp
View file @
8e429ae5
...
@@ -105,7 +105,7 @@ int main(int, char *[])
...
@@ -105,7 +105,7 @@ int main(int, char *[])
void
async_example
()
void
async_example
()
{
{
size_t
q_size
=
4096
;
// queue size must be power of 2
size_t
q_size
=
4096
;
spdlog
::
set_async_mode
(
q_size
);
spdlog
::
set_async_mode
(
q_size
);
auto
async_file
=
spd
::
daily_logger_st
(
"async_file_logger"
,
"logs/async_log.txt"
);
auto
async_file
=
spd
::
daily_logger_st
(
"async_file_logger"
,
"logs/async_log.txt"
);
for
(
int
i
=
0
;
i
<
100
;
++
i
)
for
(
int
i
=
0
;
i
<
100
;
++
i
)
...
...
tests/test_async.cpp
0 → 100644
View file @
8e429ae5
#include "includes.h"
#include "test_sink.h"
template
<
class
T
>
std
::
string
log_info
(
const
T
&
what
,
spdlog
::
level
::
level_enum
logger_level
=
spdlog
::
level
::
info
)
{
std
::
ostringstream
oss
;
auto
oss_sink
=
std
::
make_shared
<
spdlog
::
sinks
::
ostream_sink_mt
>
(
oss
);
spdlog
::
logger
oss_logger
(
"oss"
,
oss_sink
);
oss_logger
.
set_level
(
logger_level
);
oss_logger
.
set_pattern
(
"%v"
);
oss_logger
.
info
(
what
);
return
oss
.
str
().
substr
(
0
,
oss
.
str
().
length
()
-
strlen
(
spdlog
::
details
::
os
::
default_eol
));
}
TEST_CASE
(
"basic async test "
,
"[async]"
)
{
auto
test_sink
=
std
::
make_shared
<
spdlog
::
sinks
::
test_sink_mt
>
();
size_t
queue_size
=
128
;
size_t
messages
=
256
;
auto
logger
=
spdlog
::
create_async
(
"as"
,
test_sink
,
128
,
spdlog
::
async_overflow_policy
::
block_retry
);
for
(
size_t
i
=
0
;
i
<
messages
;
i
++
)
{
logger
->
info
(
"Hello message #{}"
,
i
);
}
// the dtor wait for all messages in the queue to get processed
logger
.
reset
();
spdlog
::
drop
(
"as"
);
REQUIRE
(
test_sink
->
msg_counter
()
==
messages
);
REQUIRE
(
test_sink
->
flushed_msg_counter
()
==
messages
);
}
TEST_CASE
(
"discard policy "
,
"[async]"
)
{
auto
test_sink
=
std
::
make_shared
<
spdlog
::
sinks
::
test_sink_mt
>
();
size_t
queue_size
=
2
;
size_t
messages
=
1024
;
spdlog
::
drop
(
"as"
);
auto
logger
=
spdlog
::
create_async
(
"as"
,
test_sink
,
queue_size
,
spdlog
::
async_overflow_policy
::
discard_log_msg
);
for
(
size_t
i
=
0
;
i
<
messages
;
i
++
)
{
logger
->
info
(
"Hello message #{}"
,
i
);
}
// the dtor wait for all messages in the queue to get processed
logger
.
reset
();
spdlog
::
drop
(
"as"
);
REQUIRE
(
test_sink
->
msg_counter
()
<
messages
);
REQUIRE
(
test_sink
->
flushed_msg_counter
()
<
messages
);
}
TEST_CASE
(
"flush"
,
"[async]"
)
{
auto
test_sink
=
std
::
make_shared
<
spdlog
::
sinks
::
test_sink_mt
>
();
size_t
queue_size
=
256
;
size_t
messages
=
256
;
spdlog
::
drop
(
"as"
);
auto
logger
=
std
::
make_shared
<
spdlog
::
async_logger
>
(
"as"
,
test_sink
,
queue_size
);
for
(
size_t
i
=
0
;
i
<
messages
;
i
++
)
{
logger
->
info
(
"Hello message #{}"
,
i
);
}
// the dtor wait for all messages in the queue to get processed
logger
->
flush
();
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
250
));
REQUIRE
(
test_sink
->
msg_counter
()
==
messages
);
REQUIRE
(
test_sink
->
flushed_msg_counter
()
==
messages
);
REQUIRE
(
test_sink
->
flushed_msg_counter
()
==
messages
);
}
TEST_CASE
(
"multi threads"
,
"[async]"
)
{
auto
test_sink
=
std
::
make_shared
<
spdlog
::
sinks
::
test_sink_mt
>
();
size_t
queue_size
=
128
;
size_t
messages
=
256
;
size_t
n_threads
=
10
;
auto
logger
=
std
::
make_shared
<
spdlog
::
async_logger
>
(
"as"
,
test_sink
,
queue_size
);
std
::
vector
<
std
::
thread
>
threads
;
for
(
size_t
i
=
0
;
i
<
n_threads
;
i
++
)
{
threads
.
emplace_back
([
logger
,
messages
]
{
for
(
size_t
j
=
0
;
j
<
messages
;
j
++
)
{
logger
->
info
(
"Hello message #{}"
,
j
);
}
});
}
for
(
auto
&
t
:
threads
)
{
t
.
join
();
}
// the dtor wait for all messages in the queue to get processed
logger
.
reset
();
REQUIRE
(
test_sink
->
msg_counter
()
==
messages
*
n_threads
);
REQUIRE
(
test_sink
->
flushed_msg_counter
()
==
messages
*
n_threads
);
}
TEST_CASE
(
"to_file"
,
"[async]"
)
{
prepare_logdir
();
size_t
queue_size
=
512
;
size_t
messages
=
512
;
size_t
n_threads
=
4
;
auto
file_sink
=
std
::
make_shared
<
spdlog
::
sinks
::
simple_file_sink_mt
>
(
"logs/async_test.log"
,
true
);
auto
logger
=
spdlog
::
create_async
(
"as"
,
file_sink
,
queue_size
);
std
::
vector
<
std
::
thread
>
threads
;
for
(
size_t
i
=
0
;
i
<
n_threads
;
i
++
)
{
threads
.
emplace_back
([
logger
,
messages
]
{
for
(
size_t
j
=
0
;
j
<
messages
;
j
++
)
{
logger
->
info
(
"Hello message #{}"
,
j
);
}
});
}
for
(
auto
&
t
:
threads
)
{
t
.
join
();
}
logger
.
reset
();
spdlog
::
drop
(
"as"
);
REQUIRE
(
count_lines
(
"logs/async_test.log"
)
==
messages
*
n_threads
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment