#include "Benchmark.h" #include "lib/api/services/OrderService.server.pb.h" #include "lib/jetfix/jetfix.h" #include "lib/md/PriceSize.h" #include "lib/mercury/actors/Actor.h" #include "lib/mercury/common/FatWrapper.h" #include "lib/mercury/executor/Executor.h" #include "lib/orderman/events/OrderStateUpdateEvent.h" #include "lib/os/os.h" #include "lib/utils/PBUtils.h" #include "lib/utils/Puid.h" #include "lib/utils/SpinLock.h" #include "lib/utils/TimeStamp.h" #include "lib/utils/Uuid.h" #include "lib/utils/mpmc_bounded_queue.h" #include "server/sparkd/cmds/CmdRegistry.h" #include "lib/tracing/PerfLog.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace bts { namespace cmd { using namespace orderman; static void test_date_conversion(benchmark::State &state) { auto lldt = datetime::ll_now(); for (auto _ : state) { benchmark::DoNotOptimize(datetime::utc_ll_to_timepoint(lldt)); } } BENCHMARK(test_date_conversion); static void gen_id(benchmark::State &state) { std::string scope = "benchmark"; for (auto _ : state) { auto id = utils::reserve_ids(scope, 1); benchmark::DoNotOptimize(id + 1 > 0); } } BENCHMARK(gen_id); static void mutex_lock_unlock(benchmark::State &state) { int i = 0; std::mutex lock; for (auto _ : state) { std::lock_guard l(lock); benchmark::DoNotOptimize(i++); } } BENCHMARK(mutex_lock_unlock); static void spin_lock_unlock(benchmark::State &state) { int i = 0; std::atomic_flag _mutex = ATOMIC_FLAG_INIT; for (auto _ : state) { while (_mutex.test_and_set(std::memory_order_acquire)) ; benchmark::DoNotOptimize(i++); _mutex.clear(std::memory_order_release); } } BENCHMARK(spin_lock_unlock); static void bts_spinlock_lock_unlock(benchmark::State &state) { int i = 0; utils::SpinLock l; for (auto _ : state) { std::lock_guard g(l); benchmark::DoNotOptimize(i++); } } BENCHMARK(bts_spinlock_lock_unlock); static void serialize_update(benchmark::State &state) { api::orderman::Update update; update.set_order_key(1); update.set_price(1.2); update.set_quantity(2); std::string buffer; buffer.resize(update.ByteSize()); bts_assert(update.SerializeToArray(&buffer[0], buffer.size())); for (auto _ : state) { update.SerializeWithCachedSizesToArray((unsigned char *)&buffer[0]); } } BENCHMARK(serialize_update); struct IntId { IntId() { value = std::rand(); } bool operator<(IntId const &v) const { return value < v.value; } uint32_t value; }; static void binary_search_of_guid(benchmark::State &state) { using Id = utils::Uuid; using Map = boost::container::flat_map; auto sz = state.range(0); Map m; for (int i = 0; i < sz; i++) m[Id()] = i; auto target = Id(); int found_count = 0; for (auto _ : state) { auto it = m.find(target); if (it != m.end()) found_count++; benchmark::DoNotOptimize(found_count); } } BENCHMARK(binary_search_of_guid)->Range(8, 8 << 10); static void binary_search_of_int64(benchmark::State &state) { using Id = IntId; using Map = boost::container::flat_map; auto sz = state.range(0); Map m; for (int i = 0; i < sz; i++) m[Id()] = i; auto target = Id(); int found_count = 0; for (auto _ : state) { auto it = m.find(target); if (it != m.end()) found_count++; benchmark::DoNotOptimize(found_count); } } BENCHMARK(binary_search_of_int64)->Range(8, 8 << 10); static void binary_search_of_int32(benchmark::State &state) { using Id = IntId; using Map = boost::container::flat_map; auto sz = state.range(0); Map m; for (int i = 0; i < sz; i++) m[Id()] = i; auto target = Id(); int found_count = 0; for (auto _ : state) { auto it = m.find(target); if (it != m.end()) found_count++; benchmark::DoNotOptimize(found_count); } } BENCHMARK(binary_search_of_int32)->Range(8, 8 << 10); static void guid_gen(benchmark::State &state) { for (auto _ : state) { benchmark::DoNotOptimize(utils::Uuid()); } } BENCHMARK(guid_gen); struct A { int x; }; static void shared_ptr_cpy(benchmark::State &state) { auto a = std::make_shared(); for (auto _ : state) { std::shared_ptr b; benchmark::DoNotOptimize(b = a); } } BENCHMARK(shared_ptr_cpy); static void alloc_free(benchmark::State &state) { for (auto _ : state) { auto a = std::make_shared(); benchmark::DoNotOptimize(a.get()); } } BENCHMARK(alloc_free); static void new_delete(benchmark::State &state) { for (auto _ : state) { auto a = new A(); delete a; benchmark::DoNotOptimize(a); } } BENCHMARK(new_delete); static void boost_pool(benchmark::State &state) { boost::pool<> p(sizeof(A)); for (auto _ : state) { auto a = p.malloc(); p.free(a); benchmark::DoNotOptimize(a); } } BENCHMARK(boost_pool); static void boost_object_pool(benchmark::State &state) { boost::object_pool p; for (auto _ : state) { auto a = p.malloc(); p.free(a); benchmark::DoNotOptimize(a); } } BENCHMARK(boost_object_pool); static void protected_boost_pool(benchmark::State &state) { boost::pool<> p(sizeof(A)); std::atomic_flag _mutex = ATOMIC_FLAG_INIT; for (auto _ : state) { while (_mutex.test_and_set(std::memory_order_acquire)) ; auto a = p.malloc(); _mutex.clear(std::memory_order_release); while (_mutex.test_and_set(std::memory_order_acquire)) ; p.free(a); _mutex.clear(std::memory_order_release); benchmark::DoNotOptimize(a); } } BENCHMARK(protected_boost_pool); static void boost_singleton_pool(benchmark::State &state) { using Pool = boost::singleton_pool; for (auto _ : state) { auto a = Pool::malloc(); Pool::free(a); benchmark::DoNotOptimize(a); } } BENCHMARK(boost_singleton_pool); static void atomic_inc(benchmark::State &state) { std::atomic i(0); for (auto _ : state) { benchmark::DoNotOptimize(i++); } } BENCHMARK(atomic_inc); static void mem_copy(benchmark::State &state) { char *src = new char[state.range(0)]; char *dst = new char[state.range(0)]; memset(src, 'x', state.range(0)); for (auto _ : state) memcpy(dst, src, state.range(0)); state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(state.range(0))); delete[] src; delete[] dst; } BENCHMARK(mem_copy)->Arg(8)->Arg(64)->Arg(128)->Arg(512)->Arg(1 << 10)->Arg( 8 << 10); static void alloc_and_copy_from_pool(benchmark::State &state) { boost::pool<> p(sizeof(A)); for (auto _ : state) { auto a = new (p.malloc()) A(); // NOLINT a->~A(); // NOLINT p.free(a); benchmark::DoNotOptimize(a); } } BENCHMARK(alloc_and_copy_from_pool); static void lambda_copy(benchmark::State &state) { auto p = std::make_shared(); std::function f = [p]() { return 1; }; std::array, 1> buffer; for (auto _ : state) { benchmark::DoNotOptimize(buffer[0] = f); } } BENCHMARK(lambda_copy); static void boost_pool_allocator(benchmark::State &state) { boost::fast_pool_allocator p; for (auto _ : state) { auto ptr = std::allocate_shared(p); benchmark::DoNotOptimize(ptr.get()); } } BENCHMARK(boost_pool_allocator); int Benchmark::execute(std::vector const &args) { bts_assert(args.size() < 99); int argc = args.size() + 1; char *argv[100]; argv[0] = (char *)"spark"; for (size_t i = 0; i < args.size(); i++) argv[i + 1] = (char *)args[i].c_str(); benchmark::Initialize(&argc, argv); benchmark::RunSpecifiedBenchmarks(); return 0; } static void boost_lockless_queue(benchmark::State &state) { using FreeList = boost::lockfree::queue; FreeList queue(1000000); for (auto _ : state) { void *result; queue.push(nullptr); benchmark::DoNotOptimize(queue.pop(result)); } } BENCHMARK(boost_lockless_queue); static void tbb_concurrent_queue(benchmark::State &state) { using FreeList = tbb::concurrent_queue; FreeList queue; for (auto _ : state) { void *result; queue.push(nullptr); benchmark::DoNotOptimize(queue.try_pop(result)); } } BENCHMARK(tbb_concurrent_queue); static void boost_spsc(benchmark::State &state) { boost::lockfree::spsc_queue q(10000); int i = 0; for (auto _ : state) { q.push(++i); benchmark::DoNotOptimize(q.pop(i)); } } BENCHMARK(boost_spsc); static void boost_lockless_stack(benchmark::State &state) { using FreeList = boost::lockfree::stack; FreeList stack; for (auto _ : state) { void *result; stack.push(nullptr); benchmark::DoNotOptimize(stack.pop(result)); } } BENCHMARK(boost_lockless_stack); using Msg = mercury::FatWrapper<80>; struct PPQueue { std::atomic_flag _mutex = ATOMIC_FLAG_INIT; std::vector v; void push(Msg *e) { while (_mutex.test_and_set(std::memory_order_acquire)) ; v.push_back(e); _mutex.clear(std::memory_order_release); } bool pop(Msg **e) { auto result = false; while (_mutex.test_and_set(std::memory_order_acquire)) ; if (v.size()) { *e = v.back(); v.pop_back(); result = true; } _mutex.clear(std::memory_order_release); return result; } }; static void get_thread_id(benchmark::State &state) { for (auto _ : state) { auto tid = std::this_thread::get_id(); benchmark::DoNotOptimize(tid == std::this_thread::get_id()); } } BENCHMARK(get_thread_id); static void native_order_fix_data_create(benchmark::State &state) { auto sz = state.range(0); for (auto _ : state) { auto p = new uint8_t[sz]; delete[] p; benchmark::DoNotOptimize(p); } } BENCHMARK(native_order_fix_data_create)->Range(8, 8 << 14); static void native_order_fix_data_fragment(benchmark::State &state) { for (auto _ : state) { auto p = new jetfix::FIXFragment(); delete p; benchmark::DoNotOptimize(p); } } BENCHMARK(native_order_fix_data_fragment); static void small_vec(benchmark::State &state) { auto i = 0; for (auto _ : state) { boost::container::small_vector vec; vec.emplace_back(i++); benchmark::DoNotOptimize(vec[0]); } } BENCHMARK(small_vec); static void inter_thread_spsc(benchmark::State &state) { boost::lockfree::spsc_queue q1(10); boost::lockfree::spsc_queue q2(10); auto t2 = std::thread([&q1, &q2]() { int i = 1; cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(1, &cpuset); pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); do { while (!q1.pop(i)) ; q2.push(i); } while (i); }); cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(0, &cpuset); pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); sleep_for(2s); int i = 0; for (auto _ : state) { q1.push(++i); while (!q2.pop(i)) ; } q1.push(0); t2.join(); } BENCHMARK(inter_thread_spsc); static void inter_thread_mpmc(benchmark::State &state) { using Queue = boost::lockfree::queue; Queue q1(100), q2(100); auto t2 = std::thread([&q1, &q2]() { int i = 1; cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(1, &cpuset); pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); do { while (!q1.pop(i)) ; q2.push(i); } while (i); }); cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(0, &cpuset); pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); sleep_for(2s); int i = 0; for (auto _ : state) { q1.push(++i); while (!q2.pop(i)) ; } q1.push(0); t2.join(); } BENCHMARK(inter_thread_mpmc); static void inter_thread_mpmc_spdlog(benchmark::State &state) { using Queue = spdlog::details::mpmc_bounded_queue; Queue q1(8), q2(8); auto t2 = std::thread([&q1, &q2]() { int i = 1; cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(1, &cpuset); pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); do { while (!q1.dequeue(i)) ; q2.enqueue(i); } while (i); }); cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(0, &cpuset); pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); sleep_for(2s); int i = 0; for (auto _ : state) { q1.enqueue(++i); while (!q2.dequeue(i)) ; } q1.enqueue(0); t2.join(); } BENCHMARK(inter_thread_mpmc_spdlog); static void same_thread_mpmc_spdlog(benchmark::State &state) { using Queue = spdlog::details::mpmc_bounded_queue; Queue q1(8); int i = 0; for (auto _ : state) { q1.enqueue(++i); q1.dequeue(i); } } BENCHMARK(same_thread_mpmc_spdlog); static void moodycamel_concurrent_queue(benchmark::State &state) { using Q = moodycamel::ConcurrentQueue; Q queue(8); int i = 0; for (auto _ : state) { queue.enqueue(++i); // NOLINT benchmark::DoNotOptimize(queue.try_dequeue(i)); } } BENCHMARK(moodycamel_concurrent_queue); static void moodycamel_spsc_queue(benchmark::State &state) { using Queue = moodycamel::ReaderWriterQueue; Queue q1(8); int i = 0; for (auto _ : state) { q1.enqueue(++i); benchmark::DoNotOptimize(q1.try_dequeue(i)); } } BENCHMARK(moodycamel_spsc_queue); struct MyBase { virtual int inc(int &x) = 0; }; struct MyDerived : MyBase { int inc(int &x) override { return x += 1; }; }; struct MyDirect { int inc(int &x) { return x += 1; }; }; static void derived_call(benchmark::State &state) { int x = 0; MyDerived d; MyBase &b = d; for (auto _ : state) { benchmark::DoNotOptimize(b.inc(x)); } } BENCHMARK(derived_call); static void direct_call(benchmark::State &state) { int x = 0; MyDerived b; for (auto _ : state) { benchmark::DoNotOptimize(b.inc(x)); } } BENCHMARK(direct_call); static void get_time_of_day(benchmark::State &state) { std::string result(strlen("20121204-14:44:02.999"), ' '); struct timeval tv; for (auto _ : state) { gettimeofday(&tv, nullptr); auto tm = localtime(&tv.tv_sec); sprintf(&result[0], "%d%02d%02d-%02d:%02d:%02d.%03d", tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, 0); } } BENCHMARK(get_time_of_day); static void fix_time_stamp(benchmark::State &state) { std::string r(128, 32); auto now = datetime::now(); for (auto _ : state) { datetime::FIX_timestamp(now, &r); } } BENCHMARK(fix_time_stamp); static void perf_log(benchmark::State &state) { trace::PerfLog l; for (auto _ : state) { l.log(trace::CorID{"sim", 0}, "this", 1, "txt", 3); } } BENCHMARK(perf_log); void int_price_less(benchmark::State& state) { for (auto _ : state) { state.PauseTiming(); int64_t count = state.range(0); std::vector prices; prices.reserve((int)count); for (int64_t x = 0; x < count; ++x) { prices.push_back({x % 100, (int8_t)-(x%5)}); } state.ResumeTiming(); std::sort(prices.begin(), prices.end()); } } BENCHMARK(int_price_less)->Range(8, 8 << 11); std::string Benchmark::help() { return "Run micro-benchmarks. Use --benchmark_filter=regex to run only " "specific benchmarks."; } } // namespace cmd } // namespace bts