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:
PerfLogData()21 PerfLogData() {
22 FILELog::setReportingLevel( logERROR );
23
24 generateDataFiles();
25 }
26
generateDataFiles()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
TEST_F(PerfLogData,simpleLoad)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 ASSERT_THAT( log_data.getNbLine(), VBL_NB_LINES );
59 ASSERT_THAT( log_data.getMaxLength(), VBL_VISIBLE_LINE_LENGTH );
60 ASSERT_THAT( log_data.getLineLength( 123 ), VBL_VISIBLE_LINE_LENGTH );
61 ASSERT_THAT( log_data.getFileSize(), VBL_NB_LINES * (VBL_LINE_LENGTH+1LL) );
62
63 // Blocks of 5 MiB + 1 for the start notification (0%)
64 ASSERT_THAT( progressSpy.count(), log_data.getFileSize() / (5LL*1024*1024) + 2 );
65 ASSERT_THAT( endSpy.count(), 1 );
66 QList<QVariant> arguments = endSpy.takeFirst();
67 ASSERT_THAT( arguments.at(0).toInt(),
68 static_cast<int>( LoadingStatus::Successful ) );
69 }
70
71 class PerfLogDataRead : public PerfLogData {
72 public:
PerfLogDataRead()73 PerfLogDataRead() : PerfLogData(), log_data(), endSpy(
74 &log_data, SIGNAL( loadingFinished( LoadingStatus ) ) ) {
75 log_data.attachFile( TMPDIR "/verybiglog.txt" );
76 endSpy.wait( 20000 );
77 }
78
79 LogData log_data;
80 SafeQSignalSpy endSpy;
81 };
82
TEST_F(PerfLogDataRead,sequentialRead)83 TEST_F( PerfLogDataRead, sequentialRead ) {
84 ASSERT_THAT( log_data.getNbLine(), VBL_NB_LINES );
85 // Read all lines sequentially
86 QString s;
87 {
88 TestTimer t;
89
90 for (int i = 0; i < VBL_NB_LINES; i++) {
91 s = log_data.getLineString(i);
92 }
93 }
94 }
95
TEST_F(PerfLogDataRead,sequentialReadExpanded)96 TEST_F( PerfLogDataRead, sequentialReadExpanded ) {
97 ASSERT_THAT( log_data.getNbLine(), VBL_NB_LINES );
98 // Read all lines sequentially (expanded)
99 QString s;
100 {
101 TestTimer t;
102
103 for (int i = 0; i < VBL_NB_LINES; i++) {
104 s = log_data.getExpandedLineString(i);
105 }
106 }
107 }
108
TEST_F(PerfLogDataRead,randomPageRead)109 TEST_F( PerfLogDataRead, randomPageRead ) {
110 ASSERT_THAT( log_data.getNbLine(), VBL_NB_LINES );
111 // Read page by page from the beginning and the end, using the QStringList
112 // function
113 QStringList list;
114 {
115 TestTimer t;
116
117 for (int page = 0; page < (VBL_NB_LINES/VBL_LINE_PER_PAGE)-1; page++)
118 {
119 list = log_data.getLines( page*VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE );
120 ASSERT_THAT(list.count(), VBL_LINE_PER_PAGE);
121 int page_from_end = (VBL_NB_LINES/VBL_LINE_PER_PAGE) - page - 1;
122 list = log_data.getLines( page_from_end*VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE );
123 ASSERT_THAT(list.count(), VBL_LINE_PER_PAGE);
124 }
125 }
126 }
127
TEST_F(PerfLogDataRead,randomPageReadExpanded)128 TEST_F( PerfLogDataRead, randomPageReadExpanded ) {
129 ASSERT_THAT( log_data.getNbLine(), VBL_NB_LINES );
130 // Read page by page from the beginning and the end, using the QStringList
131 // function
132 QStringList list;
133 {
134 TestTimer t;
135
136 for (int page = 0; page < (VBL_NB_LINES/VBL_LINE_PER_PAGE)-1; page++)
137 {
138 list = log_data.getExpandedLines( page*VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE );
139 ASSERT_THAT(list.count(), VBL_LINE_PER_PAGE);
140 int page_from_end = (VBL_NB_LINES/VBL_LINE_PER_PAGE) - page - 1;
141 list = log_data.getExpandedLines( page_from_end*VBL_LINE_PER_PAGE, VBL_LINE_PER_PAGE );
142 ASSERT_THAT(list.count(), VBL_LINE_PER_PAGE);
143 }
144 }
145 }
146