File: TestH5Table.java

package info (click to toggle)
jhdf 2.11.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 39,996 kB
  • ctags: 17,959
  • sloc: java: 100,416; ansic: 24,920; sh: 2,256; makefile: 957; cpp: 48
file content (137 lines) | stat: -rw-r--r-- 4,308 bytes parent folder | download
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
package test.object.misc;

import java.util.Vector;

import ncsa.hdf.hdf5lib.H5;
import ncsa.hdf.hdf5lib.HDF5Constants;
import ncsa.hdf.object.Dataset;
import ncsa.hdf.object.Datatype;
import ncsa.hdf.object.FileFormat;
import ncsa.hdf.object.h5.H5Datatype;
import ncsa.hdf.object.h5.H5File;

public class TestH5Table {
    
    public static void main(final String[] args) {
        long dim1=1000, dim2=50;
        
        if (args.length > 1) 
        {
            try {
                dim1 = Long.parseLong(args[0]);
                dim2 = Long.parseLong(args[1]);
            } catch (final Exception ex) {
                ex.printStackTrace();
                System.exit(0);
            }
        }
        
        try { 
            testTable(dim1, dim2); 
        } catch (final Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    
    /**
     * Test the performance of reading a small table data.
     */
    public static final void testTable(final long dim1, final long dim2) throws Exception 
    {
        int nObjs = 0; // number of object left open
        Dataset dset =null;
        long t0=0, t1=0, time=0, nbytes=0, readKBS=0, writeKBS=0;
        final String dname = "/table";
        
        final String[] COMPOUND_MEMBER_NAMES = {"int32", "float32"};
        final H5Datatype[] COMPOUND_MEMBER_DATATYPES = {
            new H5Datatype(Datatype.CLASS_INTEGER, 4, -1, -1), 
            new H5Datatype(Datatype.CLASS_FLOAT, 4, -1, -1)};
        
        final long DIM1 = dim1;
        final long DIM2 = dim2;
        final long[] DIMs = {DIM1, DIM2};
        final int DIM_SIZE = (int)(DIM1*DIM2);
        
        final int[] DATA_INT = new int[DIM_SIZE];
        final float[] DATA_FLOAT = new float[DIM_SIZE];
        final Vector DATA_COMP = new Vector(2);
        
        for (int i=0; i<DIM_SIZE; i++) {
            DATA_INT[i] = i;
            DATA_FLOAT[i] = i+i/100.0f;
        }
        DATA_COMP.add(0, DATA_INT);
        DATA_COMP.add(1, DATA_FLOAT);
        
        final H5File file = new H5File("testH5Table.h5", FileFormat.CREATE);
        file.open();
        
        try {
            dset = file.createCompoundDS(dname, null, DIMs, null, null, -1, 
                    COMPOUND_MEMBER_NAMES, COMPOUND_MEMBER_DATATYPES, null, DATA_COMP);
        } catch (final Exception ex) { 
            System.out.println("file.createCompoundDS() failed. "+ ex);
        }
        
        try {
            time = 0;
            nbytes = (DIM_SIZE*8L*1000000L);            
            dset = (Dataset)file.get(dname);
            dset.clearData();
            collectGarbage();
            
            // test reading
            t0 = System.nanoTime();
            try {
                dset.getData();
            } catch (final Exception ex) { 
                 System.out.println("dset.getData() failed. "+ ex);
            }
            t1 = System.nanoTime();
            time = (t1-t0);
            readKBS = (nbytes)/time;

            // test writing
            t0 = System.nanoTime();
            try {
                dset.write();
            } catch (final Exception ex) { 
                 System.out.println("dset.write() failed. "+ ex);
            }
            t1 = System.nanoTime();
            time = (t1-t0);
            writeKBS = (nbytes)/time;
            System.out.println("\nReading/writing a "+DIM1+"x"+DIM2+" table [KB/S]: \t"+ readKBS+"\t"+writeKBS);
            
            try {
                nObjs = H5.H5Fget_obj_count(file.getFID(), HDF5Constants.H5F_OBJ_ALL);
            } catch (final Exception ex) { 
                 System.out.println("H5.H5Fget_obj_count() failed. "+ ex);
            }
            
            try {            
                file.close();
            } catch (final Exception ex) { 
                System.out.println("file.close() failed. "+ ex);
            }
        } finally {
            // delete the testing file
            file.deleteOnExit();
        }
    }

    private static void collectGarbage() {
        try {
            System.gc();
            Thread.sleep(100);
            System.runFinalization();
            Thread.sleep(100);
        }
        catch (final Exception ex){
            ex.printStackTrace();
        }
    }
}