Search in sources :

Example 11 with Stopwatch

use of org.nutz.lang.Stopwatch in project nutz by nutzam.

the class JsonFaster method json.

@Test
public void json() {
    String jsonStr = "{name:'wendal'}";
    nutzJson(100000, jsonStr);
    fastJson(100000, jsonStr);
    System.gc();
    int t = 5000 * 10000;
    Stopwatch sw = Stopwatch.begin();
    nutzJson(t, jsonStr);
    sw.stop();
    System.out.println("Nutz-Json " + t + "次耗时: " + sw.getDuration());
    System.gc();
    sw = Stopwatch.begin();
    // fastJson(t, jsonStr);
    sw.stop();
    System.out.println("Fast-Json " + t + "次耗时: " + sw.getDuration());
    System.gc();
    // -------------------------------------------------------------------
    sw = Stopwatch.begin();
    nutzJson(t, jsonStr);
    sw.stop();
    System.out.println("Nutz-Json " + t + "次耗时: " + sw.getDuration());
    System.gc();
    sw = Stopwatch.begin();
    // fastJson(t, jsonStr);
    sw.stop();
    System.out.println("Fast-Json " + t + "次耗时: " + sw.getDuration());
    System.gc();
}
Also used : Stopwatch(org.nutz.lang.Stopwatch) Test(org.junit.Test)

Example 12 with Stopwatch

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();
}
Also used : Context(org.nutz.lang.util.Context) El(org.nutz.el.El) Stopwatch(org.nutz.lang.Stopwatch) Atom(org.nutz.trans.Atom) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with Stopwatch

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();
        }
    }
}
Also used : NutDao(org.nutz.dao.impl.NutDao) SQLException(java.sql.SQLException) Stopwatch(org.nutz.lang.Stopwatch) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Atom(org.nutz.trans.Atom) SimpleDataSource(org.nutz.dao.impl.SimpleDataSource) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) AbcPet(org.nutz.dao.test.meta.nutzcn.AbcPet) Issue1163Pet(org.nutz.dao.test.meta.issue1163.Issue1163Pet) Pet(org.nutz.dao.test.meta.Pet)

Example 14 with Stopwatch

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);
        // 应用完成后执行用户自定义的 CommandLineRunner
        callRunners(ioc);
    } 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;
}
Also used : Stopwatch(org.nutz.lang.Stopwatch) Ioc(org.nutz.ioc.Ioc) File(java.io.File)

Example 15 with Stopwatch

use of org.nutz.lang.Stopwatch in project nutzcloud by nutzam.

the class UserModule method benchmark.

@Ok("raw")
@At
public String benchmark() {
    // 先确保service能访问
    userService.ping();
    String NL = "\r\n";
    StringBuilder sb = new StringBuilder();
    sb.append("预热1k次").append(NL);
    Stopwatch sw = Stopwatch.begin();
    for (int i = 0; i < 1000; i++) {
        userService.ping();
    }
    sw.stop();
    sb.append("预热耗时: ").append(sw.toString()).append(NL);
    sb.append("单个请求平均耗时: ").append(sw.getDuration() / 1000.0).append(NL);
    sb.append("QPS: ").append((int) (1000.0 / sw.getDuration() * 1000)).append(NL);
    sb.append("-----------------------").append(NL);
    sw = Stopwatch.begin();
    benchmark(10, 10000);
    sw.stop();
    sb.append("10线程执行1w次,耗时: ").append(sw.toString()).append(NL);
    sb.append("单个请求平均耗时: ").append(sw.getDuration() / 10000.0).append(NL);
    sb.append("QPS: ").append((int) (10000.0 / sw.getDuration() * 1000)).append(NL);
    sb.append("-----------------------").append(NL);
    sw = Stopwatch.begin();
    benchmark(25, 10000);
    sw.stop();
    sb.append("25线程执行1w次,耗时: ").append(sw.toString()).append(NL);
    sb.append("单个请求平均耗时: ").append(sw.getDuration() / 10000.0).append(NL);
    sb.append("QPS: ").append((int) (10000.0 / sw.getDuration() * 1000)).append(NL);
    sb.append("-----------------------").append(NL);
    sw = Stopwatch.begin();
    benchmark(100, 20000);
    sw.stop();
    sb.append("100线程执行1w次,耗时: ").append(sw.toString()).append(NL);
    sb.append("单个请求平均耗时: ").append(sw.getDuration() / 20000.0).append(NL);
    sb.append("QPS: ").append((int) (20000.0 / sw.getDuration() * 1000)).append(NL);
    sb.append("-----------------------").append(NL);
    sw = Stopwatch.begin();
    benchmark(200, 20000);
    sw.stop();
    sb.append("200线程执行2w次,耗时: ").append(sw.toString()).append(NL);
    sb.append("单个请求平均耗时: ").append(sw.getDuration() / 20000.0).append(NL);
    sb.append("QPS: ").append((int) (20000.0 / sw.getDuration() * 1000)).append(NL);
    return sb.toString();
}
Also used : Stopwatch(org.nutz.lang.Stopwatch) At(org.nutz.mvc.annotation.At) Ok(org.nutz.mvc.annotation.Ok)

Aggregations

Stopwatch (org.nutz.lang.Stopwatch)15 Test (org.junit.Test)4 File (java.io.File)3 Pet (org.nutz.dao.test.meta.Pet)3 Atom (org.nutz.trans.Atom)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 Issue1163Pet (org.nutz.dao.test.meta.issue1163.Issue1163Pet)2 AbcPet (org.nutz.dao.test.meta.nutzcn.AbcPet)2 Ioc (org.nutz.ioc.Ioc)2 IocBean (org.nutz.ioc.loader.annotation.IocBean)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Ignore (org.junit.Ignore)1 Cnd (org.nutz.dao.Cnd)1