1
|
package eu.dnetlib.data.mdstore.modular.mongodb;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.io.FileInputStream;
|
5
|
import java.io.IOException;
|
6
|
import java.util.Iterator;
|
7
|
import java.util.UUID;
|
8
|
|
9
|
import com.mongodb.DBObject;
|
10
|
import com.mongodb.client.MongoDatabase;
|
11
|
import eu.dnetlib.data.mdstore.MDStoreServiceException;
|
12
|
import eu.dnetlib.data.mdstore.modular.RecordParserFactory;
|
13
|
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao;
|
14
|
import org.apache.commons.io.IOUtils;
|
15
|
import org.junit.Before;
|
16
|
import org.junit.Ignore;
|
17
|
import org.junit.Test;
|
18
|
import org.junit.runner.RunWith;
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
import org.springframework.core.io.ClassPathResource;
|
21
|
import org.springframework.test.context.ContextConfiguration;
|
22
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
23
|
|
24
|
@Ignore
|
25
|
@RunWith(SpringJUnit4ClassRunner.class)
|
26
|
@ContextConfiguration(classes = ConfigurationTestConfig.class)
|
27
|
public class FeedSpeedTest {
|
28
|
|
29
|
private static final int N_RECORDS = 68271;
|
30
|
|
31
|
@Autowired
|
32
|
private MongoDatabase db;
|
33
|
|
34
|
@Autowired
|
35
|
private MDStoreDao dao;
|
36
|
|
37
|
@Autowired
|
38
|
private RecordParserFactory recordParserfactory;
|
39
|
|
40
|
@Before
|
41
|
public void setup() throws MDStoreServiceException {
|
42
|
dao.createMDStore("speed_test", "testFormat", "testInterpretation", "testLayout");
|
43
|
}
|
44
|
|
45
|
|
46
|
@Test
|
47
|
public void testSpeedFromFolder() throws IOException {
|
48
|
Iterable<String> iterable = new Iterable<String>() {
|
49
|
|
50
|
private int counter = 0;
|
51
|
private double last = System.currentTimeMillis();
|
52
|
|
53
|
@Override
|
54
|
public Iterator<String> iterator() {
|
55
|
return new Iterator<String>() {
|
56
|
|
57
|
@Override
|
58
|
public boolean hasNext() {
|
59
|
return counter < N_RECORDS;
|
60
|
}
|
61
|
|
62
|
@Override
|
63
|
public String next() {
|
64
|
if (counter % 10000 == 0) {
|
65
|
System.out.println("10K records processed in " + (System.currentTimeMillis() - last) / 1000 + " seconds");
|
66
|
last = System.currentTimeMillis();
|
67
|
}
|
68
|
|
69
|
File f = new File(String.format("/var/lib/eagle/content/EDH/HD%06d.xml", counter++));
|
70
|
if (f.exists()) {
|
71
|
try {
|
72
|
FileInputStream fileInputStream = new FileInputStream(f);
|
73
|
String s = IOUtils.toString(fileInputStream);
|
74
|
fileInputStream.close();
|
75
|
return s;
|
76
|
} catch (Exception e) {
|
77
|
return null;
|
78
|
}
|
79
|
} else {
|
80
|
try {
|
81
|
FileInputStream fileInputStream = new FileInputStream(new File("/var/lib/eagle/content/EDH/HD000001.xml"));
|
82
|
String s = IOUtils.toString(fileInputStream);
|
83
|
fileInputStream.close();
|
84
|
return s;
|
85
|
} catch (Exception e) {
|
86
|
return null;
|
87
|
}
|
88
|
}
|
89
|
}
|
90
|
|
91
|
@Override
|
92
|
public void remove() {}
|
93
|
};
|
94
|
}
|
95
|
};
|
96
|
|
97
|
MongoMDStore mdStore =
|
98
|
new MongoMDStore(UUID.randomUUID().toString(), db.getCollection("speed_test", DBObject.class), recordParserfactory.newInstance(), true, db);
|
99
|
mdStore.feed(iterable, false);
|
100
|
}
|
101
|
|
102
|
//@Ignore
|
103
|
@Test
|
104
|
public void testFeedSpeedFromTemplate() throws MDStoreServiceException, IOException {
|
105
|
MongoMDStore mdStore =
|
106
|
new MongoMDStore(UUID.randomUUID().toString(), db.getCollection("speed_test", DBObject.class), recordParserfactory.newInstance(), false, db);
|
107
|
mdStore.feed(new Iterable<String>() {
|
108
|
|
109
|
private int counter = 0;
|
110
|
private double last = System.currentTimeMillis();
|
111
|
private String templateRecord = IOUtils.toString(new ClassPathResource("/eu/dnetlib/data/mdstore/modular/mongodb/templateRecord.xml")
|
112
|
.getInputStream());
|
113
|
|
114
|
@Override
|
115
|
public Iterator<String> iterator() {
|
116
|
return new Iterator<String>() {
|
117
|
|
118
|
@Override
|
119
|
public boolean hasNext() {
|
120
|
return counter < N_RECORDS;
|
121
|
}
|
122
|
|
123
|
@Override
|
124
|
public String next() {
|
125
|
if (counter % 10000 == 0) {
|
126
|
System.out.println("10K records processed in " + (System.currentTimeMillis() - last) / 1000 + " seconds");
|
127
|
last = System.currentTimeMillis();
|
128
|
}
|
129
|
|
130
|
File f = new File(String.format("/var/lib/eagle/content/EDH/HD%06d.xml", counter++));
|
131
|
if (f.exists()) {
|
132
|
try {
|
133
|
FileInputStream fileInputStream = new FileInputStream(f);
|
134
|
String s = IOUtils.toString(fileInputStream);
|
135
|
fileInputStream.close();
|
136
|
return s;
|
137
|
} catch (Exception e) {
|
138
|
return null;
|
139
|
}
|
140
|
} else {
|
141
|
counter++;
|
142
|
try {
|
143
|
FileInputStream fileInputStream = new FileInputStream(new File("/var/lib/eagle/content/EDH/HD000009.xml"));
|
144
|
String s = IOUtils.toString(fileInputStream);
|
145
|
fileInputStream.close();
|
146
|
return s;
|
147
|
} catch (Exception e) {
|
148
|
return null;
|
149
|
}
|
150
|
}
|
151
|
}
|
152
|
|
153
|
@Override
|
154
|
public void remove() {}
|
155
|
};
|
156
|
}
|
157
|
}, false);
|
158
|
}
|
159
|
}
|