Skip to content

Commit

Permalink
refactoring LoadTest
Browse files Browse the repository at this point in the history
  • Loading branch information
ScouterProject committed Jul 4, 2015
1 parent e6a3084 commit 0be4bd6
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 359 deletions.
8 changes: 4 additions & 4 deletions scouter.server/src/scouter/server/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public void run() {
exit.deleteOnExit();

System.out.println("System JRE version : " + System.getProperty("java.version"));
System.out.println("This product includes GeoLite data created by MaxMind, available from");
System.out.println("http://www.maxmind.com");
System.out.println("download: http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz");
System.out.println("add configure: geoip_data_city=<download path>/GeoLiteCity.dat");
// System.out.println("This product includes GeoLite data created by MaxMind, available from");
// System.out.println("http://www.maxmind.com");
// System.out.println("download: http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz");
// System.out.println("add configure: geoip_data_city=<download path>/GeoLiteCity.dat");

while (true) {
if (exit.exists() == false) {
Expand Down
1 change: 1 addition & 0 deletions scouter.server/src/scouter/server/term/handler/Help.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ object Help {
System.out.println("\tobjtype = ObjType List");
System.out.println("\tobject = Object List");
System.out.println("\tcounter [objType] = counter list for the objType");
System.out.println("\tvisitor [objType] = today visit users for the objType");
System.out.println("\tdashboard [objType] = javaee's dashboard");
System.out.println("\tdashboard [objType] [Counter] = host's dashdoard");
System.out.println("\trealtime [objType] TPS [SUM/AVG] = realtime counter view");
Expand Down
168 changes: 79 additions & 89 deletions scouter.server/src/scouter/server/test/LoadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,103 +14,93 @@
* limitations under the License.
*/

package scouter.server.test;

package scouter.server.test;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import scouter.util.FileUtil;

public class LoadTest {

public static Random r = new Random();

public static void main(String[] args) throws Throwable {

ExecutorService ex = Executors.newCachedThreadPool();

final String url = args.length == 0 ? "http://127.0.0.1:8080" : args[0];
final String loop = args.length > 1 ? args[1] : "loop=10000";
Runnable r = new Runnable() {
public void run() {
try {
LoadTest.process(url + "/" + getUrl() + "?" + loop);
} catch (Exception x) {
// x.printStackTrace();
// System.out.println(x.getMessage());
}
}
};
while (true) {
ex.execute(r);
// ex.execute(r);
// ex.execute(r);
r.run();
}
}

private static void process(String arg) throws Exception {
long stime = System.currentTimeMillis();
try {
URL u = new URL(arg);

URLConnection uc = u.openConnection();
setUser(uc);
uc.connect();
InputStream o = uc.getInputStream();
byte[] b = FileUtil.readAll(o);
String cookie = uc.getHeaderField("Set-Cookie");
keepCookie(cookie);

o.close();
long dur = System.currentTimeMillis() - stime;
System.out.println(arg + " " + dur + " ms");
} catch (Exception e) {
long dur = System.currentTimeMillis() - stime;
System.out.println(arg + " " + dur + " ms - ERROR");
}
}



private static String getUrl() {
return "test" + r.nextInt(10) + "/" + r.nextInt(100) + ".x";

}

private static Random rand = new Random();
private static List<String> cookies = new ArrayList();

private static void setUser(URLConnection uc) {
if (cookies.size() == 0)
return;
if (Math.abs(rand.nextInt()) % 2 == 0)
return;
int x = Math.abs(rand.nextInt()) % cookies.size();
uc.addRequestProperty("Cookie", "Scouter=" + cookies.get(x));
}

private static void keepCookie(String cookie) {
if (cookie == null)
return;
int x1 = cookie.indexOf("Scouter");
if (x1 >= 0) {
String value = null;
int x2 = cookie.indexOf(';', x1);
if (x2 > 0) {
value = cookie.substring(x1 + "Scouter".length() + 1, x2);
} else {
value = cookie.substring(x1 + "Scouter".length() + 1);
}
cookies.add(value);
}
if (cookies.size() > 10000)
cookies.clear();
}

public class LoadTest {

public static void main(String[] args) throws Throwable {

ExecutorService ex = Executors.newCachedThreadPool();

final String url = args.length == 0 ? "http://127.0.0.1:8080" : args[0];
Runnable r = new Runnable() {
public void run() {
try {
LoadTest.process(url);
} catch (Exception x) {
}
}
};
while (true) {
ex.execute(r);
r.run();
}
}

private static void process(String arg) throws Exception {
long stime = System.currentTimeMillis();
try {
URL u = new URL(arg);
URLConnection uc = u.openConnection();
String uu = setUser(uc);
uc.connect();
InputStream o = uc.getInputStream();
FileUtil.readAll(o);
Map<String, List<String>> heads = uc.getHeaderFields();
List<String> cookie = heads.get("Set-Cookie");
keepCookie("" + cookie);
o.close();
long dur = System.currentTimeMillis() - stime;
System.out.println(arg + " " + dur + " ms " + uu);
} catch (Exception e) {
long dur = System.currentTimeMillis() - stime;
System.out.println(arg + " " + dur + " ms - ERROR");
}
}

private static Random rand = new Random();
private static List<String> cookies = new ArrayList();

private static String setUser(URLConnection uc) {
if (cookies.size() == 0)
return "none";
if (Math.abs(rand.nextInt()) % 2 == 0)
return "none";
int x = Math.abs(rand.nextInt()) % cookies.size();
uc.addRequestProperty("Cookie", "SCOUTER=" + cookies.get(x));

return "SCOUTER=" + cookies.get(x);
}

private static void keepCookie(String cookie) {
if (cookie == null)
return;

int x1 = cookie.indexOf("SCOUTER");
if (x1 >= 0) {
String value = null;
int x2 = cookie.indexOf(';', x1);
if (x2 > 0) {
value = cookie.substring(x1 + "SCOUTER".length() + 1, x2);
} else {
value = cookie.substring(x1 + "SCOUTER".length() + 1);
}
cookies.add(value);
System.out.println("SCOUTER=" + value);
}
if (cookies.size() > 1000000)
cookies.clear();
}
}
114 changes: 0 additions & 114 deletions scouter.server/src/scouter/server/test/LoadTest2.java

This file was deleted.

Loading

0 comments on commit 0be4bd6

Please sign in to comment.