File: VillaTester.java

package info (click to toggle)
qdbm 1.8.78-6.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,952 kB
  • ctags: 5,454
  • sloc: ansic: 31,454; cpp: 3,623; perl: 2,167; java: 2,079; ruby: 1,690; makefile: 1,318; sh: 396
file content (286 lines) | stat: -rw-r--r-- 9,327 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*************************************************************************************************
 * Test cases for Villa for Java
 *                                                      Copyright (C) 2000-2006 Mikio Hirabayashi
 * This file is part of QDBM, Quick Database Manager.
 * QDBM is free software; you can redistribute it and/or modify it under the terms of the GNU
 * Lesser General Public License as published by the Free Software Foundation; either version
 * 2.1 of the License or any later version.  QDBM is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
 * details.
 * You should have received a copy of the GNU Lesser General Public License along with QDBM; if
 * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA.
 *************************************************************************************************/


package qdbm;



/**
 * Test case for Villa.
 */
class VillaTester extends Thread {
  //----------------------------------------------------------------
  // constants
  //----------------------------------------------------------------
  private static final int LOOPNUM = 10000;
  private static final int THREADNUM = 10;
  private static final java.util.Random RND = new java.util.Random();
  //----------------------------------------------------------------
  // instance fields
  //----------------------------------------------------------------
  private Villa myvilla;
  private int myid;
  private boolean myprinter;
  private VillaException myve;
  //----------------------------------------------------------------
  // public or protected methods
  //----------------------------------------------------------------
  /**
   * Main routine.
   * @param args specifies a name of a database file with the first element.
   */
  public static void main(String[] args) throws Exception {
    if(args.length != 1){
      System.err.println("usege: java qdbm.VillaTester name");
      return;
    }
    if(!dowrite(args[0], false)) System.exit(1);
    if(!doread(args[0])) System.exit(1);
    if(!domulti(args[0], false)) System.exit(1);
    if(!dowrite(args[0], true)) System.exit(1);
    if(!doread(args[0])) System.exit(1);
    if(!domulti(args[0], true)) System.exit(1);
    System.out.println("all ok");
    System.out.println("");
    System.exit(0);
  }
  /**
   * Perform writing test.
   * @param name specifies a name of a database.
   * @param zc specifies whether to compress leaves in the database.
   * @return whether the test is success or not.
   */
  public static boolean dowrite(String name, boolean zc){
    System.out.println("<Writing Test>");
    System.out.println("  name=" + name);
    System.out.println("");
    Villa villa = null;
    boolean err = false;
    try {
      // open a database
      System.out.print("Creating ... ");
      int omode = Villa.OWRITER | Villa.OCREAT | Villa.OTRUNC;
      if(zc) omode |= Villa.OZCOMP;
      villa = new Villa(name, omode, Villa.CMPLEX);
      System.out.println("ok");
      // loop for storing
      System.out.println("Writing");
      for(int i = 1; i <= LOOPNUM; i++){
        byte[] buf = Util.numstr(i, 8, '0').getBytes();
        // store a record into the database
        villa.put(buf, buf, Villa.DOVER);
        // print progression
        if(i % (LOOPNUM / 250) == 0){
          System.out.print('.');
          System.out.flush();
          if(i == LOOPNUM || i % (LOOPNUM / 10) == 0){
            System.out.println(" (" + new String(buf) + ")");
            System.out.flush();
          }
        }
      }
      System.out.println("ok");
    } catch(VillaException e){
      err = true;
      // report an error
      System.err.println(e);
    } finally {
      try {
        // close the database
        System.out.print("Closing ... ");
        if(villa != null) villa.close();
        System.out.println("ok");
      } catch(VillaException e){}
    }
    System.out.println("");
    return err ? false : true;
  }
  /**
   * Perform reading test.
   * @param name specifies a name of a database.
   * @return whether the test is success or not.
   */
  public static boolean doread(String name){
    System.out.println("<Reading Test>");
    System.out.println("  name=" + name);
    System.out.println("");
    Villa villa = null;
    boolean err = false;
    try {
      // open a database
      System.out.print("Opening ... ");
      villa = new Villa(name);
      System.out.println("ok");
      System.out.println("Reading");
      // loop for retrieving
      for(int i = 1; i <= LOOPNUM; i++){
        byte[] buf = Util.numstr(i, 8, '0').getBytes();
        // retrieve a record from the database
        villa.get(buf);
        // print progression
        if(i % (LOOPNUM / 250) == 0){
          System.out.print('.');
          System.out.flush();
          if(i == LOOPNUM || i % (LOOPNUM / 10) == 0){
            System.out.println(" (" + new String(buf) + ")");
            System.out.flush();
          }
        }
      }
      System.out.println("ok");
    } catch(VillaException e){
      err = true;
      // report an error
      System.err.println(e);
    } finally {
      try {
        // close the database
        System.out.print("Closing ... ");
        if(villa != null) villa.close();
        System.out.println("ok");
      } catch(VillaException e){}
    }
    System.out.println("");
    return err ? false : true;
  }
  /**
   * Perform multi thread test.
   * @param name specifies a name of a database.
   * @param zc specifies whether to compress leaves in the database.
   * @return whether the test is success or not.
   */
  public static boolean domulti(String name, boolean zc){
    System.out.println("<Multi-thread Test>");
    System.out.println("  name=" + name);
    System.out.println("");
    Villa villa = null;
    boolean err = false;
    try {
      // open a database
      System.out.print("Opening ... ");
      int omode = Villa.OWRITER | Villa.OCREAT | Villa.OTRUNC;
      if(zc) omode |= Villa.OZCOMP;
      villa = new Villa(name, omode, Villa.CMPLEX);
      System.out.println("ok");
      // prepare threads
      VillaTester[] dts = new VillaTester[THREADNUM];
      // roop for each threads
      System.out.println("Writing");
      for(int i = 0; i < dts.length; i++){
        dts[i] = new VillaTester(villa, i, i == dts.length / 2);
        dts[i].start();
      }
      // join every threads
      for(int i = 0; i < dts.length; i++){
        try {
          dts[i].join();
          if(dts[i].myve != null){
            err = true;
            System.err.println(dts[i].myve);
          }
        } catch(InterruptedException e){
          i--;
        }
      }
      if(!err) System.out.println("ok");
      System.out.flush();
      // check every record
      System.out.print("Validation checking ... ");
      for(int i = 1; i <= LOOPNUM; i++){
        byte[] buf = Util.numstr(i, 8, '0').getBytes();
        byte[] val = villa.get(buf);
        if(val.length != 1 || villa.vsiz(buf) != 1){
          err = true;
          System.err.println("size error: " + val.length);
        }
        int vnum = villa.vnum(buf);
        if(vnum != THREADNUM){
          err = true;
          System.err.println("vnum error: " + vnum);
        }
      }
      if(!err) System.out.println("ok");
    } catch(VillaException e){
      err = true;
      System.err.println(e);
    } finally {
      try {
        // close the database
        System.out.print("Closing ... ");
        if(villa != null) villa.close();
        System.out.println("ok");
      } catch(VillaException e){}
    }
    System.out.println("");
    return err ? false : true;
  }
  /**
   * Constructor for multi thread test.
   */
  private VillaTester(Villa villa, int id, boolean printer){
    myvilla = villa;
    myid = id;
    myprinter = printer;
    myve = null;
  }
  /**
   * Method for multi thread test.
   */
  public void run(){
    // roop for storing
    for(int i = 1; i <= LOOPNUM; i++){
      // store a record into the database
      byte[] buf = Util.numstr(i, 8, '0').getBytes();
      boolean tran = false;
      try {
        if(RND.nextInt(LOOPNUM / 7 + 1) == 0){
          myvilla.tranbegin();
          tran = true;
        }
        if(RND.nextInt(LOOPNUM / 20) == 0) yield();
        myvilla.put(buf, (myid % 2 == 0 ? "=" : "*").getBytes(),
                    RND.nextInt(3) == 0 ? Villa.DDUPR : Villa.DDUP);
      } catch(VillaException e){
        try {
          if(tran) myvilla.tranabort();
        } catch(VillaException ee){}
        myve = e;
        return;
      }
      try {
        if(tran) myvilla.trancommit();
      } catch(VillaException e){
        myve = e;
        return;
      }
      // print progression
      if(myprinter && i % (LOOPNUM / 250) == 0){
        System.out.print('.');
        System.out.flush();
        if(i == LOOPNUM || i % (LOOPNUM / 10) == 0){
          System.out.println(" (" + new String(buf) + ")");
          System.out.flush();
          // garbage collection test
          System.gc();
        }
      }
    }
  }
}



/* END OF FILE */