#include "Benchmark.h" #include "lib/api/services/OrderService.server.pb.h" #include "lib/utils/PBUtils.h" #include "lib/utils/Puid.h" #include "lib/utils/SpinLock.h" #include "lib/utils/Uuid.h" #include "server/sparkd/cmds/CmdRegistry.h" #include "lib/jetfix/jetfix.h" #include "lib/mercury/broker/CoreEventBroker.h" #include "lib/mercury/mercury_utils/FatWrapper.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace bts { namespace cmd { auto sec_str = " id: 3\n" " symbol: \"ES\"\n" " exchange: VENUE_GLBX\n" " contract_spec {\n" " low_tick: 0.25\n" " high_tick: 0.25\n" " cash_per_point: 50\n" " price_multiplier: 0.01\n" " }\n" " future {\n" " expiration_year: 2018\n" " expiration_month: 9\n" " expiration_day: 1\n" " }\n" " glbx {\n" " exchange_id: 57287\n" " security_desc: \"ESU8\"\n" " cfi_code: \"FFIXSX\"\n" " cme_symbol: \"ES\"\n" " }\n"; auto order_str = " order_key: 189285\n" " timestamp {\n" " seconds: 1509644176\n" " nanos: 717250000\n" " }\n" " native {\n" " account_name: \"BTS \"\n" " operator_id: \"BTS \"\n" " security_key: 3\n" " side: SIDE_BUY\n" " time_in_force: TIF_DAY\n" " manual_order_indicator: MOI_YES\n" " order_type: OT_LIMIT\n" " price: 2500\n" " order_quantity: 12\n" " }\n" " customer_or_firm: COF_FIRM\n" " cti_code: CTI_CTI1\n" " order_status: OS_UNACKNOWLEDGED\n"; auto order_state_str = " status: ORDER_STATUS_COMPLETED\n" " price: 65.56\n" " quantity: 5\n" " filled_quantity: 5\n" " last_price: 65.56\n" " avg_price: 65.56\n" " last_change {\n" " seconds: 1532982945\n" " nanos: 378466000\n" " }\n" " limit {\n" " native_state {\n" " exchange_order_id: \"12345678901234567\"\n" " native_status: NATIVE_ORDER_STATUS_FILLED\n" " last_msg_seq_num: 868\n" " routing_record {\n" " session: \"sim\"\n" " account: \"ca_ACC A\"\n" " operator_id: \"op_id_bts\"\n" " cti_code: CTI_CODE_CTI_CODE1\n" " customer_or_firm: CUSTOMER_OR_FIRM_FIRM\n" " location: \"us,nc\"\n" " }\n" " native_type: NATIVE_ORDER_TYPE_LIMIT\n" " }\n" " }\n" " created {\n" " seconds: 1532962902\n" " nanos: 989007000\n" " }\n"; 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_order(benchmark::State &state) { // auto order_state = // utils::pb::from_string(order_state_str); // auto // auto order_state_cpy = order_state; // for (auto _ : state) { // order_state_cpy = order_state; // } // } // BENCHMARK(copy_order); 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); static void serialize_order(benchmark::State &state) { auto order_state = utils::pb::from_string(order_state_str); std::string buffer; buffer.resize(order_state.ByteSize()); bts_assert(order_state.SerializeToArray(&buffer[0], buffer.size())); for (auto _ : state) { order_state.SerializeWithCachedSizesToArray((unsigned char*) &buffer[0]); } } BENCHMARK(serialize_order); 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; } struct Notify { mercury::EventBrokerID id; mercury::SubID suid; mercury::StreamID sid; utils::FatWrapper<80> payload; }; static void queue_notification(benchmark::State &state) { std::array, 1> queue; for (auto _ : state) { auto &n = queue[0].wrap_in_place(); n.id = mercury::EventBrokerID(1); n.suid = mercury::SubID(1); n.sid = mercury::StreamID(1); auto &a = n.payload.wrap_in_place(); a.x = 1; benchmark::DoNotOptimize(queue[0].empty()); } } BENCHMARK(queue_notification); static void queue_notification2(benchmark::State &state) { std::array, 2> queue; auto &n = queue[0].wrap_in_place(); n.id = mercury::EventBrokerID(1); n.suid = mercury::SubID(1); n.sid = mercury::StreamID(1); auto &a = n.payload.wrap_in_place(); a.x = 1; for (auto _ : state) { queue[1] = queue[0]; benchmark::DoNotOptimize(queue[1].empty()); } } BENCHMARK(queue_notification2); 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 moody_camel_queue(benchmark::State &state) { using Q = moodycamel::ConcurrentQueue; Q queue(1000000); for (auto _ : state) { void *result; void *arg = &queue; queue.enqueue(arg); // NOLINT benchmark::DoNotOptimize(queue.try_dequeue(result)); } } BENCHMARK(moody_camel_queue); static void moody_camel_fat_push(benchmark::State &state) { using Msg = utils::FatWrapper<80>; using Q = moodycamel::ConcurrentQueue; Q queue; Msg fw; auto &n = fw.wrap_in_place(); n.id = mercury::EventBrokerID(1); n.suid = mercury::SubID(1); n.sid = mercury::StreamID(1); auto &a = n.payload.wrap_in_place(); a.x = 1; for (auto _ : state) { queue.enqueue(fw); Msg fw2; benchmark::DoNotOptimize(queue.try_dequeue(fw2)); } } BENCHMARK(moody_camel_fat_push); 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 = utils::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 moody_camel_slim_push(benchmark::State &state) { using Q = moodycamel::ConcurrentQueue; Q queue; PPQueue store; store.v.reserve(10); Msg fw; auto &n = fw.wrap_in_place(); n.id = mercury::EventBrokerID(1); n.suid = mercury::SubID(1); n.sid = mercury::StreamID(1); auto &a = n.payload.wrap_in_place(); a.x = 1; store.push(&fw); for (auto _ : state) { Msg *fw_ptr = nullptr; store.pop(&fw_ptr); auto &n = fw_ptr->wrap_in_place(); n.id = mercury::EventBrokerID(1); n.suid = mercury::SubID(1); n.sid = mercury::StreamID(1); auto &a = n.payload.wrap_in_place(); a.x = 1; queue.enqueue(fw_ptr); queue.try_dequeue(fw_ptr); store.push(fw_ptr); benchmark::DoNotOptimize(fw_ptr); } } BENCHMARK(moody_camel_slim_push); using EventBrokerID = uint32_t; using StreamID = uint32_t; using SubID = uint32_t; using TopicPtr = std::shared_ptr; using IEventBrokerPtr = TopicPtr; using TopicFilter = std::function; using InternalEventHandler = TopicFilter; using InternalSubID = uint32_t; struct NeighbourAddition { NeighbourAddition() : ptr() {} IEventBrokerPtr ptr; }; struct NeighbourRemoval { NeighbourRemoval() : id() {} EventBrokerID id; }; struct Advertisement { Advertisement() : id(), sid(), topic() {} EventBrokerID id; StreamID sid; TopicPtr topic; }; struct Unadvertisement { Unadvertisement() : id(), sid() {} EventBrokerID id; StreamID sid; }; struct Subscription { Subscription() : id(), suid(), sid() {} EventBrokerID id; SubID suid; StreamID sid; }; struct Unsubscription { Unsubscription() : id(), suid(), sid() {} EventBrokerID id; SubID suid; StreamID sid; }; struct InternalSubscription { InternalSubscription() : suid(), filter(), handler() {} InternalSubID suid; TopicFilter filter; InternalEventHandler handler; }; struct InternalUnsubscription { InternalUnsubscription() : suid() {} InternalSubID suid; }; template static void lambda_alloc_copy(benchmark::State &state) { utils::FatWrapper<128> fw; using Action = std::function; T t; for (auto _ : state) { benchmark::DoNotOptimize( fw.wrap_in_place([]() { return sizeof(T); })); } } BENCHMARK_TEMPLATE(lambda_alloc_copy, NeighbourAddition); BENCHMARK_TEMPLATE(lambda_alloc_copy, NeighbourRemoval); BENCHMARK_TEMPLATE(lambda_alloc_copy, Advertisement); BENCHMARK_TEMPLATE(lambda_alloc_copy, Unadvertisement); BENCHMARK_TEMPLATE(lambda_alloc_copy, Subscription); BENCHMARK_TEMPLATE(lambda_alloc_copy, Unsubscription); BENCHMARK_TEMPLATE(lambda_alloc_copy, InternalSubscription); BENCHMARK_TEMPLATE(lambda_alloc_copy, InternalUnsubscription); template static void simple_alloc_copy(benchmark::State &state) { utils::FatWrapper<128> fw; for (auto _ : state) { benchmark::DoNotOptimize(fw.wrap_in_place()); } } BENCHMARK_TEMPLATE(simple_alloc_copy, NeighbourAddition); BENCHMARK_TEMPLATE(simple_alloc_copy, NeighbourRemoval); BENCHMARK_TEMPLATE(simple_alloc_copy, Advertisement); BENCHMARK_TEMPLATE(simple_alloc_copy, Unadvertisement); BENCHMARK_TEMPLATE(simple_alloc_copy, Subscription); BENCHMARK_TEMPLATE(simple_alloc_copy, Unsubscription); BENCHMARK_TEMPLATE(simple_alloc_copy, InternalSubscription); BENCHMARK_TEMPLATE(simple_alloc_copy, InternalUnsubscription); 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); std::string Benchmark::help() { return "Run micro-benchmarks. Use --benchmark_filter=regex to run only " "specific benchmarks."; } } // namespace cmd } // namespace bts