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 Extract sample lists from logs.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "xeTestLogParser.hpp"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "xeTestResultParser.hpp"
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deFilePath.hpp"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deString.h"
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deStringUtil.hpp"
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <vector>
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <string>
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <cstdio>
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <cstdlib>
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <fstream>
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <iostream>
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <stdexcept>
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::vector;
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::string;
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::set;
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::map;
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid writeSampleList (const char* casePath, int listNdx, const xe::ri::SampleList& sampleList)
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const string	filename	= string(casePath) + "." + de::toString(listNdx) + ".csv";
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::ofstream	out			(filename.c_str(), std::ios_base::binary);
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!out.good())
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw std::runtime_error("Failed to open " + filename);
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Header
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int ndx = 0; ndx < sampleList.sampleInfo.valueInfos.getNumItems(); ndx++)
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (ndx != 0)
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			out << ",";
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		out << static_cast<const xe::ri::ValueInfo&>(sampleList.sampleInfo.valueInfos.getItem(ndx)).name;
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	out << "\n";
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Samples
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int sampleNdx = 0; sampleNdx < sampleList.samples.getNumItems(); sampleNdx++)
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const xe::ri::Sample&	sample	= static_cast<const xe::ri::Sample&>(sampleList.samples.getItem(sampleNdx));
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int valNdx = 0; valNdx < sample.values.getNumItems(); valNdx++)
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const xe::ri::SampleValue&	value	= static_cast<const xe::ri::SampleValue&>(sample.values.getItem(valNdx));
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (valNdx != 0)
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				out << ",";
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			out << value.value;
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		out << "\n";
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid extractSampleLists (const char* casePath, int* listNdx, const xe::ri::List& items)
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int itemNdx = 0; itemNdx < items.getNumItems(); itemNdx++)
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const xe::ri::Item& child = items.getItem(itemNdx);
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (child.getType() == xe::ri::TYPE_SECTION)
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extractSampleLists(casePath, listNdx, static_cast<const xe::ri::Section&>(child).items);
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		else if (child.getType() == xe::ri::TYPE_SAMPLELIST)
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			writeSampleList(casePath, *listNdx, static_cast<const xe::ri::SampleList&>(child));
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			*listNdx += 1;
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid extractSampleLists (const xe::TestCaseResult& result)
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int listNdx = 0;
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	extractSampleLists(result.casePath.c_str(), &listNdx, result.resultItems);
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass SampleListParser : public xe::TestLogHandler
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	SampleListParser (void)
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void setSessionInfo (const xe::SessionInfo&)
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Ignored.
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	xe::TestCaseResultPtr startTestCaseResult (const char* casePath)
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return xe::TestCaseResultPtr(new xe::TestCaseResultData(casePath));
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void testCaseResultUpdated (const xe::TestCaseResultPtr&)
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Ignored.
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void testCaseResultComplete (const xe::TestCaseResultPtr& caseData)
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		xe::TestCaseResult result;
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		xe::parseTestCaseResultFromData(&m_testResultParser, &result, *caseData.get());
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		extractSampleLists(result);
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	xe::TestResultParser	m_testResultParser;
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void processLogFile (const char* filename)
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::ifstream		in				(filename, std::ifstream::binary|std::ifstream::in);
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	SampleListParser	resultHandler;
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	xe::TestLogParser	parser			(&resultHandler);
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deUint8				buf				[1024];
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int					numRead			= 0;
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!in.good())
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw std::runtime_error(string("Failed to open '") + filename + "'");
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (;;)
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		in.read((char*)&buf[0], DE_LENGTH_OF_ARRAY(buf));
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		numRead = (int)in.gcount();
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (numRead <= 0)
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			break;
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		parser.parse(&buf[0], numRead);
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	in.close();
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyryint main (int argc, const char* const* argv)
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (argc != 2)
1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		printf("%s: [filename]\n", de::FilePath(argv[0]).getBaseName().c_str());
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return -1;
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	try
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		processLogFile(argv[1]);
1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	catch (const std::exception& e)
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		printf("FATAL ERROR: %s\n", e.what());
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return -1;
1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return 0;
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
178