Project

General

Profile

1 3278 tao
package edu.ucsb.nceas.metacattest;
2
3
import java.io.FileReader;
4 7167 leinfelder
import java.io.IOException;
5 3278 tao
import java.io.Reader;
6 7167 leinfelder
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 3278 tao
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 7167 leinfelder
20
public class MetaCatQueryPerformanceTest extends TestCase {
21 3278 tao
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 7167 leinfelder
    	    try {
38
				//query(metacatURL, queryFilePath);
39
				multiThreadedQuery(metacatURL, queryFilePath);
40
			} catch (Exception e) {
41
				// TODO Auto-generated catch block
42
				e.printStackTrace();
43
			}
44 3278 tao
    	}
45
    	else
46
    	{
47
    		System.out.println("Usage: java MetaCatQueryPerformance metacatURL queryFilePath");
48
    	}
49
    }
50 7167 leinfelder
51
    public static String query(String metacatURL, String queryFile) throws MetacatInaccessibleException, IOException
52 3278 tao
    {
53 7167 leinfelder
        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 3278 tao
60 7167 leinfelder
        return result;
61 3278 tao
    }
62 7167 leinfelder
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 3278 tao
}
87 7167 leinfelder
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
}