bsl_test_phashmap_str_dump.h 3.4 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 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 39 40 41 42 43 44 45 46 47 48 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
/***************************************************************************
 * 
 * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved
 * $Id: bsl_test_phashmap_str_dump.h,v 1.1 2008/11/26 09:37:19 xiaowei Exp $ 
 * 
 **************************************************************************/
 
 
 
/**
 * @file bsl_test_phashmap_dump.h
 * @author xiaowei(com@baidu.com)
 * @date 2008/11/17 11:19:15
 * @version $Revision: 1.1 $ 
 * @brief 
 *  
 **/


#ifndef  __BSL_TEST_PHASHMAP_DUMP_H_
#define  __BSL_TEST_PHASHMAP_DUMP_H_

#include <header_string.hpp>

const int DATANUM = 1<<20;
//const int DATANUM = 10;
const char *g_dumpfile = "bsl_test_phashmap_dump.dat";

template <typename HASH>
void * __query__(void *param)
{
	auto_timer t("query time at one thread");
	HASH *test = (HASH *)param;

	for (int i=0; i<DATANUM/10; ++i) {
		bsl::string str = randstr();
		bsl::string val;
		int ret = test->get(str, &val);
		if (ret == bsl::HASH_EXIST) {
			__XASSERT2_R(str == val);
		}
	}

	fprintf(stderr, "byebye query\n");
	return NULL;
}

std::set<bsl::string> g_set;

#include <pthread.h>
pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t g_cond = PTHREAD_COND_INITIALIZER;

static bool g_flagrun = true;
static bool g_cpend = false;

template <typename HASH>
void * __update__(void *param)
{
	auto_timer t("update time");
	HASH *test = (HASH *)param;

	for (int i=0; i<DATANUM; ++i) {
		bsl::string str = randstr();
		g_set.insert(str);
		test->set(str, str);
	}

	fprintf(stderr, "start to checkpoint\n");
	pthread_mutex_lock (&g_lock);
	if (test->make_checkpoint() != 0) {
		g_flagrun = false;
	}
	g_cpend = true;
	fprintf(stderr, "singal post %d\n", (int)g_flagrun);
	pthread_cond_signal(&g_cond);
	pthread_mutex_unlock(&g_lock);

	__XASSERT2_R(g_flagrun);

	fprintf(stderr, "inserrt again\n");
	
	for (int i=0; i<DATANUM; ++i) {
		bsl::string str = randstr();
		test->set(str, str);
	}

	fprintf(stderr, "byebye insert\n");

	return NULL;
}

template <typename HASH>
void *__dump__(void *param)
{
	auto_timer t("dump");
	HASH *test = (HASH *)param;

	fprintf(stderr, "start dump\n");
	pthread_mutex_lock(&g_lock);
	if (g_cpend) {
		pthread_mutex_unlock(&g_lock);
		return NULL;
	}
	pthread_cond_wait(&g_cond, &g_lock);
	pthread_mutex_unlock(&g_lock);
	fprintf(stderr, "sart to dump to file\n");

	__XASSERT2_R(g_flagrun);

	bsl::filestream fs;
	__XASSERT2_R(fs.open(g_dumpfile, "w") == 0);
	bsl::binarchive ar(fs);
	__XASSERT2_R(test->serialization(ar) == 0);
	fs.close();

	HASH *q = new HASH;
	__XASSERT2_R(q->create(1<<20) == 0);
	__XASSERT2_R(fs.open(g_dumpfile, "r") == 0);
	bsl::binarchive inar(fs);
	__XASSERT2_R(q->deserialization(ar) == 0);
	fs.close();

	for (typename HASH::iterator iter = q->begin();
			iter != q->end(); ++iter) {
		__XASSERT2_R(g_set.find(iter->first) != g_set.end());
	}

	fprintf(stderr, "byebye dump");
	return NULL;
}

template <typename HASH>
bool test_phashmap_dump()
{
	//保存文件明 bsl_test_phashmap_dump.dat
	//10个线程查询,1个线程更新,1个线程等待dump
	__XASSERT2(
			test_phashmap_temp<HASH>(
				10, __query__<HASH>, 
				1, __update__<HASH>, 
				1, __dump__<HASH>)
			);
	//检查load数据是否正确
	
	return true;
}

class bsl_test_phashmap_dump : public CxxTest::TestSuite
{
public:
	typedef bsl::phashmap<bsl::string, bsl::string, hash_key> type1_t;
	void test_phashmap_dump_ () {
		TSM_ASSERT("test_phashmap_dump", test_phashmap_dump<type1_t>());
	}
};


#endif  //__BSL_TEST_PHASHMAP_DUMP_H_

/* vim: set ts=4 sw=4 sts=4 tw=100 */