File indexing completed on 2025-01-05 04:58:37

0001 /*
0002  *   Copyright (C) 2016 Christian Mollekopf <chrigi_1@fastmail.fm>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU General Public License as published by
0006  *   the Free Software Foundation; either version 2 of the License, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License
0015  *   along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
0018  */
0019 #include <QTest>
0020 #include <QTcpSocket>
0021 
0022 #include "../imapresource.h"
0023 #include "../imapserverproxy.h"
0024 
0025 #include "common/test.h"
0026 #include "common/domain/applicationdomaintype.h"
0027 #include "common/store.h"
0028 #include "common/resourcecontrol.h"
0029 #include "common/secretstore.h"
0030 #include "common/definitions.h"
0031 
0032 #include <tests/hawd/dataset.h>
0033 #include <tests/hawd/formatter.h>
0034 
0035 using namespace Sink;
0036 using namespace Sink::ApplicationDomain;
0037 
0038 /**
0039  * Test of complete system using the imap resource.
0040  *
0041  * This test requires the imap resource installed.
0042  */
0043 class ImapMailSyncBenchmark : public QObject
0044 {
0045     Q_OBJECT
0046 
0047     bool isBackendAvailable()
0048     {
0049         QTcpSocket socket;
0050         socket.connectToHost("localhost", 143);
0051         return socket.waitForConnected(200);
0052     }
0053 
0054     void resetTestEnvironment()
0055     {
0056         system("populatemailbox.sh");
0057     }
0058 
0059     Sink::ApplicationDomain::SinkResource createResource()
0060     {
0061         auto resource = ApplicationDomain::ImapResource::create("account1");
0062         resource.setProperty("server", "localhost");
0063         resource.setProperty("port", 143);
0064         resource.setProperty("username", "doe");
0065         Sink::SecretStore::instance().insert(resource.identifier(), "doe");
0066         return resource;
0067     }
0068 
0069     void removeResourceFromDisk(const QByteArray &identifier)
0070     {
0071         ::ImapResource::removeFromDisk(identifier);
0072     }
0073 
0074     QByteArray mResourceInstanceIdentifier;
0075     QByteArrayList mCapabilities;
0076     HAWD::State mHawdState;
0077 
0078 private slots:
0079 
0080     void initTestCase()
0081     {
0082         Test::initTest();
0083         QVERIFY(isBackendAvailable());
0084         resetTestEnvironment();
0085         auto resource = createResource();
0086         QVERIFY(!resource.identifier().isEmpty());
0087 
0088         VERIFYEXEC(Store::create(resource));
0089 
0090         mResourceInstanceIdentifier = resource.identifier();
0091         mCapabilities = resource.getProperty("capabilities").value<QByteArrayList>();
0092     }
0093 
0094     void cleanup()
0095     {
0096         VERIFYEXEC(ResourceControl::shutdown(mResourceInstanceIdentifier));
0097         removeResourceFromDisk(mResourceInstanceIdentifier);
0098     }
0099 
0100     void init()
0101     {
0102         VERIFYEXEC(ResourceControl::start(mResourceInstanceIdentifier));
0103     }
0104 
0105     void testSync()
0106     {
0107         Sink::Query query;
0108         query.resourceFilter(mResourceInstanceIdentifier);
0109 
0110         QTime time;
0111         time.start();
0112 
0113         // Ensure all local data is processed
0114         VERIFYEXEC(Store::synchronize(query));
0115         auto sync = time.elapsed();
0116         SinkLog() << "Sync took: " << Sink::Log::TraceTime(sync);
0117 
0118         VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));
0119         auto total = time.elapsed();
0120         SinkLog() << "Total took: " << Sink::Log::TraceTime(total);
0121 
0122         time.start();
0123 
0124         VERIFYEXEC(Store::synchronize(query));
0125         auto resync = time.elapsed();
0126         SinkLog() << "ReSync took: " << Sink::Log::TraceTime(resync);
0127 
0128         VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));
0129         auto resynctotal = time.elapsed();
0130         SinkLog() << "Total resync took: " << Sink::Log::TraceTime(resynctotal);
0131 
0132         Sink::Storage::DataStore storage(Sink::storageLocation(), mResourceInstanceIdentifier, Sink::Storage::DataStore::ReadOnly);
0133         auto transaction = storage.createTransaction(Sink::Storage::DataStore::ReadOnly);
0134         auto stat = transaction.stat(false);
0135         SinkLog() << "Total free pages: " << stat.freePages;
0136 
0137         HAWD::Dataset dataset("imap_mail_sync", mHawdState);
0138         HAWD::Dataset::Row row = dataset.row();
0139         row.setValue("sync", sync);
0140         row.setValue("total", total);
0141         row.setValue("resync", resync);
0142         row.setValue("resynctotal", resynctotal);
0143         row.setValue("freepages", QVariant::fromValue(stat.freePages));
0144         dataset.insertRow(row);
0145         HAWD::Formatter::print(dataset);
0146     }
0147 };
0148 
0149 QTEST_MAIN(ImapMailSyncBenchmark)
0150 
0151 #include "imapmailsyncbenchmark.moc"