/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/
#include "hbmeasuretest.h"
#include <QGridLayout>
#include <HbLabel>
#include <HbPushButton>
#include <QCoreApplication>
#include <QSignalMapper>
#include <QVariant>
#include <QVariantHash>
#include <HbComboBox>
#include <QBrush>
#include <QPalette>
#include <QGraphicsGridLayout>
#include <QTimer>
#include <QFile>
#include <QTextStream>
#include <e32std.h>
#include <HbInstance>
#include <qservicemanager.h>
#include <HbScrollArea>
#include <afactivitystorage.h>
#include <afactivation.h>
#include "afstorageglobals.h"
#include <HbCheckBox>
#include <hblineedit.h>
#include <f32file.h>
QTM_USE_NAMESPACE
const int KDefaultTimes = 10;
const int KDefaultLoop = 10;
const int KDefaultSaveBytes = 10*1024;
const int KGet1Bytes = 10*1024;
const int KGet2Bytes = 100*1024;
const int KGet3Bytes = 1024*1024;
const int KDefaultRepeat = 3;
MeasureTest::MeasureTest(QGraphicsItem *parent) : HbView(parent), mByteArray(NULL),
mActionCounter(0)
{
mFile = new QFile("c:\\data\\actautobenchmark.txt");
mFile->open(QIODevice::Append | QIODevice::Text);
mStream = new QTextStream(mFile);
QServiceManager serviceManager;
serviceManager.addService(":/activityserviceplugin.xml");
mActivityManager = serviceManager.loadInterface("com.nokia.qt.activities.ActivityManager");
if (!mActivityManager) {
qFatal("Cannot initialize critical com.nokia.qt.activities.ActivityManager service.");
}
mActivityStorage = new AfActivityStorage;
mActivation = new AfActivation;
mResultLabel = new HbLabel();
mItemLabel = new HbLabel();
mSaveOneButton = new HbPushButton("Save 1 in loop");
mSaveMoreButton = new HbPushButton("Save 10 in loop");
mGetButton = new HbPushButton("Get in loop");
mAllButton = new HbPushButton("All");
mLayout = new QGraphicsGridLayout();
mLayout->addItem(mResultLabel, 0, 0, 1, 6);
mLayout->addItem(mItemLabel, 1, 0, 1, 6);
mLayout->addItem(mSaveOneButton, 2, 0, 1, 6);
mLayout->addItem(mSaveMoreButton, 3, 0, 1, 6);
mLayout->addItem(mGetButton, 4, 0, 1, 6);
mLayout->addItem(mAllButton, 5, 0, 1, 6);
setLayout(mLayout);
connect(mSaveOneButton, SIGNAL(released()), this, SLOT(preClear()));
connect(mSaveMoreButton, SIGNAL(released()), this, SLOT(preClear()));
connect(mGetButton, SIGNAL(released()), this, SLOT(preClear()));
connect(mAllButton, SIGNAL(released()), this, SLOT(preClear()));
connect(mSaveOneButton, SIGNAL(released()), this, SLOT(saveOne()));
connect(mSaveMoreButton, SIGNAL(released()), this, SLOT(saveMore()));
connect(mGetButton, SIGNAL(released()), this, SLOT(getOne()));
connect(mAllButton, SIGNAL(released()), this, SLOT(all()));
connect(mSaveOneButton, SIGNAL(released()), this, SLOT(startTimer()));
connect(mSaveMoreButton, SIGNAL(released()), this, SLOT(startTimer()));
connect(mGetButton, SIGNAL(released()), this, SLOT(startTimer()));
connect(mAllButton, SIGNAL(released()), this, SLOT(startTimer()));
mTimer = new QTimer(this);
connect(mTimer, SIGNAL(timeout()), this, SLOT(timeout()));
}
MeasureTest::~MeasureTest()
{
delete mFile;
delete mStream;
delete mActivityManager;
delete mActivityStorage;
delete mActivation;
}
void MeasureTest::Before()
{
TTime time;
time.HomeTime();
iBefore = time.Int64();
}
void MeasureTest::After()
{
TTime time;
time.HomeTime();
iAfter = time.Int64();
}
void MeasureTest::Result()
{
TInt64 res(0);
res = iAfter-iBefore;
res = res/1000;
iResult = res;
}
int MeasureTest::average(const QList<int>& list)
{
int count = list.count();
int sum=0;
for(int i=0; i<count; i++) {
sum += list.at(i);
}
sum = (sum+0.5)/count;
return sum;
}
int MeasureTest::writeResults(const QList<int>& list, int average)
{
for(int i=0; i<list.count(); i++){
*mStream<<"| ";
*mStream<<list.at(i);
*mStream<<" |\n";
}
*mStream<<"| ";
*mStream<<average;
*mStream<<" |\n";
}
void MeasureTest::logError(const QString& mess)
{
*mStream<<"*Fail: "<<mess<<" *\n";
mStream->flush();
}
bool MeasureTest::saveBytes(int bytes)
{
delete mByteArray;
mByteArray = NULL;
mByteArray = new QByteArray(bytes, 'a');
QVariant variant;
variant = *mByteArray;
HbMainWindow *mainWindow = hbInstance->allMainWindows().first();
QPixmap screenshot = QPixmap::grabWidget(mainWindow, mainWindow->rect());
QVariantHash metadata;
metadata.insert(ActivityScreenshotKeyword, screenshot);
bool ok = false;
QString name("0");
ok = mActivityStorage->saveActivity(name, variant, metadata);
return ok;
}
bool MeasureTest::deleteActivities()
{
QStringList activities = mActivityStorage->allActivities();
QString name;
bool ok = true;
for (int i=0; i<activities.count(); i++) {
ok = mActivityStorage->removeActivity(activities.at(i));
if (!ok) {
break;
}
}
return ok;
}
bool MeasureTest::saveOneInLoop(int action)
{
int bytes = KDefaultSaveBytes;
delete mByteArray;
mByteArray = NULL;
mByteArray = new QByteArray(bytes, 'a');
QVariant variant;
variant = *mByteArray;
HbMainWindow *mainWindow = hbInstance->allMainWindows().first();
QPixmap screenshot = QPixmap::grabWidget(mainWindow, mainWindow->rect());
QVariantHash metadata;
metadata.insert(ActivityScreenshotKeyword, screenshot);
int times = KDefaultTimes;
bool ok = false;
QString name("0");
ok = deleteActivities();
if(!ok) {
logError("Remove");
return false;
}
User::After(2000000); //2s
for (int i=0; i<times; i++) {
Before();
ok = mActivityStorage->saveActivity(name, variant, metadata);
After();
Result();
if (!ok) {
break;
}
mResultsList[action].append(iResult);
ok = deleteActivities();
if(!ok) {
logError("Remove");
return false;
}
User::After(2000000); //2s
}
int averageint = average(mResultsList.at(action));
mResultsList[action].append(averageint);
if(!ok) {
logError("SaveOne");
}
return ok;
}
bool MeasureTest::saveMoreInLoop(int action)
{
int bytes = KDefaultSaveBytes;
delete mByteArray;
mByteArray = NULL;
mByteArray = new QByteArray(bytes, 'a');
QVariant variant;
variant = *mByteArray;
HbMainWindow *mainWindow = hbInstance->allMainWindows().first();
QPixmap screenshot = QPixmap::grabWidget(mainWindow, mainWindow->rect());
QVariantHash metadata;
metadata.insert(ActivityScreenshotKeyword, screenshot);
bool ok = deleteActivities();
if(!ok) {
logError("Remove");
return false;
}
int times = KDefaultTimes;
ok = false;
QString name;
User::After(2000000); //2s
for(int i=0; i<times; i++) {
Before();
for (int i=0; i<KDefaultLoop; i++) {
name.setNum(i);
ok = mActivityStorage->saveActivity(name, variant, metadata);
if (!ok) {
break;
}
}
After();
Result();
mResultsList[action].append(iResult);
QStringList activities = mActivityStorage->allActivities();
ok = deleteActivities();
if(!ok) {
logError("Remove");
return false;
}
User::After(2000000); //2s
}
int averageint = average(mResultsList.at(action));
averageint = (averageint+0.5)/KDefaultLoop;
mResultsList[action].append(averageint);
if(!ok) {
logError("SaveMore");
}
return ok;
}
bool MeasureTest::getOneInLoop(int bytes, int action)
{
int times = KDefaultTimes;
bool ok = deleteActivities();
if(!ok) {
logError("Remove");
return false;
}
ok = saveBytes(bytes);
if(!ok) {
logError("SaveBytes");
return false;
}
QStringList activities = mActivityStorage->allActivities();
User::After(2000000); //2s
QString name;
QVariant data;
for(int i=0; i<times; i++) {
Before();
for (int i=0; i<activities.count(); i++) {
data = mActivityStorage->activityData(activities.at(i));
}
After();
Result();
mResultsList[action].append(iResult);
User::After(2000000); //2s
}
int averageint = average(mResultsList.at(action));
mResultsList[action].append(averageint);
if(!ok) {
logError("GetOne");
}
return ok;
}
void MeasureTest::saveOne()
{
QList<int> l;
for(int i=0; i<KDefaultRepeat; i++) {
mActionsList.append("save1");
mResultsList.append(l);
}
}
void MeasureTest::saveMore()
{
QList<int> l;
for(int i=0; i<KDefaultRepeat; i++) {
mActionsList.append("save10");
mResultsList.append(l);
}
}
void MeasureTest::getOne()
{
QList<int> l;
for(int i=0; i<KDefaultRepeat; i++) {
mActionsList.append("get1");
mResultsList.append(l);
}
for(int i=0; i<KDefaultRepeat; i++) {
mActionsList.append("get2");
mResultsList.append(l);
}
for(int i=0; i<KDefaultRepeat; i++) {
mActionsList.append("get3");
mResultsList.append(l);
}
}
void MeasureTest::all()
{
saveOne();
saveMore();
getOne();
}
void MeasureTest::timeout()
{
if(mActionCounter == mActionsList.count()) {
mTimer->stop();
logResult();
enableControls(true);
return;
}
QString actionName = mActionsList.at(mActionCounter);
bool ok = false;
if(actionName == "save1") {
ok = saveOneInLoop(mActionCounter);
}
else if(actionName == "save10") {
ok = saveMoreInLoop(mActionCounter);
}
else if(actionName == "get1") {
ok = getOneInLoop(KGet1Bytes, mActionCounter);
}
else if(actionName == "get2") {
ok = getOneInLoop(KGet2Bytes, mActionCounter);
}
else if(actionName == "get3") {
ok = getOneInLoop(KGet3Bytes, mActionCounter);
}
else {
ok = false;
}
mItemMessage.clear();
mResultMessage.clear();
mItemMessage = (tr("action: %1")).arg(actionName);
mResultMessage = (tr("count: %1")).arg(mActionCounter).arg(ok);
mItemLabel->setPlainText(mItemMessage);
mResultLabel->setPlainText(mResultMessage);
mActionCounter++;
}
void MeasureTest::preClear()
{
mResultsList.clear();
mActionsList.clear();
mActionCounter = 0;
enableControls(false);
}
void MeasureTest::startTimer()
{
mTimer->start(1);
}
void MeasureTest::logResult()
{
QList<int> save1indexes;
QList<int> save10indexes;
QList<int> get1indexes;
QList<int> get2indexes;
QList<int> get3indexes;
for(int i=0; i<mActionsList.count(); i++) {
QString actionName = mActionsList.at(i);
if(actionName == "save1") {
save1indexes.append(i);
}
else if(actionName == "save10") {
save10indexes.append(i);
}
else if(actionName == "get1") {
get1indexes.append(i);
}
else if(actionName == "get2") {
get2indexes.append(i);
}
else if(actionName == "get3") {
get3indexes.append(i);
}
}
logSaveSummary(save1indexes, save10indexes);
logResult(save1indexes, 1, "Save one activity", "save");
logResult(save10indexes, 2, "Save 10 activities in loop", "save");
logGetSummary(get1indexes, get2indexes, get3indexes);
logResult(get1indexes, 1, "Get activities with 10 KB of data", "get");
logResult(get2indexes, 2, "Get activities with 100 KB of data", "get");
logResult(get3indexes, 3, "Get activities with 1 MB of data", "get");
}
void MeasureTest::logResult(const QList<int>& indexes, int caseno, const QString& mess, const QString& type)
{
int indexno = indexes.count();
if(indexno == 0) {
return;
}
QString caseness = tr("---+++++ Case %1.\n\n").arg(caseno);
*mStream<<caseness;
*mStream<<mess<<"\n\n";
*mStream<<"| |";
for(int i=0; i<indexno; i++) {
QString mess = tr(" *Test %1 [ms]* |").arg(i);
*mStream<<mess;
}
*mStream<<"\n";
QString tempmess;
for(int i=0; i<KDefaultTimes+1; i++) {
if(i == KDefaultTimes) {
tempmess = tr("| *Average per one %1* |").arg(type);
}
else {
tempmess = tr("| *Sample %1* |").arg(i);
}
*mStream<<tempmess;
for(int j=0; j<indexno; j++) {
const QList<int>& reslist = mResultsList.at(indexes.at(j));
if(reslist.count()>i) {
int res = reslist.at(i);
*mStream<<" "<<res<<" |";
}
else {
*mStream<<" - |";
}
}
*mStream<<"\n";
}
*mStream<<"\n\n";
mStream->flush();
}
QString MeasureTest::middle(const QList<int>& list)
{
if(list.count()==0) {
return "-";
}
QList<int> tlist = list;
qSort(tlist);
int count = tlist.count();
QString res;
res.setNum(tlist.at(count/2));
return res;
}
void MeasureTest::logSaveSummary(const QList<int>& indexes1, const QList<int>& indexes10)
{
*mStream<<"---++++ Midle time of save activity - the faster and the slowest result has been rejected.\n\n";
QList<int> save1averages;
QList<int> save10averages;
for(int i=0; i<indexes1.count(); i++) {
const QList<int>& reslist = mResultsList.at(indexes1.at(i));
if(reslist.count()==KDefaultTimes+1) {
int d = reslist.at(KDefaultTimes);
save1averages.append(reslist.at(KDefaultTimes));
}
}
for(int i=0; i<indexes10.count(); i++) {
const QList<int>& reslist = mResultsList.at(indexes10.at(i));
if(reslist.count()==KDefaultTimes+1) {
save10averages.append(reslist.at(KDefaultTimes));
}
}
QString save1middle = middle(save1averages);
QString save10middle = middle(save10averages);
*mStream<<"| *Case no.* | *no. of saved activity in one measure* | *Midle time [ms]* |\n";
*mStream<<"| 1 | 1 | "<<save1middle<<" |\n";
*mStream<<"| 2 | 10 | "<<save10middle<<" |\n\n";
mStream->flush();
}
void MeasureTest::logGetSummary(const QList<int>& get1, const QList<int>& get2 , const QList<int>& get3)
{
*mStream<<"---++++ Midle time of get activity - the faster and the slowest result has been rejected.\n\n";
QList<int> get1averages;
QList<int> get2averages;
QList<int> get3averages;
for(int i=0; i<get1.count(); i++) {
const QList<int>& reslist = mResultsList.at(get1.at(i));
if(reslist.count()==KDefaultTimes+1) {
get1averages.append(reslist.at(KDefaultTimes));
}
}
for(int i=0; i<get2.count(); i++) {
const QList<int>& reslist = mResultsList.at(get2.at(i));
if(reslist.count()==KDefaultTimes+1) {
get2averages.append(reslist.at(KDefaultTimes));
}
}
for(int i=0; i<get3.count(); i++) {
const QList<int>& reslist = mResultsList.at(get3.at(i));
if(reslist.count()==KDefaultTimes+1) {
get3averages.append(reslist.at(KDefaultTimes));
}
}
QString get1middle = middle(get1averages);
QString get2middle = middle(get2averages);
QString get3middle = middle(get3averages);
*mStream<<"| *Case no.* | *Data size [KB]* | *Midle time [ms]* |\n";
*mStream<<"| 1 | 10 | "<<get1middle<<" |\n";
*mStream<<"| 2 | 100 | "<<get2middle<<" |\n";
*mStream<<"| 3 | 1024 | "<<get3middle<<" |\n\n";
mStream->flush();
}
void MeasureTest::enableControls(bool enable)
{
mSaveOneButton->setEnabled(enable);
mSaveMoreButton->setEnabled(enable);
mGetButton->setEnabled(enable);
mAllButton->setEnabled(enable);
}