00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef AMINO_UTIL_THREADRUNNER_H
00017 #define AMINO_UTIL_THREADRUNNER_H
00018
00019 #include <sys/time.h>
00020 #include <stdio.h>
00021 #include <stdarg.h>
00022 #include <amino/thread.h>
00023 #include "testconfig.h"
00024 #include "threadFactory.h"
00025
00026 namespace test {
00027 class Logger {
00028 private:
00029 FILE *target;
00030 public:
00031 Logger(FILE *file) :
00032 target(file) {
00033 }
00034
00035 void log(const char* fmt, ...) {
00036 va_list ap;
00037 va_start(ap,fmt);
00038 vfprintf(target, fmt, ap);
00039 va_end(ap);
00040 }
00041 };
00042
00043 class ConcurrentRunner {
00044 public:
00045 virtual void
00046 runThreads(Runnable**runnable, int len, const char *testName) = 0;
00047 virtual void setClassName(const char* name) = 0;
00048 virtual ~ConcurrentRunner(){};
00049 };
00050
00051 class ThreadRunner: public ConcurrentRunner {
00052 public:
00053 ThreadRunner(int tn = 8, int on = 100, const char* c = NULL, FILE *logp =
00054 stderr) :
00055 nthread(tn), nOperation(on), tclass(c), log(logp) {
00056 }
00057
00058 template<typename ParaType> static void runThreads(
00059 ThreadFactory<ParaType> * tf, const char * className,
00060 const char * testName, FILE * logp = stderr);
00061
00062 void setClassName(const char* name) {
00063 tclass = name;
00064 }
00065 private:
00066 int nthread;
00067 int nOperation;
00068 const char *tclass;
00069 Logger log;
00070
00071 void runThreads(Runnable** runnable, int len, const char* testName);
00072 };
00073
00074 template<typename ParaType> void ThreadRunner::runThreads(ThreadFactory<
00075 ParaType> * tf, const char * className, const char * testName,
00076 FILE * logp) {
00077 const TestConfig * tc = TestConfig::getInstance();
00078 int elementNum = tc->getElementNum();
00079 int operationNum = tc->getOperationNum();
00080 vector<int> threadNum = tc->getThreadNum();
00081
00082 for (vector<int>::iterator i = threadNum.begin(); i < threadNum.end(); i++) {
00083 int curThr = *i;
00084 Runnable ** threads = tf->createThreads(curThr, elementNum, operationNum);
00085 ThreadRunner * runner =
00086 new ThreadRunner(curThr, operationNum, className, logp);
00087 runner->runThreads(threads, curThr, testName);
00088 tf -> collection(threads, curThr);
00089 tf -> deleteThreads(threads, curThr);
00090 tf -> verifyResult(curThr, elementNum);
00091 delete runner;
00092 }
00093 }
00094
00095 }
00096 #endif