xref: /6.6.0/kv_engine/tracing/tracing_test.cc (revision ee5212ee)
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2017 Couchbase, Inc
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17#include "tests/mcbp/mock_connection.h"
18
19#include <daemon/cookie.h>
20#include <daemon/front_end_thread.h>
21#include <folly/portability/GTest.h>
22#include <memcached/tracer.h>
23#include <unistd.h>
24#include <algorithm>
25#include <chrono>
26#include <iostream>
27#include <string>
28#include <thread>
29
30FrontEndThread thread;
31
32class TracingTest : public ::testing::Test {
33public:
34    void SetUp() override {
35        tracer.clear();
36    }
37    cb::tracing::Tracer tracer;
38};
39
40TEST_F(TracingTest, Basic) {
41    // empty tracer
42    EXPECT_EQ(tracer.getTotalMicros().count(), 0);
43
44    EXPECT_EQ(tracer.begin(cb::tracing::Code::Request), 0);
45    std::this_thread::sleep_for(std::chrono::microseconds(10000));
46
47    // invalid end check
48    EXPECT_FALSE(tracer.end(cb::tracing::SpanId{1}));
49
50    // valid end
51    EXPECT_TRUE(tracer.end(cb::tracing::SpanId{0}));
52
53    // valid micros
54    EXPECT_GE(tracer.getTotalMicros().count(), 10000);
55}
56
57TEST_F(TracingTest, ErrorRate) {
58    uint64_t micros_list[] = {5,
59                              11,
60                              1439,
61                              6234,
62                              7890,
63                              99999,
64                              4567321,
65                              98882110,
66                              78821369,
67                              118916406};
68    for (auto micros : micros_list) {
69        auto repMicros = tracer.encodeMicros(micros);
70        auto decoded = uint64_t(tracer.decodeMicros(repMicros).count());
71
72        if (decoded > micros) {
73            std::swap(micros, decoded);
74        }
75        // check if the error is less than 0.5%
76        EXPECT_LE((micros - decoded) * 100.0 / micros, 0.5);
77    }
78}
79
80/// Text fixture for session tracing associated with a Cookie object.
81class TracingCookieTest : public ::testing::Test {
82protected:
83    TracingCookieTest() : connection(thread), cookie(connection) {
84        cookie.setTracingEnabled(true);
85    }
86
87    void SetUp() override {
88        // Record initial time as an epoch (all subsequent times should be
89        // greater or equal to this).
90        epoch = std::chrono::steady_clock::now();
91    }
92
93    MockConnection connection;
94    Cookie cookie;
95    std::chrono::steady_clock::time_point epoch;
96};
97