I'm writing a server program for testing/learning purposes as I want to start getting into bigger programs but first I'm coding base C++ libraries for repetitious stuff so I'm not reinventing the wheel all the time.
All this server does is accept client connection and write any data to it's own file. I noticed with 200 concurrent connections sending data as fast as they can, it really bogs down and is slow. I'm only getting about 500KB/minute written to disk out of all files. This is basically the structure of the program, I'm wondering how I could improve it:
1-A SessionHandler class is created, this basically houses most of the program stuff, and a global instance of this class is then declared
2- In main, 10 worker threads are started, I'll get to those later
3- The socket is initiated and listening.
4-A loop runs poll() and acts upon any data. This is non blocking. If a new connection is detected a client is created and sent inside the session handler class which has an array of all the active connections.
5- poll then is called again and loops through each client to see if it has data if it does it grabs one byte and stores in it's buffer.
- The loop goes back to 4 at this point.
Now for the worker threads:
Basically it's just a loop where an int in the session handler keeps track of which session to poll. A poll is done to see if it has any pending data, if it does, it processes it (in this case, just appends it to file)
I noticed that increasing the number of worker threads has very little performance difference.
In this there are a lot of mutexes to ensure its thread safe, but I think that may be what is slowing me down. Maybe I need to somehow reduce the amount of shared memory I'm using.
Ex: Each time a client is accessed (to get or put data) it is locked then unlocked. Each time a session is accessed it is locked and unlocked.
Now based on this info, can you think of a better way I should be handling this, or point me to proper resources?
This is rather huge so don't expect someone to proof it line per line, but here is a link to the program and headers: (lot of them you probably wont need but was easier to just include whole thing - servertest is the actual program folder)
http://www.iceteks.com/misc/servertest.zip
Also I'm working on changing poll to select() so not sure if that will do anything performance wise, but realized poll only works in Linux so trying to make it cross platform.
All this server does is accept client connection and write any data to it's own file. I noticed with 200 concurrent connections sending data as fast as they can, it really bogs down and is slow. I'm only getting about 500KB/minute written to disk out of all files. This is basically the structure of the program, I'm wondering how I could improve it:
1-A SessionHandler class is created, this basically houses most of the program stuff, and a global instance of this class is then declared
2- In main, 10 worker threads are started, I'll get to those later
3- The socket is initiated and listening.
4-A loop runs poll() and acts upon any data. This is non blocking. If a new connection is detected a client is created and sent inside the session handler class which has an array of all the active connections.
5- poll then is called again and loops through each client to see if it has data if it does it grabs one byte and stores in it's buffer.
- The loop goes back to 4 at this point.
Now for the worker threads:
Basically it's just a loop where an int in the session handler keeps track of which session to poll. A poll is done to see if it has any pending data, if it does, it processes it (in this case, just appends it to file)
I noticed that increasing the number of worker threads has very little performance difference.
In this there are a lot of mutexes to ensure its thread safe, but I think that may be what is slowing me down. Maybe I need to somehow reduce the amount of shared memory I'm using.
Ex: Each time a client is accessed (to get or put data) it is locked then unlocked. Each time a session is accessed it is locked and unlocked.
Now based on this info, can you think of a better way I should be handling this, or point me to proper resources?
This is rather huge so don't expect someone to proof it line per line, but here is a link to the program and headers: (lot of them you probably wont need but was easier to just include whole thing - servertest is the actual program folder)
http://www.iceteks.com/misc/servertest.zip
Also I'm working on changing poll to select() so not sure if that will do anything performance wise, but realized poll only works in Linux so trying to make it cross platform.
