Extraction test!

[CJSON extraction] execution time: 589611 nanoseconds [Index offset] execution time: 207568 nanoseconds
mail@pastecode.io avatar
unknown
c_cpp
2 years ago
2.3 kB
2
Indexable
Never
class StopWatch {
    using TimePoint = std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>;

public:
    StopWatch(std::string &&n) : methodName_(n) { start_ = std::chrono::high_resolution_clock::now(); }
    ~StopWatch() {
        TimePoint end = std::chrono::high_resolution_clock::now();
        auto duration = end - start_;
        std::cout << "[" << methodName_ << "] execution time: " << duration.count() << " nanoseconds" << std::endl;
    }

private:
    std::string methodName_;
    TimePoint start_;
};

TEST_F(ShardingApi, ItemAccessTest) {
    const std::string kTestNs = "access_ns";
    const size_t kFields = 30;
    const std::string kFieldNamePref = "field_name_";
    NamespaceDef nsDef(kTestNs);
    nsDef.AddIndex(kFieldId, "hash", "int", IndexOpts().PK());
    for (size_t i = 0; i < kFields; ++i) {
        nsDef.AddIndex(kFieldNamePref + std::to_string(i), "hash", "string", IndexOpts());
    }
    Error err = rt.reindexer->AddNamespace(nsDef, lsn_t{});
    ASSERT_TRUE(err.ok()) << err.what();

    for (size_t i = 0; i < 1000; ++i) {
        Item item = rt.reindexer->NewItem(kTestNs);
        ASSERT_TRUE(item.Status().ok()) << item.Status().what();

        item[kFieldId] = (int)i;
        for (size_t i = 0; i < kFields; ++i) {
            item[kFieldNamePref + std::to_string(i)] = RandString();
        }

        err = rt.reindexer->Upsert(kTestNs, item);
        ASSERT_TRUE(err.ok()) << err.what();
    }

    QueryResults qr;
    err = rt.reindexer->Select(Query(kTestNs).Where(kFieldId, CondEq, Variant(int(1))), qr);
    ASSERT_TRUE(qr.Count() == 1);
    ASSERT_TRUE(err.ok()) << err.what();

    const size_t kTestIterations = 1000;
    const std::string kExtractedField = kFieldNamePref + "15";
    const size_t kExtractedFieldIDx = 16;
    Item testItem = qr[0].GetItem();

    {
        StopWatch stopWatch("CJSON extraction");
        for (size_t i = 0; i < kTestIterations; ++i) {
            Variant value = testItem[kExtractedField];
            (void)value;
        }
    }

    {
        StopWatch stopWatch("Index offset");
        for (size_t i = 0; i < kTestIterations; ++i) {
            Variant value = testItem[kExtractedFieldIDx];
            (void)value;
        }
    }
}