header.hpp 3.7 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 162 163 164 165 166 167 168 169 170 171
#ifndef _HEADER_HPP
#define _HEADER_HPP


#include <cxxtest/TestSuite.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <bsl/containers/hash/bsl_hashmap.h>
#include <bsl/containers/hash/bsl_phashmap.h>
#include <bsl/archive/bsl_filestream.h>
#include <bsl/archive/bsl_serialization.h>
#include <bsl/archive/bsl_binarchive.h>
#include <bsl/alloc/allocator.h>
#include <vector>
#include <map>
#include <sys/time.h>
#include <signal.h>
#include <ul_sign.h>

//////utils
#define __XASSERT(flag, fmt, arg...)  \
{\
	bool ___bsl_flag = flag; \
	if (!(___bsl_flag)) { \
		fprintf(stdout, "\n[error][%s:%d][%s]"fmt"\n", __FILE__, __LINE__, #flag, ##arg); \
		return false; \
	}\
}

#define __XASSERT2(flag) __XASSERT(flag, "")


static std::vector<pthread_t> g_pid;
static bool g_pthread_flag = true;
#define __XASSERT_R(flag, fmt, arg...) \
{\
	bool ___bsl_flag = flag; \
	if (!(___bsl_flag)) { \
		fprintf(stdout, "\n[error][%s:%d][%s]"fmt"\n", __FILE__, __LINE__, #flag, ##arg); \
		g_pthread_flag = false; \
		for (int i=0; i<(int)g_pid.size(); ++i) { \
			if (g_pid[i] != pthread_self()) { \
				int ret = pthread_cancel(g_pid[i]); \
				fprintf(stdout, "\n[error]kill pthread_t[%ld][%d]ret[%d][%m]\n", (long)g_pid[i], i, ret); \
			} \
		} \
		return NULL; \
	}\
}

#define __XASSERT2_R(flag) __XASSERT_R(flag, "")

class auto_timer
{
	char buf[64];
	timeval s, e;
	public:
	auto_timer(const char *str = NULL) {
		buf[0] = 0;
		if (str) snprintf(buf, sizeof(buf), "%s", str);
		gettimeofday(&s, NULL);
	}
	auto_timer(const char *str, int val) {
		snprintf(buf, sizeof(buf), "%s %d ", str, val);
		gettimeofday(&s, NULL);
	}
	~auto_timer() {
		gettimeofday(&e, NULL);
		long t = (long)((e.tv_sec - s.tv_sec) * 1000000 + (e.tv_usec - s.tv_usec));
		if (t/1000 > 0) {
			__BSL_DEBUG("%s %ld", buf, t);
		}
	}
};

class dis_timer
{
	timeval s, e;
public:
	void start() {
		gettimeofday(&s, NULL);
	}
	void end() {
		gettimeofday(&e, NULL);
	}
	int costms () {
		return (int)((e.tv_sec-s.tv_sec)*1000 + (e.tv_usec-s.tv_usec)/1000);
	}
};



std::string randstr(int wan=32)
{
	char buf[130] = {0};
	int len = rand()%wan + 1;
	for (int i=0; i<len; ++i) {
		buf[i] = rand()%26 + 'a';
	}
	return buf;
};

std::string int2str(int val)
{
	char buf[20];
	snprintf(buf, sizeof(buf), "%d", val);
	return buf;
}

/**
 *key hashº
 */
struct hash_key
{
	size_t operator () (const std::string &__key) const {
#if 1
		size_t key = 0;
		for (size_t i=0; i<__key.size(); ++i) {
			key = (key<<8)+__key[i];
		}
#else
		unsigned long key;
		unsigned int *k = (unsigned int *) & key;
		creat_sign_f64((char *)__key.c_str(), __key.size(), k, k+1);
#endif
		return key;
	}
};

template <class HASH>
bool test_phashmap_temp(int pnum, void *(*pfun)(void *), int upnum, void *(*upfun)(void *),
		int lnum = 0, void *(*lfun)(void *) = 0) 
{
	HASH * test = new (std::nothrow) HASH;
	g_pthread_flag = true;
	__XASSERT2(test != NULL);
	__XASSERT2(test->create(1<<20) == 0);
	g_pid.clear();
	pthread_t pid;

	{
		//auto_timer t("phashmap costime");
		
		for (int i=0; i<pnum; ++i) {
			__XASSERT(pthread_create(&pid, NULL, pfun, test) == 0,
					"create %d thread error[%m]", i);
			g_pid.push_back(pid);
		}
		for (int i=0; i<upnum; ++i) {
			__XASSERT(pthread_create(&pid, NULL, upfun, test) == 0,
					"create %d thread for up erro[%m]", i);
			g_pid.push_back(pid);
		}
		for (int i=0; i<lnum; ++i) {
			__XASSERT(pthread_create(&pid, NULL, lfun, test) == 0, 
					"create %d thread for l error[%m]", i);
			g_pid.push_back(pid);
		}

		for (int i=0; i<(int)g_pid.size(); ++i) {
			pthread_join(g_pid[i], NULL);
			//fprintf(stdout, "---------------join %d %ld success\n", i, (long)g_pid[i]);
		}
	}

	delete test;
	return g_pthread_flag;
}

#endif