13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Quality Program Test Executor
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ------------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Merge two test logs.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \todo [2013-11-08 pyry] Write variant that can operate with less memory.
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "xeTestLogParser.hpp"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "xeTestResultParser.hpp"
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "xeTestLogWriter.hpp"
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deString.h"
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <vector>
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <string>
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <cstdio>
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <cstdlib>
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <fstream>
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <iostream>
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <stdexcept>
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::vector;
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::string;
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::set;
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::map;
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyryenum Flags
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	FLAG_USE_LAST_INFO = (1<<0)
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct CommandLine
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	CommandLine (void)
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: flags(0)
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	vector<string>	srcFilenames;
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	string			dstFilename;
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deUint32		flags;
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass LogHandler : public xe::TestLogHandler
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	LogHandler (xe::BatchResult* batchResult, deUint32 flags)
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: m_batchResult	(batchResult)
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, m_flags		(flags)
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void setSessionInfo (const xe::SessionInfo& info)
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		xe::SessionInfo& combinedInfo = m_batchResult->getSessionInfo();
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (m_flags & FLAG_USE_LAST_INFO)
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (!info.targetName.empty())		combinedInfo.targetName			= info.targetName;
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (!info.releaseId.empty())		combinedInfo.releaseId			= info.releaseId;
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (!info.releaseName.empty())		combinedInfo.releaseName		= info.releaseName;
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (!info.candyTargetName.empty())	combinedInfo.candyTargetName	= info.candyTargetName;
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (!info.configName.empty())		combinedInfo.configName			= info.configName;
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (!info.resultName.empty())		combinedInfo.resultName			= info.resultName;
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (!info.timestamp.empty())		combinedInfo.timestamp			= info.timestamp;
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		else
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (combinedInfo.targetName.empty())		combinedInfo.targetName			= info.targetName;
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (combinedInfo.releaseId.empty())			combinedInfo.releaseId			= info.releaseId;
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (combinedInfo.releaseName.empty())		combinedInfo.releaseName		= info.releaseName;
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (combinedInfo.candyTargetName.empty())	combinedInfo.candyTargetName	= info.candyTargetName;
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (combinedInfo.configName.empty())		combinedInfo.configName			= info.configName;
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (combinedInfo.resultName.empty())		combinedInfo.resultName			= info.resultName;
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (combinedInfo.timestamp.empty())			combinedInfo.timestamp			= info.timestamp;
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	xe::TestCaseResultPtr startTestCaseResult (const char* casePath)
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (m_batchResult->hasTestCaseResult(casePath))
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			xe::TestCaseResultPtr existingResult = m_batchResult->getTestCaseResult(casePath);
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			existingResult->clear();
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return existingResult;
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		else
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return m_batchResult->createTestCaseResult(casePath);
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void testCaseResultUpdated (const xe::TestCaseResultPtr&)
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Ignored.
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void testCaseResultComplete (const xe::TestCaseResultPtr&)
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Ignored.
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	xe::BatchResult* const	m_batchResult;
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const deUint32			m_flags;
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void readLogFile (xe::BatchResult* dstResult, const char* filename, deUint32 flags)
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::ifstream		in				(filename, std::ifstream::binary|std::ifstream::in);
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	LogHandler			resultHandler	(dstResult, flags);
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	xe::TestLogParser	parser			(&resultHandler);
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deUint8				buf				[2048];
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int					numRead			= 0;
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!in.good())
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw std::runtime_error(string("Failed to open '") + filename + "'");
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (;;)
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		in.read((char*)&buf[0], DE_LENGTH_OF_ARRAY(buf));
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		numRead = (int)in.gcount();
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (numRead <= 0)
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			break;
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		parser.parse(&buf[0], numRead);
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	in.close();
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void mergeTestLogs (const CommandLine& cmdLine)
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	xe::BatchResult batchResult;
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (vector<string>::const_iterator filename = cmdLine.srcFilenames.begin(); filename != cmdLine.srcFilenames.end(); ++filename)
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		readLogFile(&batchResult, filename->c_str(), cmdLine.flags);
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!cmdLine.dstFilename.empty())
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		xe::writeBatchResultToFile(batchResult, cmdLine.dstFilename.c_str());
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		xe::writeTestLog(batchResult, std::cout);
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void printHelp (const char* binName)
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	printf("%s: [filename] [[filename 2] ...]\n", binName);
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	printf("  --dst=[filename]    Write final log to file, otherwise written to stdout.\n");
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	printf("  --info=[first|last] Select which session info to use (default: first).\n");
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic bool parseCommandLine (CommandLine& cmdLine, int argc, const char* const* argv)
1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int argNdx = 1; argNdx < argc; argNdx++)
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const char* arg = argv[argNdx];
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (!deStringBeginsWith(arg, "--"))
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			cmdLine.srcFilenames.push_back(arg);
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		else if (deStringBeginsWith(arg, "--dst="))
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (!cmdLine.dstFilename.empty())
1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				return false;
1803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			cmdLine.dstFilename = arg+6;
1813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		else if (deStringEqual(arg, "--info=first"))
1833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			cmdLine.flags &= ~FLAG_USE_LAST_INFO;
1843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		else if (deStringEqual(arg, "--info=last"))
1853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			cmdLine.flags |= FLAG_USE_LAST_INFO;
1863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		else
1873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return false;
1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (cmdLine.srcFilenames.empty())
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return false;
1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return true;
1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1963c827367444ee418f129b2c238299f49d3264554Jarkko Poyryint main (int argc, const char* const* argv)
1973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	try
1993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		CommandLine cmdLine;
2013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (!parseCommandLine(cmdLine, argc, argv))
2033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			printHelp(argv[0]);
2053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return -1;
2063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		mergeTestLogs(cmdLine);
2093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	catch (const std::exception& e)
2113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		printf("FATAL ERROR: %s\n", e.what());
2133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return -1;
2143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return 0;
2173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
218