use of org.nutz.lang.Stopwatch in project nutz by nutzam.
the class UploadingSpeedTest method main.
public static void main(String[] args) {
if (0 == args.length) {
System.err.println("Lack files directory!");
System.exit(0);
}
File dir = Files.findFile(args[0]);
if (null == dir) {
System.err.println("Fail to found directory: " + args[0]);
System.exit(0);
}
final Uploading up = UploadUnit.TYPE.born();
final UploadingContext uc = UploadingContext.create("~/nutz/unit/uploadtmp");
File[] files = dir.listFiles();
final MockHttpServletRequest req = request().setInputStream(insmulti("UTF-8", files));
req.setSession(session(context()));
req.init();
Object monLock = new Object();
int monInterval = 2000;
UploadMonitor mon = new UploadMonitor(monLock, req.getSession(), out, monInterval);
Thread monThread = new Thread(mon, "UploadingMonitor");
monThread.start();
out.println("Begin...");
Stopwatch sw = null;
try {
sw = Stopwatch.run(new Atom() {
public void run() {
try {
up.parse(req, uc);
} catch (UploadException e) {
throw Lang.wrapThrow(e);
}
}
});
} catch (Exception e) {
e.printStackTrace();
} finally {
mon.stop();
out.println("\n...Done!");
if (null != sw)
out.println(sw);
}
}
use of org.nutz.lang.Stopwatch in project nutz by nutzam.
the class JsonFaster method json.
@Test
public void json() {
nutzJson(10000);
fastJson(10000);
Stopwatch sw = Stopwatch.begin();
nutzJson(50 * 10000);
sw.stop();
System.out.println("Nutz-Json 50w次耗时: " + sw.getDuration());
sw.start();
fastJson(50 * 10000);
System.out.println("Fast-Json 50w次耗时: " + sw.getDuration());
//-------------------------------------------------------------------
sw.start();
nutzJson(50 * 10000);
sw.stop();
System.out.println("Nutz-Json 50w次耗时: " + sw.getDuration());
sw.start();
fastJson(50 * 10000);
sw.stop();
System.out.println("Fast-Json 50w次耗时: " + sw.getDuration());
}
use of org.nutz.lang.Stopwatch in project nutz by nutzam.
the class SimpleSpeedTest method test_speed.
@Ignore
@Test
public void test_speed() throws SecurityException, NoSuchMethodException {
final SimpleSpeedTest z = new SimpleSpeedTest();
final String elstr = "num + (i - 1 + 2 - 3 + 4 - 5 + 6 - 7)-z.abc(i)";
final Context context = Lang.context("{num:0}");
context.set("z", z);
System.out.println("\n" + Strings.dup('=', 100));
Stopwatch sw = Stopwatch.run(new Atom() {
public void run() {
int num = 0;
for (int i = 0; i < max; i++) num = num + (i - 1 + 2 - 3 + 4 - 5 + 6 - 7) - z.abc(i);
//System.out.println("Num: " + num);
}
});
System.out.println("\n" + Strings.dup('=', 100));
Stopwatch sw3 = Stopwatch.run(new Atom() {
public void run() {
try {
context.set("num", 0);
for (int i = 0; i < max; i++) context.set("num", El.eval(context.set("i", i), elstr));
//System.out.println("Num: " + context.getInt("num"));
} catch (Exception e) {
throw Lang.wrapThrow(e);
}
}
});
System.out.println("\n" + Strings.dup('=', 100));
Stopwatch sw4 = Stopwatch.run(new Atom() {
public void run() {
try {
El el2pre = new El(elstr);
context.set("num", 0);
context.set("z", z);
for (int i = 0; i < max; i++) context.set("num", el2pre.eval(context.set("i", i)));
//System.out.println("Num: " + context.getInt("num"));
} catch (Exception e) {
throw Lang.wrapThrow(e);
}
}
});
System.out.println("\n" + Strings.dup('=', 100));
Stopwatch sw5 = Stopwatch.run(new Atom() {
public void run() {
try {
El el2pre = new El(elstr);
context.set("num", 0);
context.set("z", z);
for (int i = 0; i < max; i++) context.set("num", el2pre.eval(context.set("i", i)));
//System.out.println("Num: " + context.getInt("num"));
} catch (Exception e) {
throw Lang.wrapThrow(e);
}
}
});
System.out.println("\n" + Strings.dup('=', 100));
System.out.printf("\n%20s : %s", "Invoke", sw.toString());
System.out.printf("\n%20s : %s", "Reflect", sw3.toString());
System.out.printf("\n%20s : %s", "Reflect", sw4.toString());
System.out.printf("\n%20s : %s", "Reflect", sw5.toString());
System.out.println();
}
use of org.nutz.lang.Stopwatch in project nutz by nutzam.
the class NutLoading method load.
public UrlMapping load(NutConfig config) {
if (log.isInfoEnabled()) {
log.infof("Nutz Version : %s ", Nutz.version());
log.infof("Nutz.Mvc[%s] is initializing ...", config.getAppName());
}
if (log.isDebugEnabled()) {
Properties sys = System.getProperties();
log.debug("Web Container Information:");
log.debugf(" - Default Charset : %s", Encoding.defaultEncoding());
log.debugf(" - Current . path : %s", new File(".").getAbsolutePath());
log.debugf(" - Java Version : %s", sys.get("java.version"));
log.debugf(" - File separator : %s", sys.get("file.separator"));
log.debugf(" - Timezone : %s", sys.get("user.timezone"));
log.debugf(" - OS : %s %s", sys.get("os.name"), sys.get("os.arch"));
log.debugf(" - ServerInfo : %s", config.getServletContext().getServerInfo());
log.debugf(" - Servlet API : %d.%d", config.getServletContext().getMajorVersion(), config.getServletContext().getMinorVersion());
if (config.getServletContext().getMajorVersion() > 2 || config.getServletContext().getMinorVersion() > 4)
log.debugf(" - ContextPath : %s", config.getServletContext().getContextPath());
log.debugf(" - context.tempdir : %s", config.getAttribute("javax.servlet.context.tempdir"));
log.debugf(" - MainModule : %s", config.getMainModule().getName());
}
/*
* 准备返回值
*/
UrlMapping mapping;
Ioc ioc = null;
/*
* 准备计时
*/
Stopwatch sw = Stopwatch.begin();
try {
/*
* 检查主模块,调用本函数前,已经确保过有声明 MainModule 了
*/
Class<?> mainModule = config.getMainModule();
/*
* 创建上下文
*/
createContext(config);
/*
* 检查 Ioc 容器并创建和保存它
*/
ioc = createIoc(config, mainModule);
/*
* 组装UrlMapping
*/
mapping = evalUrlMapping(config, mainModule, ioc);
/*
* 分析本地化字符串
*/
evalLocalization(config, mainModule);
// 初始化SessionProvider
createSessionProvider(config, mainModule);
/*
* 执行用户自定义 Setup
*/
evalSetup(config, mainModule);
} catch (Exception e) {
if (log.isErrorEnabled())
log.error("Error happend during start serivce!", e);
if (ioc != null) {
log.error("try to depose ioc");
try {
ioc.depose();
} catch (Throwable e2) {
log.error("error when depose ioc", e);
}
}
throw Lang.wrapThrow(e, LoadingException.class);
}
// ~ Done ^_^
sw.stop();
if (log.isInfoEnabled())
log.infof("Nutz.Mvc[%s] is up in %sms", config.getAppName(), sw.getDuration());
return mapping;
}
use of org.nutz.lang.Stopwatch in project nutz by nutzam.
the class SimpleDaoTest method test_fastinsert_speed.
// @Test
// public void test_map_blob() throws FileNotFoundException {
// if (dao.exists("t_test_map_blob")) {
// dao.drop("t_test_map_blob");
// Lang.quiteSleep(1000);
// }
// dao.execute(Sqls.create("create table t_test_map_blob(id
// VARCHAR(60),filecontent blob)"));
//
// NutMap map = new NutMap().setv(".table", "t_test_map_blob");
// map.put("id", R.UU32());
// map.put("filecontent", new
// FileInputStream("W:\\usb3.0_intel_1.0.10.255_w7.zip"));
//
// dao.insert(map);
//
// Record re = dao.fetch("t_test_map_blob", Cnd.NEW());
// assertNotNull(re);
// System.out.println(re.get("filecontent").getClass());
// System.out.println(new String((byte[])re.get("filecontent")));
//
//// assertEquals("你好", new String((byte[])re.get("filecontent")));
// }
//@Test
public void test_fastinsert_speed() {
SimpleDataSource ds = new SimpleDataSource();
ds.setJdbcUrl("jdbc:mysql://localhost/nutztest");
ds.setUsername("root");
ds.setPassword("root");
dao = new NutDao(ds);
// 删表重建
dao.create(Pet.class, true);
Lang.sleep(1000);
Stopwatch sw = Stopwatch.begin();
// 生成10*2000个对象
List<List<Pet>> list = new ArrayList<List<Pet>>();
for (int i = 0; i < 10; i++) {
List<Pet> pets = new ArrayList<Pet>();
for (int j = 0; j < 2000; j++) {
Pet pet = Pet.create(R.UU32());
pets.add(pet);
}
list.add(pets);
}
sw.stop();
System.out.println("生成对象的耗时: " + sw);
for (final List<Pet> tmp : list) {
sw = Stopwatch.begin();
Trans.exec(new Atom() {
public void run() {
dao.fastInsert(tmp);
}
});
sw.stop();
System.out.println("fastInsert插入2000个对象的耗时" + sw);
}
dao.create(Pet.class, false);
for (int i = 0; i < 10; i++) {
try {
final int t = i;
Connection conn = ds.getConnection();
conn.setAutoCommit(false);
sw = Stopwatch.begin();
System.out.println(System.currentTimeMillis());
PreparedStatement ps = conn.prepareStatement("INSERT INTO t_pet(name,alias) VALUES(?,?)");
System.out.println(System.currentTimeMillis());
for (int j = 0; j < 2000; j++) {
ps.setString(1, "pet_" + t + "_" + j);
ps.setString(2, "");
// ps.setInt(3, 30);
// ps.setInt(4, 0);
// ps.setDate(5, null);
// ps.setFloat(6, 0);
ps.addBatch();
}
System.out.println(System.currentTimeMillis());
ps.executeBatch();
conn.commit();
sw.stop();
System.out.println(sw);
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Aggregations