1+ // Software Name : cppuprofile
2+ // SPDX-FileCopyrightText: Copyright (c) 2025 Orange
3+ // SPDX-License-Identifier: BSD-3-Clause
4+ //
5+ // This software is distributed under the BSD License;
6+ // see the LICENSE file for more details.
7+ //
8+ // Author: Cédric CHEDALEUX <cedric.chedaleux@orange.com> et al
9+
10+ #include < catch2/catch_test_macros.hpp>
11+ #include < unistd.h>
12+ #include < uprofile.h>
13+
14+ static const std::string filename = " ./test.log" ;
15+
16+ bool fileExists (const std::string& filename) {
17+ std::ifstream file (filename);
18+ return file.is_open ();
19+ }
20+
21+ int fileSize (const std::string& filename) {
22+ std::ifstream file (filename, std::ios::binary | std::ios::ate);
23+ if (!file.good ()) return 0 ;
24+ return file.tellg ();
25+ }
26+
27+ TEST_CASE (" Uprofile instant metrics" , " [instant]" )
28+ {
29+ uprofile::start (filename.c_str ());
30+
31+ SECTION (" Instant CPU Usage" )
32+ {
33+ std::vector<float > loads = uprofile::getInstantCpuUsage ();
34+ for (auto it = loads.cbegin (); it != loads.cend (); ++it) {
35+ REQUIRE (*it >= 0 .);
36+ REQUIRE (*it <= 100.0 );
37+ }
38+ }
39+
40+ SECTION (" No update in the file" )
41+ {
42+ std::ifstream file (filename, std::ios::binary | std::ios::ate);
43+ REQUIRE (file.good ());
44+ REQUIRE (file.tellg () == 0 );
45+ file.close ();
46+ sleep (1 );
47+ file.open (filename, std::ios::binary | std::ios::ate);
48+ REQUIRE (file.tellg () == 0 );
49+ }
50+
51+ uprofile::stop ();
52+ std::remove (filename.c_str ());
53+ }
54+
55+ TEST_CASE (" Uprofile monitoring metrics" , " [monitoring]" )
56+ {
57+ uprofile::start (filename.c_str ());
58+ uprofile::startCPUUsageMonitoring (200 );
59+ SECTION (" File existency" )
60+ {
61+ REQUIRE ( fileExists (filename) );
62+ }
63+
64+ SECTION (" File update" )
65+ {
66+ // Check that the file grows over time
67+ auto size = fileSize (filename);
68+ sleep (1 );
69+ REQUIRE (fileSize (filename) > size);
70+ }
71+ uprofile::stop ();
72+ std::remove (filename.c_str ());
73+ }
74+
75+ TEST_CASE (" Uprofile rotating files" , " [rotation]" )
76+ {
77+ // The two rotating files will have a maximum of 100 bytes in total
78+ int maxSize = 100 ; // bytes
79+ uprofile::start (filename.c_str (), maxSize);
80+
81+ // Enable all monitoring to stress the library
82+ uprofile::startCPUUsageMonitoring (50 );
83+ uprofile::startSystemMemoryMonitoring (50 );
84+ uprofile::startProcessMemoryMonitoring (50 );
85+
86+ size_t pos = filename.rfind (' .' );
87+ std::string file1 = filename;
88+ std::string file2 = filename;
89+ file1.insert (pos," _0" );
90+ file2.insert (pos," _1" );
91+
92+ SECTION (" Two rotating files" )
93+ {
94+ // Wait a couple of seconds to have several rotations
95+ sleep (2 );
96+ REQUIRE ( fileExists (file1) );
97+ REQUIRE ( fileExists (file2) );
98+
99+ // Check total size does not exceed the limit
100+ REQUIRE (fileSize (file1) + fileSize (file2) < maxSize);
101+ }
102+
103+ uprofile::stop ();
104+ std::remove (file1.c_str ());
105+ std::remove (file2.c_str ());
106+ }
0 commit comments