summaryrefslogtreecommitdiff
path: root/org.genivi.commonapi.core.verification/src/PFPrimitive.cpp
blob: bc0c6a71663fd64bc3f3c876d07d49d4aadab81f (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
/* 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_0/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) {
		 // Stop the time & verify call status
		 watch_.stop();
		 EXPECT_EQ(callStatus, CommonAPI::CallStatus::SUCCESS);

		 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());
	}

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

    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 = meanTime / arraySize_;
		uint32_t callsPerSeconds = 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_);

		for (uint32_t i = 0; i < loopCountPerPaylod; ++i) {
			TestInterface::TestArray out;
			watch_.start();
			testProxy_->testMethodAsync(in, myCallback_);
			condVar_.wait(uniqueLock);
		}

		// 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();
}