comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/tax/Query.java @ 68:5028fdace37b

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