perftest.h 1.77 KB
Newer Older
1 2 3
#ifndef PERFTEST_H_
#define PERFTEST_H_

4
#define TEST_RAPIDJSON	1
5 6 7 8
#define TEST_JSONCPP	0
#define TEST_YAJL		0
#define TEST_ULTRAJSON  0
#define TEST_PLATFORM   0
9
#define TEST_MISC		0
10

11 12
#if TEST_RAPIDJSON
//#define RAPIDJSON_SSE2
13
#define RAPIDJSON_SSE42
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#endif

#if TEST_YAJL
#include "yajl/yajl_common.h"
#undef YAJL_MAX_DEPTH
#define YAJL_MAX_DEPTH 1024
#endif

////////////////////////////////////////////////////////////////////////////////
// Google Test

#ifdef __cplusplus

#include "gtest/gtest.h"

#ifdef _MSC_VER
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#pragma warning(disable : 4996) // 'function': was declared deprecated
#endif

//! Base class for all performance tests
class PerfTest : public ::testing::Test {
public:
	virtual void SetUp() {
39 40 41
		FILE *fp = fopen(filename_ = "data/sample.json", "rb");
		if (!fp) 
			fp = fopen(filename_ = "../../bin/data/sample.json", "rb");
42 43 44 45 46 47
		ASSERT_TRUE(fp != 0);

		fseek(fp, 0, SEEK_END);
		length_ = (size_t)ftell(fp);
		fseek(fp, 0, SEEK_SET);
		json_ = (char*)malloc(length_ + 1);
48
		ASSERT_EQ(length_, fread(json_, 1, length_, fp));
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
		json_[length_] = '\0';
		fclose(fp);

		// whitespace test
		whitespace_length_ = 1024 * 1024;
		whitespace_ = (char *)malloc(whitespace_length_  + 4);
		char *p = whitespace_;
		for (size_t i = 0; i < whitespace_length_; i += 4) {
			*p++ = ' ';
			*p++ = '\n';
			*p++ = '\r';
			*p++ = '\t';
		}
		*p++ = '[';
		*p++ = '0';
		*p++ = ']';
		*p++ = '\0';
	}

	virtual void TearDown() {
		free(json_);
		free(whitespace_);
	}

protected:
74
	const char* filename_;
75 76 77 78 79 80 81 82 83 84 85
	char *json_;
	size_t length_;
	char *whitespace_;
	size_t whitespace_length_;

	static const size_t kTrialCount = 1000;
};

#endif // __cplusplus

#endif // PERFTEST_H_