1
|
package edu.ucsb.nceas.metacattest;
|
2
|
|
3
|
import java.io.FileReader;
|
4
|
import java.io.IOException;
|
5
|
import java.io.Reader;
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.List;
|
8
|
import java.util.concurrent.ExecutorService;
|
9
|
import java.util.concurrent.Executors;
|
10
|
import java.util.concurrent.TimeUnit;
|
11
|
|
12
|
import edu.ucsb.nceas.metacat.client.Metacat;
|
13
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
14
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
15
|
import edu.ucsb.nceas.utilities.IOUtil;
|
16
|
import junit.framework.Test;
|
17
|
import junit.framework.TestCase;
|
18
|
import junit.framework.TestSuite;
|
19
|
|
20
|
public class MetaCatQueryPerformanceTest extends TestCase {
|
21
|
|
22
|
/**
|
23
|
* Create a suite of tests to be run together
|
24
|
*/
|
25
|
public static Test suite()
|
26
|
{
|
27
|
TestSuite suite = new TestSuite();
|
28
|
return suite;
|
29
|
}
|
30
|
|
31
|
public static void main(String[] arg)
|
32
|
{
|
33
|
if (arg != null & arg.length == 2)
|
34
|
{
|
35
|
String metacatURL = arg[0];
|
36
|
String queryFilePath = arg[1];
|
37
|
try {
|
38
|
//query(metacatURL, queryFilePath);
|
39
|
multiThreadedQuery(metacatURL, queryFilePath);
|
40
|
} catch (Exception e) {
|
41
|
// TODO Auto-generated catch block
|
42
|
e.printStackTrace();
|
43
|
}
|
44
|
}
|
45
|
else
|
46
|
{
|
47
|
System.out.println("Usage: java MetaCatQueryPerformance metacatURL queryFilePath");
|
48
|
}
|
49
|
}
|
50
|
|
51
|
public static String query(String metacatURL, String queryFile) throws MetacatInaccessibleException, IOException
|
52
|
{
|
53
|
Metacat m = MetacatFactory.createMetacatConnection(metacatURL);;
|
54
|
FileReader fr = new FileReader(queryFile);
|
55
|
Reader r = m.query(fr);
|
56
|
//System.out.println("Starting query...");
|
57
|
String result = IOUtil.getAsString(r, true);
|
58
|
System.out.println("Query result:\n" + result);
|
59
|
|
60
|
return result;
|
61
|
}
|
62
|
|
63
|
public static void multiThreadedQuery(String metacatURL, String queryFile) throws InterruptedException {
|
64
|
|
65
|
int nThreads = 25;
|
66
|
ExecutorService executor = Executors.newFixedThreadPool(nThreads);
|
67
|
|
68
|
List<QueryRunner> tasks = new ArrayList<QueryRunner>();
|
69
|
// launch threads
|
70
|
for (int i = 0; i < nThreads; i++) {
|
71
|
QueryRunner query = new QueryRunner(metacatURL, queryFile, i);
|
72
|
tasks.add(query);
|
73
|
executor.execute(query);
|
74
|
}
|
75
|
|
76
|
// now wait
|
77
|
executor.shutdown();
|
78
|
executor.awaitTermination(1, TimeUnit.HOURS);
|
79
|
|
80
|
// now show the runtimes
|
81
|
for (QueryRunner query: tasks) {
|
82
|
System.out.println("query: " + query.id + " took: " + query.runtime);
|
83
|
}
|
84
|
|
85
|
}
|
86
|
}
|
87
|
class QueryRunner implements Runnable{
|
88
|
|
89
|
String metacatURL;
|
90
|
String queryFile;
|
91
|
int id;
|
92
|
long runtime;
|
93
|
|
94
|
public QueryRunner(String metacatURL, String queryFile, int id) {
|
95
|
this.metacatURL = metacatURL;
|
96
|
this.queryFile = queryFile;
|
97
|
this.id = id;
|
98
|
}
|
99
|
|
100
|
@Override
|
101
|
public void run() {
|
102
|
try {
|
103
|
long startTime = System.currentTimeMillis();
|
104
|
MetaCatQueryPerformanceTest.query(metacatURL, queryFile);
|
105
|
long endTime = System.currentTimeMillis();
|
106
|
this.runtime = endTime - startTime;
|
107
|
} catch (Exception e) {
|
108
|
e.printStackTrace();
|
109
|
}
|
110
|
}
|
111
|
}
|