inital commit
[c11concurrency-benchmarks.git] / mabain / src / test / mbtest2.cpp
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <time.h>
4 #include <assert.h>
5 #include <atomic>
6 #include <fstream>
7 #include <cstring>
8
9 #include "../db.h"
10
11 #include "./test_key.h"
12
13 using namespace mabain;
14
15 static const char *mbdir = "/var/tmp/mabain_test/";
16 static pthread_t wid = 0;
17 static MBConfig mbconf;
18 static bool stop_processing = false;
19 static uint32_t run_time = 3600;
20
21 static void* run_mb_test(void *arg);
22
23 static void* TestThread(void *arg)
24 {
25     DB *dbw = (DB *) arg;
26     mbconf.options = CONSTS::ReaderOptions();
27     DB db(mbconf);
28     if(!db.is_open()) {
29         std::cerr << "failed tp open db\n";
30         abort();
31     }
32     assert(db.SetAsyncWriterPtr(dbw) == MBError::SUCCESS);
33     assert(db.AsyncWriterEnabled());
34         
35
36     int64_t ikey = 0;
37     int rval;
38     TestKey tkey_int(MABAIN_TEST_KEY_TYPE_INT);
39     TestKey tkey_sha1(MABAIN_TEST_KEY_TYPE_SHA_128);
40     TestKey tkey_sha2(MABAIN_TEST_KEY_TYPE_SHA_256);
41     std::string keystr;
42     MBData mbd;
43
44     while(!stop_processing) {
45         keystr = tkey_int.get_key(ikey);
46         rval = db.Find(keystr, mbd);
47         if(rval != MBError::SUCCESS) {
48             db.Add(keystr, keystr);
49         } 
50         keystr = tkey_sha1.get_key(ikey);
51         rval = db.Find(keystr, mbd);
52         if(rval != MBError::SUCCESS) {
53             db.Add(keystr, keystr);
54         } 
55         keystr = tkey_sha2.get_key(ikey);
56         rval = db.Find(keystr, mbd);
57         if(rval != MBError::SUCCESS) {
58             db.Add(keystr, keystr);
59         } 
60
61         ikey++;
62         usleep(1);
63     }    
64
65     assert(db.UnsetAsyncWriterPtr(dbw) == MBError::SUCCESS);
66     db.Close();
67     return NULL;
68 }
69
70 void stop_mb_test()
71 {
72     if(wid != 0) {
73         if(pthread_join(wid, NULL) != 0) {
74             std::cout << "cannot join mbtest thread\n";
75         }
76     }
77 }
78
79 void start_mb_test()
80 {
81     if(pthread_create(&wid, NULL, run_mb_test, NULL) != 0) {
82         std::cout << "failed to create test thread" << std::endl;
83     }
84 }
85
86 static void* run_mb_test(void *arg)
87 {
88     int64_t run_stop_time = time(NULL) + run_time;
89     int nreaders = 8;
90     srand(time(NULL));
91
92     DB::SetLogFile("/var/tmp/mabain_test/mabain.log");
93     memset(&mbconf, 0, sizeof(mbconf));
94     mbconf.mbdir = mbdir;
95     mbconf.block_size_index = 32*1024*1024;
96     mbconf.block_size_data = 32*1024*1024;
97     mbconf.memcap_index = 128LL*1024*1024;
98     mbconf.memcap_data = 128LL*1024*1024;
99
100     int options = CONSTS::WriterOptions() | CONSTS::ASYNC_WRITER_MODE;
101     mbconf.options = options;
102     DB *db = new DB(mbconf);
103     if(!db->is_open()) {
104         std::cerr << "failed to open writer db" << std::endl;
105         delete db;
106         abort();
107     }
108
109     pthread_t tid[256];
110     assert(nreaders < 256);
111     for(int i = 0; i < nreaders; i++) {
112         if(pthread_create(&tid[i], NULL, TestThread, db) != 0) {
113             std::cout << "failed to create reader thread" << std::endl;
114             abort();
115         }
116     }
117
118     int sleep_time;
119     while(!stop_processing) {
120         std::cout << "Running rc... " << db->Count() <<"\n";
121         db->CollectResource(8*1024*1024LL, 8*1024*1024LL, 1000000000000, 3000000);
122
123         sleep_time = (rand() % 10) + 1;
124         sleep(sleep_time);
125
126         if(time(NULL) >= run_stop_time) {
127             stop_processing = true;   
128         }
129     }
130
131     for(int i = 0; i < nreaders; i++) {
132         pthread_join(tid[i], NULL);
133     }
134     db->Close();
135     delete db;
136     DB::CloseLogFile();
137     return NULL;
138 }
139
140 static void SetTestStatus(bool success)
141 {
142     std::string cmd;
143     if(success) {
144         cmd = std::string("touch ") + mbdir + "/_success";
145     } else {
146         cmd = std::string("rm ") + mbdir + "/_success >" + mbdir + "/out 2>" + mbdir + "/err";
147     }
148     if(system(cmd.c_str()) != 0) {
149     }
150 }
151
152 int main(int argc, char *argv[])
153 {
154     if(argc > 1) {
155         mbdir = argv[1];
156         std::cout << "Test db directory is " << mbdir << "\n";
157     }
158     if(argc > 2) {
159         run_time = atoi(argv[2]);
160         std::cout << "running " << argv[0] << " for " << run_time << " seconds...\n";
161     }
162
163     DB::SetLogFile(std::string(mbdir) + "/mabain.log");
164     SetTestStatus(false);
165     run_mb_test(NULL);
166     DB::CloseLogFile();
167     SetTestStatus(true);
168     return 0;
169 }