summaryrefslogtreecommitdiff
path: root/org.genivi.commonapi.core.verification/src/PFPrimitive.cpp
blob: b2f6849849f8b1c1bc940ae0352e9b55d6e8f1c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* Copyright (C) 2014 BMW Group
 * Author: Juergen Gehring (juergen.gehring@bmw.de)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
* @file Performance_Primitive
*/

#include <gtest/gtest.h>
#include "CommonAPI/CommonAPI.hpp"

#include "v1/commonapi/performance/primitive/TestInterfaceProxy.hpp"
#include "stub/PFPrimitiveStub.h"

#include "utils/StopWatch.h"

const std::string serviceId = "service-sample";
const std::string clientId = "client-sample";

const std::string domain = "local";
const std::string testAddress = "commonapi.performance.primitive.TestInterface";
const int tasync = 100000;

const int usecPerSecond = 1000000;

// Define the max. array size to test
const int maxPrimitiveArraySize = 1024*16;
// Define the loop count how often the commonAPI test function is called for calculating the mean time
const int loopCountPerPaylod = 1000;

using namespace v1_0::commonapi::performance::primitive;

class Environment: public ::testing::Environment {
public:
    virtual ~Environment() {
    }

    virtual void SetUp() {
    }

    virtual void TearDown() {
    }
};

class PFPrimitive: public ::testing::Test {
public:
     void recvArray(const CommonAPI::CallStatus& callStatus, TestInterface::TestArray y) {
         (void)y;
         EXPECT_EQ(callStatus, CommonAPI::CallStatus::SUCCESS);
         callCount_++;
         if (callCount_ == loopCountPerPaylod) {
             callCount_ = 0;
             std::unique_lock<std::mutex> uniqueLock(synchLock_);
             condVar_.notify_one();
         }
     }

protected:
    void SetUp() {
        runtime_ = CommonAPI::Runtime::get();
        ASSERT_TRUE((bool)runtime_);

        testStub_ = std::make_shared<PFPrimitiveStub>();
        bool serviceRegistered = runtime_->registerService(domain, testAddress, testStub_, serviceId);
        ASSERT_TRUE(serviceRegistered);

        testProxy_ = runtime_->buildProxy<TestInterfaceProxy>(domain, testAddress, clientId);
        ASSERT_TRUE((bool)testProxy_);

        testProxy_->isAvailableBlocking();
        ASSERT_TRUE(testProxy_->isAvailable());

        callCount_ = 0;
    }

    void TearDown() {
        bool unregistered = runtime_->unregisterService(domain, PFPrimitiveStub::StubInterface::getInterface(), testAddress);
        ASSERT_TRUE(unregistered);

        // wait that proxy is not available
        int counter = 0;  // counter for avoiding endless loop
        while ( testProxy_->isAvailable() && counter < 10 ) {
            usleep(100000);
            counter++;
        }

        ASSERT_FALSE(testProxy_->isAvailable());
    }

    void printTestValues() {
        // Get elapsed time, calculate mean time and print out!
        StopWatch::usec_t methodCallTime = watch_.getTotalElapsedMicroseconds();
        StopWatch::usec_t meanTime = (methodCallTime / loopCountPerPaylod);
        StopWatch::usec_t perByteTime = StopWatch::usec_t(meanTime / arraySize_);
        uint32_t callsPerSeconds = uint32_t(usecPerSecond / (methodCallTime / loopCountPerPaylod));

        std::cout << "[MEASURING ]  Size=" << std::setw(7) << std::setfill('.') << arraySize_
                  << ", Mean-Time=" << std::setw(7) << std::setfill('.') << meanTime << "us"
                  << ", per-Byte=" << std::setw(7) << std::setfill('.')
                  << (perByteTime <= 0 ? ".....<1" : std::to_string(perByteTime)) << "us"
                  << ", calls/s=" << std::setw(7) << std::setfill('.') << callsPerSeconds
                  << std::endl;
    }

    std::string configFileName_;
    std::shared_ptr<CommonAPI::Runtime> runtime_;
    std::shared_ptr<TestInterfaceProxy<>> testProxy_;
    std::shared_ptr<TestInterfaceStub> testStub_;

    StopWatch watch_;
    std::mutex synchLock_;
    std::condition_variable condVar_;
    uint32_t callCount_;
    std::function<void (const CommonAPI::CallStatus&, TestInterface::TestArray)> myCallback_;
    uint32_t arraySize_ = 1;
};

/**
* @test Test synchronous ping pong function call
*   - primitive array is array of UInt_8
*     - The stub just set the in array to the out array
*     - CallStatus and array content will be used to verify the sync call has succeeded
*     - Using double payload every cycle, starting with 1 end with maxPrimitiveArraySize
*     - Doing primitiveLoopSize loops to build the mean time
*/
TEST_F(PFPrimitive, Ping_Pong_Primitive_Synchronous) {
    CommonAPI::CallStatus callStatus;

    watch_.reset();

    // Loop until maxPrimitiveArraySize
    while (arraySize_ <= maxPrimitiveArraySize) {

        // Create in-array with actual arraySize
        TestInterface::TestArray in(arraySize_);

        // Call commonAPI method loopCountPerPaylod times to calculate mean time
        for (uint32_t i = 0; i < loopCountPerPaylod; ++i) {

            // Create an empty out-array for every commonAPI function call
            TestInterface::TestArray out;

            // Call commonAPI function and measure time
            watch_.start();
            testProxy_->testMethod(in, callStatus, out);
            watch_.stop();

            // Check the call was successful & out array has same elements than in array
            EXPECT_EQ(callStatus, CommonAPI::CallStatus::SUCCESS);
            EXPECT_EQ(in, out);
        }

        // Printing results
        printTestValues();

        // Increase array size for next iteration
        arraySize_ *= 2;

        // Reset StopWatch for next iteration
        watch_.reset();
    }
}

/**
* @test Test asynchronous ping pong function call
*   - primitive array is array of UInt_8
*     - The stub just set (copies) the in array to the out array
*     - Only the CallStatus will be used to verify the async call has succeeded
*     - Using double payload every cycle, starting with 1 end with maxPrimitiveArraySize
*     - Doing primitiveLoopSize loops to build the mean time
*/
TEST_F(PFPrimitive, Ping_Pong_Primitive_Asynchronous) {
    myCallback_ = std::bind(&PFPrimitive::recvArray, this, std::placeholders::_1, std::placeholders::_2);

    std::unique_lock<std::mutex> uniqueLock(synchLock_);

    // Loop until maxPrimitiveArraySize
    while (arraySize_ <= maxPrimitiveArraySize) {

        watch_.reset();

        // Initialize testData, call count stop watch for next iteration!
        TestInterface::TestArray in(arraySize_);

#ifdef WIN32
        // DBus under Windows is way to slow at the moment (about 10 times slower than linux), so without an increase in timeout, this test never succeeds.
        // Only raising for WIN32, since linux should run with the default timeout without problems.
        CommonAPI::CallInfo callInfo(60000);
#endif

        watch_.start();
        for (uint32_t i = 0; i < loopCountPerPaylod; ++i) {
#ifdef WIN32
            testProxy_->testMethodAsync(in, myCallback_, &callInfo);
#else
            testProxy_->testMethodAsync(in, myCallback_);
#endif
        }
        condVar_.wait(uniqueLock);
        watch_.stop();

        // Printing results
        printTestValues();

        // Increase array size for next iteration
        arraySize_ *= 2;
    }
}

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    ::testing::AddGlobalTestEnvironment(new Environment());
    return RUN_ALL_TESTS();
}