mirror of
https://github.com/gabime/spdlog.git
synced 2025-05-03 05:23:51 +00:00
bench and examples updates
This commit is contained in:
parent
5897584175
commit
76436d07cc
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
int main(int, char* [])
|
int main(int, char* [])
|
||||||
{
|
{
|
||||||
int howmany = 1000000;
|
int howmany = 1048576;
|
||||||
namespace spd = spdlog;
|
namespace spd = spdlog;
|
||||||
spd::set_async_mode(howmany);
|
spd::set_async_mode(howmany);
|
||||||
///Create a file rotating logger with 5mb size max and 3 rotated files
|
///Create a file rotating logger with 5mb size max and 3 rotated files
|
||||||
@ -14,5 +14,9 @@ int main(int, char* [])
|
|||||||
for(int i = 0 ; i < howmany; ++i)
|
for(int i = 0 ; i < howmany; ++i)
|
||||||
logger->info() << "spdlog message #" << i << ": This is some text for your pleasure";
|
logger->info() << "spdlog message #" << i << ": This is some text for your pleasure";
|
||||||
|
|
||||||
|
|
||||||
|
//because spdlog async logger waits for the back thread logger to finish all messages upon destrcuting,
|
||||||
|
//and we want to measure only the time it took to push those messages to the backthread..
|
||||||
|
abort();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -14,10 +14,10 @@ int main(int argc, char* argv[])
|
|||||||
if(argc > 1)
|
if(argc > 1)
|
||||||
thread_count = atoi(argv[1]);
|
thread_count = atoi(argv[1]);
|
||||||
|
|
||||||
int howmany = 1000000;
|
int howmany = 1048576;
|
||||||
|
|
||||||
namespace spd = spdlog;
|
namespace spd = spdlog;
|
||||||
spd::set_async_mode(howmany, std::chrono::seconds(0));
|
spd::set_async_mode(howmany);
|
||||||
///Create a file rotating logger with 5mb size max and 3 rotated files
|
///Create a file rotating logger with 5mb size max and 3 rotated files
|
||||||
auto logger = spd::rotating_logger_mt("file_logger", "logs/spd-sample", 10 *1024 * 1024 , 5);
|
auto logger = spd::rotating_logger_mt("file_logger", "logs/spd-sample", 10 *1024 * 1024 , 5);
|
||||||
|
|
||||||
@ -45,5 +45,7 @@ int main(int argc, char* argv[])
|
|||||||
t.join();
|
t.join();
|
||||||
};
|
};
|
||||||
|
|
||||||
return 0;
|
//because spdlog async logger waits for the back thread logger to finish all messages upon destrcuting,
|
||||||
|
//and we want to measure only the time it took to push those messages to the backthread..
|
||||||
|
abort();
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count
|
|||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
|
||||||
int howmany = 1000000;
|
int howmany = 1048576;
|
||||||
int threads = 10;
|
int threads = 10;
|
||||||
bool auto_flush = false;
|
bool auto_flush = false;
|
||||||
int file_size = 30 * 1024 * 1024;
|
int file_size = 30 * 1024 * 1024;
|
||||||
@ -94,17 +94,12 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
spdlog::set_async_mode(howmany);
|
spdlog::set_async_mode(howmany);
|
||||||
|
|
||||||
for(int i = 0; i < 3; ++i)
|
for(int i = 0; i < 5; ++i)
|
||||||
{
|
{
|
||||||
auto as = spdlog::daily_logger_st("as", "logs/daily_async", auto_flush);
|
auto as = spdlog::daily_logger_st("as", "logs/daily_async", auto_flush);
|
||||||
//bench_mt(howmany, spdlog::create<null_sink_st>("as"), threads);
|
bench_mt(howmany, as, threads);
|
||||||
bench_mt(howmany, as, threads);
|
|
||||||
as->stop();
|
|
||||||
spdlog::drop("as");
|
spdlog::drop("as");
|
||||||
}
|
}
|
||||||
|
|
||||||
spdlog::stop();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (std::exception &ex)
|
catch (std::exception &ex)
|
||||||
{
|
{
|
||||||
|
@ -1,130 +0,0 @@
|
|||||||
/*************************************************************************/
|
|
||||||
/* spdlog - an extremely fast and easy to use c++11 logging library. */
|
|
||||||
/* Copyright (c) 2014 Gabi Melman. */
|
|
||||||
/* */
|
|
||||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
||||||
/* a copy of this software and associated documentation files (the */
|
|
||||||
/* "Software"), to deal in the Software without restriction, including */
|
|
||||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
||||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
||||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
||||||
/* the following conditions: */
|
|
||||||
/* */
|
|
||||||
/* The above copyright notice and this permission notice shall be */
|
|
||||||
/* included in all copies or substantial portions of the Software. */
|
|
||||||
/* */
|
|
||||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
||||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
||||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
||||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
||||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
||||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
||||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
||||||
/*************************************************************************/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
//Fast to int to string
|
|
||||||
//Base on :http://stackoverflow.com/a/4351484/192001
|
|
||||||
//Modified version to pad zeros according to padding arg
|
|
||||||
|
|
||||||
namespace spdlog
|
|
||||||
{
|
|
||||||
namespace details
|
|
||||||
{
|
|
||||||
|
|
||||||
const char digit_pairs[201] =
|
|
||||||
{
|
|
||||||
"00010203040506070809"
|
|
||||||
"10111213141516171819"
|
|
||||||
"20212223242526272829"
|
|
||||||
"30313233343536373839"
|
|
||||||
"40414243444546474849"
|
|
||||||
"50515253545556575859"
|
|
||||||
"60616263646566676869"
|
|
||||||
"70717273747576777879"
|
|
||||||
"80818283848586878889"
|
|
||||||
"90919293949596979899"
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
inline std::string& fast_itostr(int n, std::string& s, size_t padding)
|
|
||||||
{
|
|
||||||
if (n == 0)
|
|
||||||
{
|
|
||||||
s = std::string(padding, '0');
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
int sign = -(n < 0);
|
|
||||||
unsigned int val = static_cast<unsigned int>((n^sign) - sign);
|
|
||||||
|
|
||||||
size_t size;
|
|
||||||
if (val >= 10000)
|
|
||||||
{
|
|
||||||
if (val >= 10000000)
|
|
||||||
{
|
|
||||||
if (val >= 1000000000)
|
|
||||||
size = 10;
|
|
||||||
else if (val >= 100000000)
|
|
||||||
size = 9;
|
|
||||||
else
|
|
||||||
size = 8;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (val >= 1000000)
|
|
||||||
size = 7;
|
|
||||||
else if (val >= 100000)
|
|
||||||
size = 6;
|
|
||||||
else
|
|
||||||
size = 5;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (val >= 100)
|
|
||||||
{
|
|
||||||
if (val >= 1000)
|
|
||||||
size = 4;
|
|
||||||
else
|
|
||||||
size = 3;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (val >= 10)
|
|
||||||
size = 2;
|
|
||||||
else
|
|
||||||
size = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
size -= sign;
|
|
||||||
if (size < padding)
|
|
||||||
size = padding;
|
|
||||||
|
|
||||||
s.resize(size);
|
|
||||||
char* c = &s[0];
|
|
||||||
if (sign)
|
|
||||||
*c = '-';
|
|
||||||
|
|
||||||
c += size - 1;
|
|
||||||
while (val >= 100)
|
|
||||||
{
|
|
||||||
size_t pos = val % 100;
|
|
||||||
val /= 100;
|
|
||||||
*(short*)(c - 1) = *(short*)(digit_pairs + 2 * pos);
|
|
||||||
c -= 2;
|
|
||||||
}
|
|
||||||
while (val > 0)
|
|
||||||
{
|
|
||||||
*c-- = static_cast<char>('0' + (val % 10));
|
|
||||||
val /= 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (c >= s.data())
|
|
||||||
*c-- = '0';
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,204 +0,0 @@
|
|||||||
/*
|
|
||||||
A modified version of Intrusive MPSC node-based queue
|
|
||||||
|
|
||||||
Original code from
|
|
||||||
http://www.1024cores.net/home/lock-free-algorithms/queues/intrusive-mpsc-node-based-queue
|
|
||||||
licensed by Dmitry Vyukov under the terms below:
|
|
||||||
|
|
||||||
Simplified BSD license
|
|
||||||
|
|
||||||
Copyright (c) 2010-2011 Dmitry Vyukov. All rights reserved.
|
|
||||||
Redistribution and use in source and binary forms, with or without modification,
|
|
||||||
are permitted provided that the following conditions are met:
|
|
||||||
1. Redistributions of source code must retain the above copyright notice, this list of
|
|
||||||
conditions and the following disclaimer.
|
|
||||||
|
|
||||||
2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
||||||
of conditions and the following disclaimer in the documentation and/or other materials
|
|
||||||
provided with the distribution.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY DMITRY VYUKOV "AS IS" AND ANY EXPRESS OR IMPLIED
|
|
||||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
||||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
||||||
SHALL DMITRY VYUKOV OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
||||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
|
||||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
||||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
||||||
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
||||||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
|
|
||||||
The views and conclusions contained in the software and documentation are those of the authors and
|
|
||||||
should not be interpreted as representing official policies, either expressed or implied, of Dmitry Vyukov.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*************************************************************************/
|
|
||||||
/********* The code in its current form adds the license below: **********/
|
|
||||||
/*************************************************************************/
|
|
||||||
/* spdlog - an extremely fast and easy to use c++11 logging library. */
|
|
||||||
/* Copyright (c) 2014 Gabi Melman. */
|
|
||||||
/* */
|
|
||||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
||||||
/* a copy of this software and associated documentation files (the */
|
|
||||||
/* "Software"), to deal in the Software without restriction, including */
|
|
||||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
||||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
||||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
||||||
/* the following conditions: */
|
|
||||||
/* */
|
|
||||||
/* The above copyright notice and this permission notice shall be */
|
|
||||||
/* included in all copies or substantial portions of the Software. */
|
|
||||||
/* */
|
|
||||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
||||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
||||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
||||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
||||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
||||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
||||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
||||||
/*************************************************************************/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <atomic>
|
|
||||||
namespace spdlog
|
|
||||||
{
|
|
||||||
namespace details
|
|
||||||
{
|
|
||||||
template<typename T>
|
|
||||||
class mpsc_q
|
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
|
||||||
using item_type = T;
|
|
||||||
explicit mpsc_q(size_t max_size) :
|
|
||||||
_max_size(max_size),
|
|
||||||
_size(0),
|
|
||||||
_stub(),
|
|
||||||
_head(&_stub),
|
|
||||||
_tail(&_stub)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
mpsc_q(const mpsc_q&) = delete;
|
|
||||||
mpsc_q& operator=(const mpsc_q&) = delete;
|
|
||||||
|
|
||||||
~mpsc_q()
|
|
||||||
{
|
|
||||||
clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename TT>
|
|
||||||
bool push(TT&& val)
|
|
||||||
{
|
|
||||||
if (_size >= _max_size)
|
|
||||||
return false;
|
|
||||||
mpscq_node_t* new_node = new mpscq_node_t(std::forward<TT>(val));
|
|
||||||
push_node(new_node);
|
|
||||||
++_size;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to pop or return false immediatly is queue is empty
|
|
||||||
bool pop(T& value)
|
|
||||||
{
|
|
||||||
mpscq_node_t* node = pop_node();
|
|
||||||
if (node != nullptr)
|
|
||||||
{
|
|
||||||
--_size;
|
|
||||||
value = std::move(node->value);
|
|
||||||
delete(node);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Empty the queue by popping all its elements
|
|
||||||
void clear()
|
|
||||||
{
|
|
||||||
while (mpscq_node_t* node = pop_node())
|
|
||||||
{
|
|
||||||
--_size;
|
|
||||||
delete(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return approx size
|
|
||||||
size_t approx_size() const
|
|
||||||
{
|
|
||||||
return _size.load();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
struct mpscq_node_t
|
|
||||||
{
|
|
||||||
std::atomic<mpscq_node_t*> next;
|
|
||||||
T value;
|
|
||||||
|
|
||||||
mpscq_node_t() :next(nullptr) {}
|
|
||||||
mpscq_node_t(const mpscq_node_t&) = delete;
|
|
||||||
mpscq_node_t& operator=(const mpscq_node_t&) = delete;
|
|
||||||
|
|
||||||
explicit mpscq_node_t(const T& val):
|
|
||||||
next(nullptr),
|
|
||||||
value(val) {}
|
|
||||||
|
|
||||||
explicit mpscq_node_t(T&& val) :
|
|
||||||
next(nullptr),
|
|
||||||
value(std::move(val)) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
size_t _max_size;
|
|
||||||
std::atomic<size_t> _size;
|
|
||||||
mpscq_node_t _stub;
|
|
||||||
std::atomic<mpscq_node_t*> _head;
|
|
||||||
mpscq_node_t* _tail;
|
|
||||||
|
|
||||||
// Lockfree push
|
|
||||||
void push_node(mpscq_node_t* n)
|
|
||||||
{
|
|
||||||
n->next = nullptr;
|
|
||||||
mpscq_node_t* prev = _head.exchange(n);
|
|
||||||
prev->next = n;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clever lockfree pop algorithm by Dmitry Vyukov using single xchng instruction..
|
|
||||||
// Return pointer to the poppdc node or nullptr if no items left in the queue
|
|
||||||
mpscq_node_t* pop_node()
|
|
||||||
{
|
|
||||||
mpscq_node_t* tail = _tail;
|
|
||||||
mpscq_node_t* next = tail->next;
|
|
||||||
if (tail == &_stub)
|
|
||||||
{
|
|
||||||
if (nullptr == next)
|
|
||||||
return nullptr;
|
|
||||||
_tail = next;
|
|
||||||
tail = next;
|
|
||||||
next = next->next;
|
|
||||||
}
|
|
||||||
if (next)
|
|
||||||
{
|
|
||||||
_tail = next;
|
|
||||||
return tail;
|
|
||||||
}
|
|
||||||
mpscq_node_t* head = _head;
|
|
||||||
if (tail != head)
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
push_node(&_stub);
|
|
||||||
next = tail->next;
|
|
||||||
if (next)
|
|
||||||
{
|
|
||||||
_tail = next;
|
|
||||||
return tail;
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user