jpayne@68
|
1 package tax;
|
jpayne@68
|
2
|
jpayne@68
|
3 import java.io.IOException;
|
jpayne@68
|
4 import java.io.InputStream;
|
jpayne@68
|
5 import java.net.MalformedURLException;
|
jpayne@68
|
6 import java.net.URL;
|
jpayne@68
|
7 import java.util.Arrays;
|
jpayne@68
|
8 import java.util.Locale;
|
jpayne@68
|
9
|
jpayne@68
|
10 import shared.Shared;
|
jpayne@68
|
11 import shared.Timer;
|
jpayne@68
|
12
|
jpayne@68
|
13 public class Query {
|
jpayne@68
|
14
|
jpayne@68
|
15 /** For testing throughput */
|
jpayne@68
|
16 public static void main(String[] args){
|
jpayne@68
|
17
|
jpayne@68
|
18 // sun.net.http.errorstream.enableBuffering=true;
|
jpayne@68
|
19
|
jpayne@68
|
20 // System.setProperty("sun.net.http.errorstream.enableBuffering", "true");
|
jpayne@68
|
21 // System.setProperty("http.errorstream.enableBuffering", "true");
|
jpayne@68
|
22 ////// System.setProperty("", "");
|
jpayne@68
|
23 // System.setProperty("http.keepAlive", "true");
|
jpayne@68
|
24 // System.setProperty("http.maxConnections", "50");
|
jpayne@68
|
25 //// System.setProperty("", "");
|
jpayne@68
|
26 //// System.setProperty("", "");
|
jpayne@68
|
27 //
|
jpayne@68
|
28 // System.out.println("sun.net.http.errorstream.enableBuffering="+System.getProperty("sun.net.http.errorstream.enableBuffering"));
|
jpayne@68
|
29 // System.out.println("sun.net.http.errorstream.timeout="+System.getProperty("sun.net.http.errorstream.timeout"));
|
jpayne@68
|
30 // System.out.println("sun.net.http.errorstream.bufferSize="+System.getProperty("sun.net.http.errorstream.bufferSize"));
|
jpayne@68
|
31 // System.out.println("http.keepAlive="+System.getProperty("http.keepAlive"));
|
jpayne@68
|
32 // System.out.println("http.maxConnections="+System.getProperty("http.maxConnections"));
|
jpayne@68
|
33 // System.out.println(System.getProperties());
|
jpayne@68
|
34 // System.out.println("="+System.getProperty(""));
|
jpayne@68
|
35 // System.out.println("="+System.getProperty(""));
|
jpayne@68
|
36 // System.out.println("="+System.getProperty(""));
|
jpayne@68
|
37
|
jpayne@68
|
38 String x=args[0];
|
jpayne@68
|
39 int requests=(args.length>1 ? Integer.parseInt(args[1]) : 1);
|
jpayne@68
|
40
|
jpayne@68
|
41 Timer t=new Timer();
|
jpayne@68
|
42 Timer t2=new Timer();
|
jpayne@68
|
43 Timer t3=new Timer();
|
jpayne@68
|
44
|
jpayne@68
|
45 // URL url=toUrl(x);
|
jpayne@68
|
46 for(int i=0; i<requests; i++){
|
jpayne@68
|
47 String s=request(x);
|
jpayne@68
|
48 // System.out.println(s);
|
jpayne@68
|
49 if(i==0){t3.start();}
|
jpayne@68
|
50 //t2.stop("");
|
jpayne@68
|
51 //t2.start();
|
jpayne@68
|
52 }
|
jpayne@68
|
53
|
jpayne@68
|
54 t3.stop();
|
jpayne@68
|
55
|
jpayne@68
|
56 t.stop("Time: \t");
|
jpayne@68
|
57 double qps=requests*1000000000.0/(t.elapsed);
|
jpayne@68
|
58 System.err.println(String.format(Locale.ROOT, "Qps: \t%.2f", qps));
|
jpayne@68
|
59
|
jpayne@68
|
60 System.err.println("Time2: \t"+t3);
|
jpayne@68
|
61 double qps2=(requests-1)*1000000000.0/(t3.elapsed);
|
jpayne@68
|
62 System.err.println(String.format(Locale.ROOT, "Qps2: \t%.2f", qps2));
|
jpayne@68
|
63
|
jpayne@68
|
64 // System.out.println(s);
|
jpayne@68
|
65
|
jpayne@68
|
66 // BufferedReader reader;
|
jpayne@68
|
67 // try {
|
jpayne@68
|
68 // reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
|
jpayne@68
|
69 //
|
jpayne@68
|
70 // for (String line; (line = reader.readLine()) != null;) {
|
jpayne@68
|
71 // System.out.println(line);
|
jpayne@68
|
72 // }
|
jpayne@68
|
73 //
|
jpayne@68
|
74 // } catch (UnsupportedEncodingException e) {
|
jpayne@68
|
75 // // TODO Auto-generated catch block
|
jpayne@68
|
76 // e.printStackTrace();
|
jpayne@68
|
77 // } catch (IOException e) {
|
jpayne@68
|
78 // // TODO Auto-generated catch block
|
jpayne@68
|
79 // e.printStackTrace();
|
jpayne@68
|
80 // }
|
jpayne@68
|
81 }
|
jpayne@68
|
82
|
jpayne@68
|
83 public static int[] getGi(String...args){
|
jpayne@68
|
84 return get("pt_gi", args);
|
jpayne@68
|
85 }
|
jpayne@68
|
86
|
jpayne@68
|
87 public static int[] getAccession(String...args){
|
jpayne@68
|
88 return get("pt_accession", args);
|
jpayne@68
|
89 }
|
jpayne@68
|
90
|
jpayne@68
|
91 public static int[] get(String type, String...args){
|
jpayne@68
|
92 String host=Shared.taxServer();
|
jpayne@68
|
93 StringBuilder sb=new StringBuilder(host.length()+32);
|
jpayne@68
|
94 sb.append(host);
|
jpayne@68
|
95 sb.append(type);
|
jpayne@68
|
96 sb.append('\t');
|
jpayne@68
|
97 String comma="";
|
jpayne@68
|
98 for(String arg : args){
|
jpayne@68
|
99 sb.append(comma);
|
jpayne@68
|
100 sb.append(arg);
|
jpayne@68
|
101 comma=",";
|
jpayne@68
|
102 }
|
jpayne@68
|
103 String result=request(sb.toString());
|
jpayne@68
|
104 String[] split=result.split(",");
|
jpayne@68
|
105 int[] ret=new int[split.length];
|
jpayne@68
|
106 for(int i=0; i<split.length; i++){
|
jpayne@68
|
107 ret[i]=Integer.parseInt(split[i]);
|
jpayne@68
|
108 }
|
jpayne@68
|
109 return ret;
|
jpayne@68
|
110 }
|
jpayne@68
|
111
|
jpayne@68
|
112 public static String request(String x){
|
jpayne@68
|
113 InputStream is=toStream(x);
|
jpayne@68
|
114 if(is==null){return null;}
|
jpayne@68
|
115 try {
|
jpayne@68
|
116 byte[] buffer=new byte[256];
|
jpayne@68
|
117 int count=is.read(buffer);
|
jpayne@68
|
118 int next=0;
|
jpayne@68
|
119 while(count>-1){
|
jpayne@68
|
120 next+=count;
|
jpayne@68
|
121 if(next>=buffer.length){
|
jpayne@68
|
122 buffer=Arrays.copyOf(buffer, buffer.length*2);
|
jpayne@68
|
123 }
|
jpayne@68
|
124 count=is.read(buffer, next, buffer.length-next);
|
jpayne@68
|
125 }
|
jpayne@68
|
126 is.close(); //TODO: Why am I not closing this stream?
|
jpayne@68
|
127 return new String(buffer, 0, next);
|
jpayne@68
|
128 } catch (IOException e) {
|
jpayne@68
|
129 // TODO Auto-generated catch block
|
jpayne@68
|
130 e.printStackTrace();
|
jpayne@68
|
131 }
|
jpayne@68
|
132 try {
|
jpayne@68
|
133 is.close();
|
jpayne@68
|
134 } catch (IOException e) {
|
jpayne@68
|
135 // TODO Auto-generated catch block
|
jpayne@68
|
136 e.printStackTrace();
|
jpayne@68
|
137 }
|
jpayne@68
|
138 return null;
|
jpayne@68
|
139 }
|
jpayne@68
|
140
|
jpayne@68
|
141 public static InputStream toStream(String x){
|
jpayne@68
|
142 URL url=null;
|
jpayne@68
|
143
|
jpayne@68
|
144 try {
|
jpayne@68
|
145 url = new URL(x);
|
jpayne@68
|
146 } catch (MalformedURLException e1) {
|
jpayne@68
|
147 // TODO Auto-generated catch block
|
jpayne@68
|
148 e1.printStackTrace();
|
jpayne@68
|
149 return null;
|
jpayne@68
|
150 }
|
jpayne@68
|
151
|
jpayne@68
|
152 try {
|
jpayne@68
|
153 InputStream is=url.openStream();
|
jpayne@68
|
154 return is;
|
jpayne@68
|
155 } catch (IOException e) {
|
jpayne@68
|
156 // TODO Auto-generated catch block
|
jpayne@68
|
157 e.printStackTrace();
|
jpayne@68
|
158 }
|
jpayne@68
|
159 return null;
|
jpayne@68
|
160 }
|
jpayne@68
|
161
|
jpayne@68
|
162 // public static URL toUrl(String x){
|
jpayne@68
|
163 // URL url=null;
|
jpayne@68
|
164 // try {
|
jpayne@68
|
165 // url = new URL(x);
|
jpayne@68
|
166 // } catch (MalformedURLException e1) {
|
jpayne@68
|
167 // // TODO Auto-generated catch block
|
jpayne@68
|
168 // e1.printStackTrace();
|
jpayne@68
|
169 // }
|
jpayne@68
|
170 //
|
jpayne@68
|
171 // return url;
|
jpayne@68
|
172 // }
|
jpayne@68
|
173 //
|
jpayne@68
|
174 // public static URLConnection toUrlConnection(URL url, String x){
|
jpayne@68
|
175 // URLConnection urlc=null;
|
jpayne@68
|
176 //
|
jpayne@68
|
177 // try {
|
jpayne@68
|
178 // urlc = url.openConnection();
|
jpayne@68
|
179 // } catch (IOException e) {
|
jpayne@68
|
180 // // TODO Auto-generated catch block
|
jpayne@68
|
181 // e.printStackTrace();
|
jpayne@68
|
182 // }
|
jpayne@68
|
183 // return urlc;
|
jpayne@68
|
184 // }
|
jpayne@68
|
185
|
jpayne@68
|
186 }
|