int Wcache() { Cache cache; int nRet = cache.Attach(g_sKeyFile, WRITER); if (nRet < 0) { printf("%s\n", cache.Strerr()); return -1; } printf("Attach success %d\n", nRet); nRet = cache.Register(); if (nRet < 0) { printf("%s\n", cache.Strerr()); return -1; } printf("Register OK %d\n", nRet); gettimeofday(&time_c, NULL); while (true) { if (_HandleSignal() < 0) break; gettimeofday(&time_a, NULL); memcpy(wbuff, &time_a, sizeof(time_a)); nRet = cache.Write(wbuff, g_len); gettimeofday(&time_b, NULL); if (nRet != g_len) { printf("write fialed %s\n", cache.Strerr()); return -1; } g_count0++; unsigned long long use_time0 = (time_b.tv_sec - time_a.tv_sec) * 1000000 + (time_b.tv_usec - time_a.tv_usec); total_time0 += use_time0; if(g_usleep > 0) usleep(g_usleep); } double time = (time_b.tv_sec-time_c.tv_sec)*1000000 + (time_b.tv_usec - time_c.tv_usec); double speed = (double)g_count0/time; printf("speed:%lf unit/s \n", speed*1000000); return 0; }
milliSec typicalUse( Cache &CC, unsigned objectKind, unsigned maxObjectCount, unsigned maxIteration) { assert(objectKind>0); assert(maxIteration>0); assert(maxObjectCount>0); vector< AbstractProduct* > fetched; fetched.reserve(maxObjectCount); srand(0); // initialise the pseudo random operator milliSec start(0); milliSec end(0); try{ // Registering objects for(size_t i=0;i<objectKind;i++) CC.Register(i, createProductNull); // Simulating real use start = getmilliSeconds(); for(unsigned i=0;i<maxIteration;i++) { const size_t size(fetched.size()); if( size == maxObjectCount ){ CC.ReleaseObject(fetched.back()); fetched.pop_back(); } else if(size == 0){ fetched.push_back(CC.CreateObject(int(objectKind*rand()/(RAND_MAX + 1.0)))); } else if(rand()<RAND_MAX/2){ CC.ReleaseObject(fetched.back()); fetched.pop_back(); } else { fetched.push_back(CC.CreateObject(int(objectKind*rand()/(RAND_MAX + 1.0)))); } } end = getmilliSeconds(); }catch(std::exception &e) { cout << "Error in executing typicalUse " << endl << e.what() << endl; } // Cleaning in use objects for(std::vector<AbstractProduct*>::iterator itr = fetched.begin(); itr!=fetched.end(); itr++) CC.ReleaseObject(*itr); fetched.clear(); return end-start; }
bool testEvictionError() { bool testPassed = false; Cache CC; CC.Register(nullID, createProductNull); CC.setMaxCreation(1); AbstractProduct *pProduct1 = NULL, *pProduct2 = NULL; try{ pProduct1 = CC.CreateObject(nullID); // should be OK pProduct2 = CC.CreateObject(nullID); // should cast an exception } catch(std::exception &e){ if(strcmp(e.what(), EvictionException().what())==0) testPassed = true; } if(pProduct1!=NULL) CC.ReleaseObject(pProduct1); if(pProduct2!=NULL) CC.ReleaseObject(pProduct2); return testPassed; }
int Rcache() { Cache cache; int nRet = cache.Attach(g_sKeyFile, READER); if(nRet < 0) { printf("%s\n", cache.Strerr() ); return -1; } printf("Attach success %d\n", nRet); if( cache.Register( 0 ) < 0) return -1; gettimeofday(&time_c, NULL); while (true) { if (_HandleSignal() < 0) break; gettimeofday(&time_a, NULL); nRet = cache.Read(rbuff, sizeof(rbuff), g_nSig); gettimeofday(&time_b, NULL); struct timeval *time_w = (struct timeval *)(rbuff); if(nRet == 0) { printf("writer exit\n"); continue; } if (nRet != g_len) { printf("read fialed nRet %d %s\n", nRet, cache.Strerr()); break; } g_count0++; if(g_verify == 1) { struct data* d = (struct data* )(rbuff+sizeof(struct timeval)); if( d->compare() < 0) { printf("format error %s\n");fflush(NULL); } } unsigned long long use_time0 = (time_b.tv_sec - time_a.tv_sec) * 1000000 + (time_b.tv_usec - time_a.tv_usec); total_time0 += use_time0; unsigned long long delay_time = (time_b.tv_sec - time_w->tv_sec) * 1000000 + (time_b.tv_usec - time_w->tv_usec); if(delay_time < 10000) { if(delay_time > g_delay_max) g_delay_max = delay_time; recv_delay_count0++; recv_delay0 += delay_time; } } return 0; }
int Cache_() { Cache cachew; Cache cacher; int nRet = cachew.Attach(g_sKeyFile, WRITER); if (nRet < 0) { printf("%s\n", cachew.Strerr()); return -1; } nRet = cacher.Attach(g_sKeyFile, READER); if (nRet < 0) { printf("%s\n", cacher.Strerr()); return -1; } printf("Attach success %d\n", nRet); nRet = cachew.Register(); if (nRet < 0) { printf("%s\n", cachew.Strerr()); return -1; } nRet = cacher.Register(); if (nRet < 0) { printf("%s\n", cacher.Strerr()); return -1; } printf("Register OK %d\n", nRet); while (true) { if (_HandleSignal() < 0) break; gettimeofday(&time_a, NULL); nRet = cachew.Write(wbuff, g_len); gettimeofday(&time_b, NULL); if (nRet != g_len) { printf("fialed %s\n", cachew.Strerr()); break; } if(g_usleep > 0) usleep(g_usleep); gettimeofday(&time_c, NULL); nRet = cacher.Read(rbuff, sizeof(rbuff), g_nSig); gettimeofday(&time_d, NULL); if (nRet != g_len) { printf("fialed %s\n", cacher.Strerr()); break; } g_count1++; g_count2++; unsigned long long use_time1 = (time_b.tv_sec - time_a.tv_sec) * 1000000 + (time_b.tv_usec - time_a.tv_usec); total_time1 += use_time1; unsigned long long use_time2 = (time_d.tv_sec - time_c.tv_sec) * 1000000 + (time_d.tv_usec - time_c.tv_usec); total_time2 += use_time2; } return 0; }