Go Back   ZeroC Forums > Help Center

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
  #1 (permalink)  
Old 05-26-2006
kingbo kingbo is offline
Registered User
 
Name: Tang Fan
Organization: ShenZhen Yijin Platform Network Technology Co.,Ltd
Project: Stock quotation server
 
Join Date: Jul 2005
Posts: 15
Send a message via MSN to kingbo
-->
Why concurrent access to FreezeMap is so slow?

When I test the concurrent access to FreezeMap,I found it is much slower than single-thread method.My code like following:

Code:
ObjectAdapterPtr	g_pAdapter;
vector<string> g_GetNamesFromMap(long beginTime,long endTime)
{
	vector<string> back;
	Freeze::ConnectionPtr pConn = Freeze::createConnection(g_pAdapter->getCommunicator(), "db");
	IHisDataMap hisDataMap(pConn, "map");
	
	IHisDataMap::const_iterator pEnd = hisDataMap.upperBoundForTime(endTime);
	IHisDataMap::const_iterator pos;
	for(pos = hisDataMap.lowerBoundForTime(beginTime);pos!=pEnd;++pos)
	{
		back.push_back(pos->first);
	}
	return back;
}
vector<string> g_SetNamesToMap(string name,long time)
{
	Freeze::ConnectionPtr pConn = Freeze::createConnection(g_pAdapter->getCommunicator(), "db");
	IHisDataMap hisDataMap(pConn, "map");

	IHisDataMap::iterator pos = hisDataMap.find(name);	
	if(pos != hisDataMap.end())
	{
		pos.set(time);
	}else
	{
		hisDataMap.insert(make_pair(name, time));	
	}
	
}


void JobThread::run()
{
	m_bRunning = true;
	IceUtil::Time sleepTime = IceUtil::Time::milliSeconds(1000);
	while(true)
	{
		if(!m_bRunning)
		{
			break;
		}else
		{
			vector<string> names = g_GetNamesFromMap(0,ULONG_MAX);
		}
		ThreadControl::sleep(sleepTime);
	}
}

void main()
{
	//Init communicator and g_pAdapter
	.............................
	
	int iThreadNum = 30;
	for(int i=0;i<iThreadNum;++i)
	{
		JobThread pThread = new JobThread();
		pThread->start();
	}
}
1. When iThreadNum = 1, the g_GetNamesFromMap will use 600ms to finish per time. When iThreadNum = 30, it will takes 33s to finish. Is there anything wrong?
2. When some threads are invoking g_GetNamesFromMap, I will call g_SetNamesToMap to update the map. In this case, do I have to serialize the two function or just call g_SetNamesToMap directly?
__________________
Tang Fan
ShenZhen Yijin Platform Network Technology Co.,Ltd
Project: Stock quotation server
Reply With Quote
  #2 (permalink)  
Old 05-26-2006
bernard's Avatar
bernard bernard is online now
ZeroC Staff
 
Name: Bernard Normier
Organization: ZeroC, Inc.
Project: Ice
 
Join Date: Feb 2003
Location: Palm Beach Gardens, FL
Posts: 816
Quote:
Originally Posted by kingbo
1. When iThreadNum = 1, the g_GetNamesFromMap will use 600ms to finish per time. When iThreadNum = 30, it will takes 33s to finish. Is there anything wrong?
Your code is not very clear to me: each thread is an infinite loop that gets the names and sleeps for one full second? Which time are you measuring?

In g_GetNamesFromMap, you can avoid creating transactions by using a const map:
Code:
const IHisDataMap hisDataMap(pConn, "map");
This could improve performance a little.


Quote:
Originally Posted by kingbo
2. When some threads are invoking g_GetNamesFromMap, I will call g_SetNamesToMap to update the map. In this case, do I have to serialize the two function or just call g_SetNamesToMap directly?
You don't need to serialize, since you're using separate connections/maps in each function. However, you need to deal with deadlocks. See the Ice manual for details.

Cheers,
Bernard
__________________
Bernard Normier
ZeroC, Inc.
Reply With Quote
  #3 (permalink)  
Old 05-26-2006
kingbo kingbo is offline
Registered User
 
Name: Tang Fan
Organization: ShenZhen Yijin Platform Network Technology Co.,Ltd
Project: Stock quotation server
 
Join Date: Jul 2005
Posts: 15
Send a message via MSN to kingbo
-->
vow! So quick response in weekend. Thank you a lot!

Quote:
Your code is not very clear to me: each thread is an infinite loop that gets the names and sleeps for one full second? Which time are you measuring?
In fact,the code is:

Code:
void JobThread::run()
{
	m_bRunning = true;
	IceUtil::Time sleepTime = IceUtil::Time::milliSeconds(1000);
	while(true)
	{
		if(!m_bRunning)
		{
			break;
		}else
		{
			long beginPos = GetTickCount();
			vector<string> names = g_GetNamesFromMap(0,ULONG_MAX);
			long endPos = GetTickCount();
			printf("g_GetNamesFromMap takes %d\n",endPos-beginPos);
		}
		ThreadControl::sleep(sleepTime);
	}
}
I just tested your suggestion of "const IHisDataMap hisDataMap(pConn, "map");" and found the performence is improved a little but not satisfied. It still takes 20s to 30s per time.
__________________
Tang Fan
ShenZhen Yijin Platform Network Technology Co.,Ltd
Project: Stock quotation server
Reply With Quote
  #4 (permalink)  
Old 05-26-2006
bernard's Avatar
bernard bernard is online now
ZeroC Staff
 
Name: Bernard Normier
Organization: ZeroC, Inc.
Project: Ice
 
Join Date: Feb 2003
Location: Palm Beach Gardens, FL
Posts: 816
Another improvement would be to keep a Map opened in your main thread:

Code:
Freeze::ConnectionPtr pConn = Freeze::createConnection(g_pAdapter->getCommunicator(), "db");
	IHisDataMap hisDataMap(pConn, "map");
This way, you would avoid opening/closing the map all the time.

Cheers,
Bernard
__________________
Bernard Normier
ZeroC, Inc.
Reply With Quote
  #5 (permalink)  
Old 05-27-2006
kingbo kingbo is offline
Registered User
 
Name: Tang Fan
Organization: ShenZhen Yijin Platform Network Technology Co.,Ltd
Project: Stock quotation server
 
Join Date: Jul 2005
Posts: 15
Send a message via MSN to kingbo
-->
There is huge distance between 33s and 600ms. I guess I must miss some important configuration in DB_CONFIG. My DB_CONFIG is:
Quote:
set_cachesize 0 1073741824 1
set_lk_max_lockers 40000
set_lk_max_locks 40000
set_lk_max_objects 40000
set_tx_max 20000
set_lg_bsize 2097152
set_lg_max 2097152
set_flags DB_TXN_NOSYNC
set_flags DB_TXN_WRITE_NOSYNC
__________________
Tang Fan
ShenZhen Yijin Platform Network Technology Co.,Ltd
Project: Stock quotation server
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Slow callback sample burghboy Help Center 7 02-02-2007 09:43 AM
why Icegrid very slow? ChenQingQing Help Center 10 05-26-2006 06:04 AM
FreezeMap lazy initialize xdm Help Center 3 03-01-2006 11:07 PM
Integration of functional/concurrent processes languages? skropp Comments 1 01-26-2006 06:08 PM
FreezeMap Use Clarification Ken Carpenter Comments 11 03-01-2003 06:42 AM


All times are GMT -4. The time now is 10:38 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
(c) 2008 ZeroC, Inc.