Skip to content

Commit 371ce9a

Browse files
author
ubuntu
committed
update example/src/test_cpu_usage/main.cpp
1 parent f0b96d2 commit 371ce9a

2 files changed

Lines changed: 36 additions & 28 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Notice
2-
* New release has 15% reduction in cpu usage.
2+
* New release has about 20% reduction in cpu usage.
33

44
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
55

example/src/test_cpu_usage/main.cpp

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ using ::ccapi::Session;
3535
using ::ccapi::SessionConfigs;
3636
using ::ccapi::SessionOptions;
3737
using ::ccapi::Subscription;
38+
using ::ccapi::UtilString;
3839
using ::ccapi::UtilSystem;
3940

4041
struct Ticker {
@@ -43,46 +44,53 @@ struct Ticker {
4344
};
4445

4546
int main(int argc, char** argv) {
46-
const auto& numSymbols = UtilSystem::getEnvAsInt("NUM_SYMBOLS", 25);
47+
const auto& numSymbols = UtilSystem::getEnvAsInt("NUM_SYMBOLS", 50);
48+
std::vector<std::string> instruments = UtilString::split(UtilSystem::getEnvAsString("SYMBOLS"), ",");
49+
instruments.erase(std::remove_if(instruments.begin(), instruments.end(), [](const std::string& s) { return s.empty(); }), instruments.end());
4750
bool subscribeMarketDepth = UtilSystem::getEnvAsBool("SUBSCRIBE_MARKET_DEPTH");
4851
bool subscribeMarketDepth50 = UtilSystem::getEnvAsBool("SUBSCRIBE_MARKET_DEPTH_50");
4952
bool subscribeMarketDepth400 = UtilSystem::getEnvAsBool("SUBSCRIBE_MARKET_DEPTH_400");
5053
bool subscribeTrade = UtilSystem::getEnvAsBool("SUBSCRIBE_TRADE");
5154
size_t subscriptionDataEventPrintCount = UtilSystem::getEnvAsInt("SUBSCRIPTION_DATA_EVENT_PRINT_COUNT", 10000);
5255
SessionOptions sessionOptions;
53-
sessionOptions.enableCheckOrderBookChecksum = true;
5456
SessionConfigs sessionConfigs;
5557
MyEventHandler eventHandler(subscriptionDataEventPrintCount);
5658
Session session(sessionOptions, sessionConfigs, &eventHandler);
5759
std::string exchange = "okx";
58-
Request request(Request::Operation::GET_TICKERS, exchange);
59-
request.appendParam({
60-
{"INSTRUMENT_TYPE", "SPOT"},
61-
});
62-
Queue<Event> eventQueue;
63-
session.sendRequest(request, &eventQueue);
64-
std::vector<Event> eventList = eventQueue.purge();
65-
const auto& message = eventList.front().getMessageList().front();
66-
std::vector<Ticker> tickers;
67-
for (const auto& element : message.getElementList()) {
68-
const auto& instrument = element.getValue("INSTRUMENT");
69-
const auto& volume24h = std::stod(element.getValue("VOLUME_24H"));
70-
if (volume24h > 0) {
71-
const auto& averagePrice = (std::stod(element.getValue("OPEN_24H_PRICE")) + std::stod(element.getValue("HIGH_24H_PRICE")) +
72-
std::stod(element.getValue("LOW_24H_PRICE")) + std::stod(element.getValue("LAST_PRICE"))) /
73-
4;
74-
const auto& quoteVolume24h = averagePrice * volume24h;
75-
tickers.push_back({instrument, quoteVolume24h});
60+
61+
if (instruments.empty()) {
62+
Request request(Request::Operation::GET_TICKERS, exchange);
63+
request.appendParam({
64+
{"INSTRUMENT_TYPE", "SPOT"},
65+
});
66+
Queue<Event> eventQueue;
67+
session.sendRequest(request, &eventQueue);
68+
std::vector<Event> eventList = eventQueue.purge();
69+
const auto& message = eventList.front().getMessageList().front();
70+
std::vector<Ticker> tickers;
71+
for (const auto& element : message.getElementList()) {
72+
const auto& instrument = element.getValue("INSTRUMENT");
73+
const auto& volume24h = std::stod(element.getValue("VOLUME_24H"));
74+
if (volume24h > 0) {
75+
const auto& averagePrice = (std::stod(element.getValue("OPEN_24H_PRICE")) + std::stod(element.getValue("HIGH_24H_PRICE")) +
76+
std::stod(element.getValue("LOW_24H_PRICE")) + std::stod(element.getValue("LAST_PRICE"))) /
77+
4;
78+
const auto& quoteVolume24h = averagePrice * volume24h;
79+
tickers.push_back({instrument, quoteVolume24h});
80+
}
81+
}
82+
std::sort(tickers.begin(), tickers.end(), [](const Ticker& a, const Ticker& b) {
83+
return a.quoteVolume24h > b.quoteVolume24h; // descending
84+
});
85+
std::vector<Ticker> topNTickers;
86+
topNTickers.assign(tickers.begin(), tickers.begin() + std::min(numSymbols, static_cast<int>(tickers.size())));
87+
for (const auto& ticker : topNTickers) {
88+
instruments.push_back(ticker.instrument);
7689
}
7790
}
78-
std::sort(tickers.begin(), tickers.end(), [](const Ticker& a, const Ticker& b) {
79-
return a.quoteVolume24h > b.quoteVolume24h; // descending
80-
});
81-
std::vector<Ticker> topNTickers;
82-
topNTickers.assign(tickers.begin(), tickers.begin() + std::min(numSymbols, static_cast<int>(tickers.size())));
91+
std::cout << "instruments = " + UtilString::join(instruments, ",") << std::endl;
8392
std::vector<Subscription> subscriptionList;
84-
for (const auto& ticker : topNTickers) {
85-
const auto& instrument = ticker.instrument;
93+
for (const auto& instrument : instruments) {
8694
if (subscribeMarketDepth) {
8795
Subscription subscription(exchange, instrument, "MARKET_DEPTH");
8896
subscriptionList.push_back(subscription);

0 commit comments

Comments
 (0)