2016-08-02 01:46:05 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
2016-08-02 01:46:05 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Stopwatch.h>
|
|
|
|
#include <Common/Exception.h>
|
2019-01-11 19:12:36 +00:00
|
|
|
#include <Common/ThreadPool.h>
|
2016-08-02 01:46:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void f() { ++x; }
|
2016-08-02 01:46:05 +00:00
|
|
|
|
|
|
|
/*void f()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::vector<std::string> vec;
|
|
|
|
for (size_t i = 0; i < 100; ++i)
|
|
|
|
vec.push_back(std::string(rand() % 10, ' '));
|
2016-08-02 01:46:05 +00:00
|
|
|
}*/
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void * g(void *) { f(); return {}; }
|
2016-08-02 01:46:05 +00:00
|
|
|
|
|
|
|
|
2018-11-21 20:56:37 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int PTHREAD_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-02 01:46:05 +00:00
|
|
|
template <typename F>
|
|
|
|
void test(size_t n, const char * name, F && kernel)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
x = 0;
|
2016-08-02 01:46:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Stopwatch watch;
|
|
|
|
Stopwatch watch_one;
|
|
|
|
double max_seconds = 0;
|
2016-08-02 01:46:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::cerr << name << ":\n";
|
2016-08-02 01:46:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
watch_one.restart();
|
2016-08-02 01:46:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
kernel();
|
2016-08-02 01:46:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
watch_one.stop();
|
|
|
|
if (watch_one.elapsedSeconds() > max_seconds)
|
|
|
|
max_seconds = watch_one.elapsedSeconds();
|
|
|
|
}
|
2016-08-02 01:46:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
watch.stop();
|
2016-08-02 01:46:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::cerr
|
|
|
|
<< std::fixed << std::setprecision(2)
|
|
|
|
<< n << " ops in "
|
|
|
|
<< watch.elapsedSeconds() << " sec., "
|
|
|
|
<< n / watch.elapsedSeconds() << " ops/sec., "
|
|
|
|
<< "avg latency: " << watch.elapsedSeconds() / n * 1000000 << " μs, "
|
|
|
|
<< "max latency: " << max_seconds * 1000000 << " μs "
|
|
|
|
<< "(res = " << x << ")"
|
|
|
|
<< std::endl;
|
2016-08-02 01:46:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t n = argc == 2 ? DB::parse<UInt64>(argv[1]) : 100000;
|
|
|
|
|
|
|
|
/* test(n, "Create and destroy boost::threadpool each iteration", []
|
|
|
|
{
|
|
|
|
boost::threadpool::pool tp(1);
|
|
|
|
tp.schedule(f);
|
|
|
|
tp.wait();
|
|
|
|
});*/
|
|
|
|
|
|
|
|
test(n, "Create and destroy ThreadPool each iteration", []
|
|
|
|
{
|
|
|
|
ThreadPool tp(1);
|
|
|
|
tp.schedule(f);
|
|
|
|
tp.wait();
|
|
|
|
});
|
|
|
|
|
|
|
|
test(n, "pthread_create, pthread_join each iteration", []
|
|
|
|
{
|
|
|
|
pthread_t thread;
|
|
|
|
if (pthread_create(&thread, nullptr, g, nullptr))
|
2018-11-21 20:56:37 +00:00
|
|
|
DB::throwFromErrno("Cannot create thread.", DB::ErrorCodes::PTHREAD_ERROR);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (pthread_join(thread, nullptr))
|
2018-11-21 20:56:37 +00:00
|
|
|
DB::throwFromErrno("Cannot join thread.", DB::ErrorCodes::PTHREAD_ERROR);
|
2017-04-01 07:20:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test(n, "Create and destroy std::thread each iteration", []
|
|
|
|
{
|
|
|
|
std::thread thread(f);
|
|
|
|
thread.join();
|
|
|
|
});
|
|
|
|
|
|
|
|
/* {
|
|
|
|
boost::threadpool::pool tp(1);
|
|
|
|
|
|
|
|
test(n, "Schedule job for boost::threadpool each iteration", [&tp]
|
|
|
|
{
|
|
|
|
tp.schedule(f);
|
|
|
|
tp.wait();
|
|
|
|
});
|
|
|
|
}*/
|
|
|
|
|
|
|
|
{
|
|
|
|
ThreadPool tp(1);
|
|
|
|
|
|
|
|
test(n, "Schedule job for Threadpool each iteration", [&tp]
|
|
|
|
{
|
|
|
|
tp.schedule(f);
|
|
|
|
tp.wait();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/* {
|
|
|
|
boost::threadpool::pool tp(128);
|
|
|
|
|
|
|
|
test(n, "Schedule job for boost::threadpool with 128 threads each iteration", [&tp]
|
|
|
|
{
|
|
|
|
tp.schedule(f);
|
|
|
|
tp.wait();
|
|
|
|
});
|
|
|
|
}*/
|
|
|
|
|
|
|
|
{
|
|
|
|
ThreadPool tp(128);
|
|
|
|
|
|
|
|
test(n, "Schedule job for Threadpool with 128 threads each iteration", [&tp]
|
|
|
|
{
|
|
|
|
tp.schedule(f);
|
|
|
|
tp.wait();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2016-08-02 01:46:05 +00:00
|
|
|
}
|