xref: /glogg/tests/logdataPerfTest.cpp (revision 1ee847ca425151b00d956eaea3ee654b7e55afc9)
1 #include <QTest>
2 #include <QSignalSpy>
3 
4 #include "log.h"
5 #include "test_utils.h"
6 
7 #include "data/logdata.h"
8 
9 #include "gmock/gmock.h"
10 
11 #define TMPDIR "/tmp"
12 
13 static const qint64 VBL_NB_LINES = 4999999LL;
14 static const int VBL_LINE_PER_PAGE = 70;
15 static const char* vbl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line\t\t%07d\n";
16 static const int VBL_LINE_LENGTH = (76+2+7) ; // Without the final '\n' !
17 static const int VBL_VISIBLE_LINE_LENGTH = (76+8+4+7); // Without the final '\n' !
18 
19 class PerfLogData : public testing::Test {
20   public:
21     PerfLogData() {
22         FILELog::setReportingLevel( logERROR );
23 
24         generateDataFiles();
25     }
26 
27     bool generateDataFiles() {
28         char newLine[90];
29 
30         QFile file( TMPDIR "/verybiglog.txt" );
31         if ( file.open( QIODevice::WriteOnly ) ) {
32             for (int i = 0; i < VBL_NB_LINES; i++) {
33                 snprintf(newLine, 89, vbl_format, i);
34                 file.write( newLine, qstrlen(newLine) );
35             }
36         }
37         else {
38             return false;
39         }
40         file.close();
41 
42         return true;
43     }
44 };
45 
46 TEST_F( PerfLogData, simpleLoad ) {
47     LogData log_data;
48     QSignalSpy progressSpy( &log_data, SIGNAL( loadingProgressed( int ) ) );
49     QSignalSpy endSpy( &log_data, SIGNAL( loadingFinished( LoadingStatus ) ) );
50 
51     {
52         TestTimer t;
53 
54         log_data.attachFile( TMPDIR "/verybiglog.txt" );
55         ASSERT_TRUE( endSpy.wait( 20000 ) );
56     }
57 
58     // Blocks of 5 MiB + 1 for the start notification (0%)
59     ASSERT_THAT( progressSpy.count(), log_data.getFileSize() / (5LL*1024*1024) + 2 );
60     ASSERT_THAT( endSpy.count(), 1 );
61     QList<QVariant> arguments = endSpy.takeFirst();
62     ASSERT_THAT( arguments.at(0).toInt(),
63             static_cast<int>( LoadingStatus::Successful ) );
64 
65     ASSERT_THAT( log_data.getNbLine(), VBL_NB_LINES );
66     ASSERT_THAT( log_data.getMaxLength(), VBL_VISIBLE_LINE_LENGTH );
67     ASSERT_THAT( log_data.getLineLength( 123 ), VBL_VISIBLE_LINE_LENGTH );
68     ASSERT_THAT( log_data.getFileSize(), VBL_NB_LINES * (VBL_LINE_LENGTH+1LL) );
69 }
70 
71 class PerfLogDataRead : public PerfLogData {
72   public:
73     PerfLogDataRead() : PerfLogData(), endSpy(
74             &log_data, SIGNAL( loadingFinished( LoadingStatus ) ) ) {
75         log_data.attachFile( TMPDIR "/verybiglog.txt" );
76         endSpy.safeWait( 10000 );
77     }
78 
79     LogData log_data;
80     SafeQSignalSpy endSpy;
81 };
82 
83 TEST_F( PerfLogDataRead, sequentialRead ) {
84     // Read all lines sequentially
85     QString s;
86     {
87         TestTimer t;
88 
89         for (int i = 0; i < VBL_NB_LINES; i++) {
90             s = log_data.getLineString(i);
91         }
92     }
93 }
94 
95 TEST_F( PerfLogDataRead, sequentialReadExpanded ) {
96     // Read all lines sequentially (expanded)
97     QString s;
98     {
99         TestTimer t;
100 
101         for (int i = 0; i < VBL_NB_LINES; i++) {
102             s = log_data.getExpandedLineString(i);
103         }
104     }
105 }
106 
107 TEST_F( PerfLogDataRead, randomPageRead ) {
108     // Read page by page from the beginning and the end, using the QStringList
109     // function
110     QStringList list;
111     {
112         TestTimer t;
113 
114         for (int page = 0; page < (VBL_NB_LINES/VBL_LINE_PER_PAGE)-1; page++)
115         {
116             list = log_data.getLines( page*VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE );
117             EXPECT_THAT(list.count(), VBL_LINE_PER_PAGE);
118             int page_from_end = (VBL_NB_LINES/VBL_LINE_PER_PAGE) - page - 1;
119             list = log_data.getLines( page_from_end*VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE );
120             EXPECT_THAT(list.count(), VBL_LINE_PER_PAGE);
121         }
122     }
123 }
124 
125 TEST_F( PerfLogDataRead, randomPageReadExpanded ) {
126     // Read page by page from the beginning and the end, using the QStringList
127     // function
128     QStringList list;
129     {
130         TestTimer t;
131 
132         for (int page = 0; page < (VBL_NB_LINES/VBL_LINE_PER_PAGE)-1; page++)
133         {
134             list = log_data.getExpandedLines( page*VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE );
135             EXPECT_THAT(list.count(), VBL_LINE_PER_PAGE);
136             int page_from_end = (VBL_NB_LINES/VBL_LINE_PER_PAGE) - page - 1;
137             list = log_data.getExpandedLines( page_from_end*VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE );
138             EXPECT_THAT(list.count(), VBL_LINE_PER_PAGE);
139         }
140     }
141 }
142