d3fc9b55d78b101ca8d7506e2b275bff9c4ac6c9
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / storage / store_test.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: store_test.C /main/7 1996/08/21 15:56:52 drk $ */
24
25 #include <sys/time.h>
26 #include <sys/times.h>
27 #include "utility/pm_random.h"
28 #include "storage/page_storage.h"
29
30 ////////////////////////////
31 // case: store pages exist
32 ////////////////////////////
33 void real_page_cache_test_1(pm_random& rand_gen, page_storage** st, unsigned int ct, unsigned int no_access)
34 {
35    unsigned int j, k;
36    page_storage::access_t l;
37
38    for ( unsigned int i=0; i< no_access; i++)
39    {
40
41       j = rand_gen.rand() % ct; // pick the store
42       k = rand_gen.rand() % (st[j] -> pages()) + 1; // pick the page
43
44 // pick the READ/WRITE option
45
46       l = (rand_gen.rand_01()>0.5) ? page_storage::READ : page_storage::WRITE;
47
48       cerr << "store=" << st[j] -> my_name() << " ";
49       cerr << "page=" << k << " ";
50       if (l==page_storage::READ) 
51          cerr << "read" ;
52       else
53          cerr << "write";
54       cerr << "\n";
55
56       (*st[j])(k, l);
57    }
58 }
59
60 page_storage** 
61 prepare_store(char* path, lru& open_file_policy, 
62               pm_random& rand_gen, unsigned int ct, unsigned int low, unsigned int high)
63 {
64    char name[256];
65
66    page_storage** x = new page_storagePtr[ct];
67    unixf_storage*  unix_file = 0;
68
69    for ( unsigned int i=0; i<ct; i++) {
70       int pages = rand_gen.rand() % (high-low) + low;
71
72 /*
73 if ( i == 0 )
74    pages = 2;
75 if ( i == 1 )
76    pages = 17;
77 */
78
79       snprintf(name, sizeof(name), "test.%d", i);
80
81       if ( exist_file(name) == true )
82          del_file(name);
83
84       unix_file = new unixf_storage(path, name, &open_file_policy);
85       x[i] = new page_storage(path, name, unix_file, 1024);
86       x[i] -> add_page_frames(pages);
87    }
88    return x;
89 }
90    
91 void quit_store(page_storage** st, unsigned int ct)
92 {
93    for ( unsigned int i=0; i<ct; i++) {
94      delete st[i];
95    }
96    delete [] st;
97 }
98
99 int page_cache_test_1(int argc, char** argv)
100 {
101    if ( argc != 7 ) {
102       cerr << "usage: page_cache_test1 db_path stores min_pages max_pages no_probes\n";
103       cerr << "where \n";
104       cerr << " db_path: a path where the test dbs will be created;\n";
105       cerr << " stores: number of stores;\n";
106       cerr << " min_pages: min number of pages in these stores;\n";
107       cerr << " max_pages: max number of pages in these stores;\n";
108       cerr << " no_probes: number of probes to check the page cache.\n";
109       return 1;
110    }
111
112 #ifdef __uxp__
113    int seed;
114    struct tms tp;
115    if ((seed = (int)times(&tp)) < 0)
116       seed = 19;
117 #else
118    struct timeval tp;
119    struct timezone tzp;
120
121    int seed = ( gettimeofday(&tp, &tzp) == 0 ) ? int(tp.tv_sec) : 19;
122 #endif
123    pm_random rand_gen;
124    rand_gen.seed(seed);
125
126    char* path = argv[2];
127
128    if ( check_and_create_dir(path) != true )
129      return -1;
130
131    lru open_file_policy(20, 1000, false);
132
133    unsigned int ct = atoi(argv[3]);
134    unsigned int low = atoi(argv[4]); if ( low == 0 ) low = 1;
135
136    unsigned int high = atoi(argv[5]);
137    unsigned int no_access= atoi(argv[6]);
138
139    page_storage** st =
140         prepare_store(path, open_file_policy, rand_gen, ct, low, high);
141
142    real_page_cache_test_1(rand_gen, st, ct, no_access);
143
144    quit_store(st, ct);
145
146    return 0;
147 }
148
149 int store_test(int argc, char** argv)
150 {
151    if ( strcmp(argv[1], "page_cache_test_1") == 0 )
152      return page_cache_test_1(argc, argv);
153    else
154      return 2;
155 }
156