| 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 |
|
| 9 |
import shared.Shared; |
| 10 |
import shared.Timer; |
| 11 |
import shared.Tools; |
| 12 |
|
| 13 |
public class Query { |
| 14 |
|
| 15 |
|
| 16 |
public static void main(String[] args){ |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 46 |
for(int i=0; i<requests; i++){ |
| 47 |
String s=request(x); |
| 48 |
|
| 49 |
if(i==0){t3.start();} |
| 50 |
|
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
t3.stop(); |
| 55 |
|
| 56 |
t.stop("Time: \t"); |
| 57 |
double qps=requests*1000000000.0/(t.elapsed); |
| 58 |
System.err.println(Tools.format("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(Tools.format("Qps2: \t%.2f", qps2)); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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(); |
| 127 |
return new String(buffer, 0, next); |
| 128 |
} catch (IOException e) { |
| 129 |
|
| 130 |
e.printStackTrace(); |
| 131 |
} |
| 132 |
try { |
| 133 |
is.close(); |
| 134 |
} catch (IOException e) { |
| 135 |
|
| 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 |
|
| 148 |
e1.printStackTrace(); |
| 149 |
return null; |
| 150 |
} |
| 151 |
|
| 152 |
try { |
| 153 |
InputStream is=url.openStream(); |
| 154 |
return is; |
| 155 |
} catch (IOException e) { |
| 156 |
|
| 157 |
e.printStackTrace(); |
| 158 |
} |
| 159 |
return null; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
} |