1/*
2 * Copyright (c) 2020 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file expected in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include "performance_func_test.h"
16#include <fstream>
17namespace {
18    static const int TIME_TRANSFORMATION_US = 1000000; // 1000000:1000000 microseconds
19    static const int CYCLE_TIMES = 1000; // 1000:Cycle 1000 times
20    std::ofstream g_writeIntoFile;
21}
22
23using namespace OHOS;
24using namespace std;
25using namespace testing::ext;
26using namespace OHOS::Camera;
27float PerformanceFuncTest::calTime(struct timeval start, struct timeval end)
28{
29    float time_use = 0;
30    time_use = (end.tv_sec - start.tv_sec) * TIME_TRANSFORMATION_US + (end.tv_usec - start.tv_usec);
31    return time_use;
32}
33void PerformanceFuncTest::SetUpTestCase(void) {}
34void PerformanceFuncTest::TearDownTestCase(void) {}
35void PerformanceFuncTest::SetUp(void) {}
36void PerformanceFuncTest::TearDown(void)
37{
38    Test_->Close();
39}
40
41/**
42  * @tc.name: Check Open camera's time consuming.
43  * @tc.desc: Check Open camera's time consuming.
44  * @tc.size: MediumTest
45  * @tc.type: Function
46  */
47HWTEST_F(PerformanceFuncTest, Camera_Performance_0001, TestSize.Level3)
48{
49    std::cout << "==========[test log] Performance: Check Open camera's time consuming."<< std::endl;
50    struct timeval start;
51    struct timeval end;
52    float totle_time_use = 0;
53    g_writeIntoFile.open("TimeConsuming.txt", ios::app);
54    for (int i= 0; i < CYCLE_TIMES; i++) {
55        float time_use;
56        std::cout << "Running " << i << " time" << std::endl;
57        gettimeofday(&start, NULL);
58        Test_ = std::make_shared<OHOS::Camera::Test>();
59        Test_->Init();
60        Test_->Open();
61        gettimeofday(&end, NULL);
62        time_use = calTime(start, end);
63        totle_time_use = totle_time_use + time_use;
64        // Post-processing, turn off the camera
65        Test_->Close();
66    }
67    float avrg_time = totle_time_use / CYCLE_TIMES;
68    EXPECT_LT(avrg_time, 80000);
69    std::cout << "==========[test log] Performance: Open camera's average time consuming: ";
70    std::cout << avrg_time << "us." << std::endl;
71    g_writeIntoFile << "==========[test log] Performance: Open camera's average time consuming: ";
72    g_writeIntoFile << avrg_time << "us." << std::endl;
73}
74
75/**
76  * @tc.name: Check Start Streams's time consuming.
77  * @tc.desc: Check Start Streams's time consuming.
78  * @tc.size: MediumTest
79  * @tc.type: Function
80  */
81HWTEST_F(PerformanceFuncTest, Camera_Performance_0002, TestSize.Level3)
82{
83    std::cout << "==========[test log] Performance: Check Start Streams's time consuming." << std::endl;
84    struct timeval start;
85    struct timeval end;
86    float time_use;
87    float totle_time_use = 0;
88    g_writeIntoFile.open("TimeConsuming.txt", ios::app);
89    Test_ = std::make_shared<OHOS::Camera::Test>();
90    Test_->Init();
91    Test_->Open();
92    for (int i = 0; i < CYCLE_TIMES; i++) {
93        std::cout << "Running " << i << " time" << std::endl;
94        // Create and get streamOperator information
95        Test_->CreateStreamOperatorCallback();
96        Test_->rc = Test_->cameraDevice->GetStreamOperator(Test_->streamOperatorCallback, Test_->streamOperator);
97        EXPECT_EQ(false, Test_->rc != Camera::NO_ERROR || Test_->streamOperator == nullptr);
98        // Create data flow
99        Test_->streamInfo = std::make_shared<Camera::StreamInfo>();
100        Test_->streamInfo->streamId_ = 1001;
101        Test_->streamInfo->width_ = 1920;
102        Test_->streamInfo->height_ = 1080;
103        Test_->StreamInfoFormat();
104        Test_->streamInfo->dataspace_ = 10;
105        Test_->streamInfo->intent_ = Camera::PREVIEW;
106        Test_->streamInfo->tunneledMode_ = 5;
107        std::shared_ptr<OHOS::Camera::Test::StreamConsumer> preview_consumer =
108        std::make_shared<OHOS::Camera::Test::StreamConsumer>();
109#ifdef CAMERA_BUILT_ON_OHOS_LITE
110        Test_->streamInfo->bufferQueue_ = preview_consumer->CreateProducer([this](OHOS::SurfaceBuffer* buffer) {
111            Test_->SaveYUV("preview", buffer->GetVirAddr(), buffer->GetSize());
112        });
113#else
114        Test_->streamInfo->bufferQueue_ = preview_consumer->CreateProducer([this](void* addr, uint32_t size) {
115            Test_->SaveYUV("preview", addr, size);
116        });
117#endif
118        Test_->streamInfo->bufferQueue_->SetQueueSize(8);
119        Test_->consumerMap_[Camera::PREVIEW] = preview_consumer;
120        std::vector<std::shared_ptr<Camera::StreamInfo>>().swap(Test_->streamInfos);
121        Test_->streamInfos.push_back(Test_->streamInfo);
122        gettimeofday(&start, NULL);
123        Test_->rc = Test_->streamOperator->CreateStreams(Test_->streamInfos);
124        EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
125        // Flow distribution
126        Test_->rc = Test_->streamOperator->CommitStreams(Camera::NORMAL, Test_->ability);
127        gettimeofday(&end, NULL);
128        EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
129        time_use = calTime(start, end);
130        totle_time_use = totle_time_use + time_use;
131        // Release stream
132        Test_->rc = Test_->streamOperator->ReleaseStreams({1001});
133        EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
134    }
135    float avrg_time = totle_time_use / CYCLE_TIMES;
136    EXPECT_LT(avrg_time, 100000);
137    std::cout << "==========[test log] Performance: Start Streams's average time consuming: ";
138    std::cout << avrg_time << "us." << std::endl;
139    g_writeIntoFile << "==========[test log] Performance: Start Streams's average time consuming: ";
140    g_writeIntoFile << avrg_time << "us. " << std::endl;
141}
142
143/**
144  * @tc.name: Check Close camera's time consuming.
145  * @tc.desc: Check Close camera's time consuming.
146  * @tc.size: MediumTest
147  * @tc.type: Function
148  */
149HWTEST_F(PerformanceFuncTest, Camera_Performance_0003, TestSize.Level3)
150{
151    std::cout << "==========[test log] Performance: Check Close camera's time consuming."<< std::endl;
152    struct timeval start;
153    struct timeval end;
154    float totle_time_use = 0;
155    g_writeIntoFile.open("TimeConsuming.txt", ios::app);
156    Test_ = std::make_shared<OHOS::Camera::Test>();
157    Test_->Init();
158    for (int i= 0; i < CYCLE_TIMES; i++) {
159        float time_use;
160        std::cout << "Running " << i << " time" << std::endl;
161        Test_->Open();
162        gettimeofday(&start, NULL);
163        Test_->Close();
164        gettimeofday(&end, NULL);
165        time_use = calTime(start, end);
166        totle_time_use = totle_time_use + time_use;
167    }
168    float avrg_time = totle_time_use / CYCLE_TIMES;
169    EXPECT_LT(avrg_time, 100000);
170    std::cout << "==========[test log] Performance: Close camera's average time consuming: ";
171    std::cout << avrg_time << "us." << std::endl;
172    g_writeIntoFile << "==========[test log] Performance: Close camera's average time consuming: ";
173    g_writeIntoFile << avrg_time << "us." << std::endl;
174}
175
176/**
177  * @tc.name: Continuous startup streams and shutdown streams.
178  * @tc.desc: Continuous startup streams and shutdown streams.
179  * @tc.size: MediumTest
180  * @tc.type: Function
181  */
182HWTEST_F(PerformanceFuncTest, Camera_Performance_0004, TestSize.Level3)
183{
184    std::cout << "==========[test log] Performance: Continuous startup streams and shutdown streams." << std::endl;
185    Test_ = std::make_shared<OHOS::Camera::Test>();
186    Test_->Init();
187    Test_->Open();
188    for (int i = 0; i < 100; i++) { // 100:Cycle 100 times
189        std::cout << "Running " << i << " time" << std::endl;
190        // Start stream
191        Test_->intents = {Camera::PREVIEW};
192        Test_->StartStream(Test_->intents);
193        // Get preview
194        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
195        // Release stream
196        Test_->captureIds = {Test_->captureId_preview};
197        Test_->streamIds.push_back(Test_->streamId_preview);
198        Test_->StopStream(Test_->captureIds, Test_->streamIds);
199    }
200}
201
202/**
203  * @tc.name: Preview and Capture, then start one Preview
204  * @tc.desc: Preview and Capture, then start one Preview
205  * @tc.size: MediumTest
206  * @tc.type: Function
207  */
208HWTEST_F(PerformanceFuncTest, Camera_Performance_0005, TestSize.Level3)
209{
210    std::cout << "==========[test log] Performance: Preview and Capture then start one Preview" << std::endl;
211    Test_ = std::make_shared<OHOS::Camera::Test>();
212    Test_->Init();
213    Test_->Open();
214    for (int i = 0; i < 100; i++) { // 100:Cycle 100 times
215        std::cout << "Running " << i << " time" << std::endl;
216        // Configure two stream information
217        Test_->intents = {Camera::PREVIEW, Camera::STILL_CAPTURE};
218        Test_->StartStream(Test_->intents);
219        // Capture preview stream
220        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
221        // Capture camera stream, single capture
222        Test_->StartCapture(Test_->streamId_capture, Test_->captureId_capture, false, true);
223        // Post-processing
224        Test_->captureIds = {Test_->captureId_preview, Test_->captureId_capture};
225        Test_->streamIds.push_back(Test_->streamId_preview);
226        Test_->streamIds.push_back(Test_->streamId_capture);
227        Test_->StopStream(Test_->captureIds, Test_->streamIds);
228        // Configure preview stream
229        Test_->intents = {Camera::PREVIEW};
230        Test_->StartStream(Test_->intents);
231        // Capture preview stream
232        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
233        // Release preview stream
234        Test_->captureIds = {Test_->captureId_preview};
235        Test_->streamIds.push_back(Test_->streamId_preview);
236        Test_->StopStream(Test_->captureIds, Test_->streamIds);
237    }
238}
239
240/**
241  * @tc.name: Preview and Video, then start one Preview
242  * @tc.desc: Preview and Video, then start one Preview
243  * @tc.size: MediumTest
244  * @tc.type: Function
245  */
246HWTEST_F(PerformanceFuncTest, Camera_Performance_0006, TestSize.Level3)
247{
248    std::cout << "==========[test log] Performance: Preview and Video, then start one Preview" << std::endl;
249    Test_ = std::make_shared<OHOS::Camera::Test>();
250    Test_->Init();
251    Test_->Open();
252    for (int i = 0; i < 100; i++) { // 100:Cycle 100 times
253        std::cout << "Running " << i << " time" << std::endl;
254        // Configure two stream information
255        Test_->intents = {Camera::PREVIEW, Camera::VIDEO};
256        Test_->StartStream(Test_->intents);
257        // Capture preview stream
258        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
259        // Capture video stream
260        Test_->StartCapture(Test_->streamId_video, Test_->captureId_video, false, true);
261        // Post-processing
262        Test_->captureIds = {Test_->captureId_preview, Test_->captureId_video};
263        Test_->streamIds = {Test_->streamId_preview, Test_->streamId_video};
264        Test_->StopStream(Test_->captureIds, Test_->streamIds);
265        // Configure preview stream
266        Test_->intents = {Camera::PREVIEW};
267        Test_->StartStream(Test_->intents);
268        // Capture preview stream
269        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
270        // Release preview stream
271        Test_->captureIds = {Test_->captureId_preview};
272        Test_->streamIds = {Test_->streamId_preview};
273        Test_->StopStream(Test_->captureIds, Test_->streamIds);
274    }
275}
276
277/**
278  * @tc.name: Preview and Video, then Preview and Capture, then Preview and Video
279  * @tc.desc: Preview and Video, then Preview and Capture, then Preview and Video
280  * @tc.size: MediumTest
281  * @tc.type: Function
282  */
283HWTEST_F(PerformanceFuncTest, Camera_Performance_0007, TestSize.Level3)
284{
285    std::cout << "==========[test log] Performance: Preview and Video, then Preview and Capture,";
286    std::cout << "then Preview and Video" << std::endl;
287    Test_ = std::make_shared<OHOS::Camera::Test>();
288    Test_->Init();
289    Test_->Open();
290    for (int i = 0; i < 100; i++) { // 100:Cycle 100 times
291        std::cout << "Running " << i << " time" << std::endl;
292        // Configure two stream information
293        Test_->intents = {Camera::PREVIEW, Camera::VIDEO};
294        Test_->StartStream(Test_->intents);
295        // Capture preview stream
296        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
297        // Capture video stream
298        Test_->StartCapture(Test_->streamId_video, Test_->captureId_video, false, true);
299        // Post-processing
300        Test_->captureIds = {Test_->captureId_preview, Test_->captureId_video};
301        Test_->streamIds = {Test_->streamId_preview, Test_->streamId_video};
302        Test_->StopStream(Test_->captureIds, Test_->streamIds);
303        Test_->consumerMap_.clear();
304        // Configure two stream information
305        Test_->intents = {Camera::PREVIEW, Camera::STILL_CAPTURE};
306        Test_->StartStream(Test_->intents);
307        // Capture preview stream
308        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
309        // Capture camera stream, continuous capture
310        Test_->StartCapture(Test_->streamId_capture, Test_->captureId_capture, false, true);
311        // Post-processing
312        Test_->captureIds = {Test_->captureId_preview, Test_->captureId_capture};
313        Test_->streamIds = {Test_->streamId_preview, Test_->streamId_capture};
314        Test_->StopStream(Test_->captureIds, Test_->streamIds);
315        Test_->consumerMap_.clear();
316        // Configure two stream information
317        Test_->intents = {Camera::PREVIEW, Camera::VIDEO};
318        Test_->StartStream(Test_->intents);
319        // Capture preview stream
320        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
321        // Capture video stream
322        Test_->StartCapture(Test_->streamId_video, Test_->captureId_video, false, true);
323        // Post-processing
324        Test_->captureIds = {Test_->captureId_preview, Test_->captureId_video};
325        Test_->streamIds = {Test_->streamId_preview, Test_->streamId_video};
326        Test_->StopStream(Test_->captureIds, Test_->streamIds);
327    }
328}
329
330/**
331  * @tc.name: Preview and Capture, then Preview and Video, then Preview and Capture
332  * @tc.desc: Preview and Capture, then Preview and Video, then Preview and Capture
333  * @tc.size: MediumTest
334  * @tc.type: Function
335  */
336HWTEST_F(PerformanceFuncTest, Camera_Performance_0008, TestSize.Level3)
337{
338    std::cout << "==========[test log] Performance: Preview and Capture, then Preview and Video,";
339    std::cout << "then Preview and Capture" << std::endl;
340    Test_ = std::make_shared<OHOS::Camera::Test>();
341    Test_->Init();
342    Test_->Open();
343    for (int i = 0; i < 100; i++) { // 100:Cycle 100 times
344        std::cout << "Running " << i << " time" << std::endl;
345        // Configure two stream information
346        Test_->intents = {Camera::PREVIEW, Camera::STILL_CAPTURE};
347        Test_->StartStream(Test_->intents);
348        // Capture preview stream
349        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
350        // Capture camera stream, continuous capture
351        Test_->StartCapture(Test_->streamId_capture, Test_->captureId_capture, false, true);
352        // Post-processing
353        Test_->captureIds = {Test_->captureId_preview, Test_->captureId_capture};
354        Test_->streamIds = {Test_->streamId_preview, Test_->streamId_capture};
355        Test_->StopStream(Test_->captureIds, Test_->streamIds);
356        Test_->consumerMap_.clear();
357        // Configure two stream information
358        Test_->intents = {Camera::PREVIEW, Camera::VIDEO};
359        Test_->StartStream(Test_->intents);
360        // Capture preview stream
361        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
362        // Capture video stream
363        Test_->StartCapture(Test_->streamId_video, Test_->captureId_video, false, true);
364        // Post-processing
365        Test_->captureIds = {Test_->captureId_preview, Test_->captureId_video};
366        Test_->streamIds = {Test_->streamId_preview, Test_->streamId_video};
367        Test_->StopStream(Test_->captureIds, Test_->streamIds);
368        Test_->consumerMap_.clear();
369        // Configure two stream information
370        Test_->intents = {Camera::PREVIEW, Camera::STILL_CAPTURE};
371        Test_->StartStream(Test_->intents);
372        // Capture preview stream
373        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
374        // Capture camera stream, continuous capture
375        Test_->StartCapture(Test_->streamId_capture, Test_->captureId_capture, false, true);
376        // Post-processing
377        Test_->captureIds = {Test_->captureId_preview, Test_->captureId_capture};
378        Test_->streamIds = {Test_->streamId_preview, Test_->streamId_capture};
379        Test_->StopStream(Test_->captureIds, Test_->streamIds);
380        Test_->consumerMap_.clear();
381    }
382}