DRUID VS ANTLR4

测试方法

环境:x86_64,eclipse kepler,jdk 6

测试对象:antlr v4,druid手写sql parser模块

测试过程:分别采用单线程、多线程并发测试。单线程时,比较antlr、druid各自解析1次同一条sql语句的性能;多线程(50线程)时,比较antlr、druid所有线程完成1次同一条sql解析的性能。

测试结果##

类别 单线程(druid比antlr) 多线程(druid比antlr)
简单select 7倍 6倍
复杂select 约200倍 约1600倍
Insert 6倍 11倍
Update 15倍 13倍
Delete 3倍 4倍

总结##

1、性能:druid好于antlr。

2、语法支持:两者皆可实现各类语法,但antlr易于druid实现。目前发现druid没有完全实现pg语法,如int ‘123’类型转换。

3、可维护性:antlr好于druid 特别是体现在新增语法时,修改druid的工作量大于antlr。

4、可读性:antlr好于druid antlr采用独立的语法文件管理语法规则,druid语法规则与代码耦合。

5、关键字支持:两者皆支持。druid需要使用switch语句块穷举。


package com.laudandjolynn.test;

import java.text.NumberFormat;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options; import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
import com.laudandjolynn.idb.AntlrParser; public class SqlParser {
private final static String ANTLR = "ANTLR";
private final static String DRUID = "DRUID"; public static void main(String[] args) throws Exception {
CommandLineParser clp = new BasicParser();
Options options = new Options();
options.addOption("t", true, "");
options.addOption("m", true, "");
options.addOption("p", true, "");
options.addOption("s", true, "");
CommandLine cl = clp.parse(options, args);
String helpMsg = "usage: java -jar test.jar -t {antlr|druid} [-m times] [-p threads] -s sql";
if (!cl.hasOption('t') || !cl.hasOption('s')) {
System.out.println(helpMsg);
System.exit(0);
} String tool = cl.getOptionValue('t');
String sql = cl.getOptionValue('s');
int threadCount = cl.hasOption('p') ? Integer.valueOf(cl
.getOptionValue('p')) : 1;
int executeTimes = cl.hasOption('m') ? Integer.valueOf(cl
.getOptionValue('m')) : 1;
if (threadCount == 1) {
single(1, sql, tool, false);
single(executeTimes, sql, tool, true);
} else {
multiple(1, sql, tool, 1, false);
multiple(executeTimes, sql, tool, threadCount, true);
}
System.exit(0);
} private static void single(int executeTimes, String sql, String tool,
boolean stat) {
long sum = 0;
long max = Long.MIN_VALUE;
long min = Long.MAX_VALUE;
int maxIndex = -1;
for (int j = 0; j < executeTimes; j++) {
long start = System.nanoTime();
parse(sql, tool);
long elapse = System.nanoTime() - start;
if (max < elapse) {
max = elapse;
maxIndex = j;
}
if (min > elapse) {
min = elapse;
}
sum += elapse;
}
if (stat) {
NumberFormat format = NumberFormat.getInstance();
format.setMaximumFractionDigits(3);
System.out.println("max: " + format.format(max / 1000000.0)
+ " ms. max value index: " + maxIndex);
System.out
.println("min: " + format.format(min / 1000000.0) + " ms");
System.out.println("avg: "
+ format.format(sum / (double) executeTimes / 1000000.0)
+ " ms");
}
} private static void multiple(int executeTimes, String sql, String tool,
int threadCount, boolean stat) throws Exception {
ExecutorService executorService = Executors
.newFixedThreadPool(threadCount);
CompletionService<Long[]> completionService = new ExecutorCompletionService<Long[]>(
executorService);
for (int i = 0; i < threadCount; i++) {
completionService.submit(new Parser(executeTimes, sql, tool));
} long sum = 0;
long max = Long.MIN_VALUE;
long min = Long.MAX_VALUE;
int maxIndex = -1;
for (int i = 0; i < threadCount; i++) {
Long[] elapses = completionService.take().get();
for (int j = 0; j < executeTimes; j++) {
if (max < elapses[j]) {
max = elapses[j];
maxIndex = i * j;
}
if (min > elapses[j]) {
min = elapses[j];
}
sum += elapses[j];
}
} if (stat) {
NumberFormat format = NumberFormat.getInstance();
format.setMaximumFractionDigits(3);
System.out.println("max: " + format.format(max / 1000000.0)
+ " ms. max value index: " + maxIndex);
System.out
.println("min: " + format.format(min / 1000000.0) + " ms");
System.out.println("avg: "
+ format.format(sum / executeTimes / threadCount
/ 1000000.0) + " ms");
}
} private static class Parser implements Callable<Long[]> {
private String sql = null;
private String tool = null;
private int executeTimes; public Parser(int executeTimes, String sql, String tool) {
this.executeTimes = executeTimes;
this.sql = sql;
this.tool = tool;
} @Override
public Long[] call() throws Exception {
Long result[] = new Long[executeTimes];
for (int i = 0; i < executeTimes; i++) {
long start = System.nanoTime();
parse(sql, tool);
result[i] = System.nanoTime() - start;
}
return result;
}
} private static void parse(String sql, String tool) {
if (ANTLR.equalsIgnoreCase(tool)) {
AntlrParser.parse(sql);
} else if (DRUID.equalsIgnoreCase(tool)) {
new PGSQLStatementParser(sql).parseStatement();
}
}
}

Druid VS Antlr4的更多相关文章

  1. Spring + SpringMVC + Druid + MyBatis 给你一个灵活的后端解决方案

    生命不息,折腾不止. 折腾能遇到很多坑,填坑我理解为成长. 两个月前自己倒腾了一套用开源框架构建的 JavaWeb 后端解决方案. Spring + SpringMVC + Druid + JPA(H ...

  2. Spring + SpringMVC + Druid + JPA(Hibernate impl) 给你一个稳妥的后端解决方案

    最近手头的工作不太繁重,自己试着倒腾了一套用开源框架组建的 JavaWeb 后端解决方案. 感觉还不错的样子,但实践和项目实战还是有很大的落差,这里只做抛砖引玉之用. 项目 git 地址:https: ...

  3. 学记:spring boot使用官网推荐以外的其他数据源druid

    虽然spring boot提供了4种数据源的配置,但是如果要使用其他的数据源怎么办?例如,有人就是喜欢druid可以监控的强大功能,有些人项目的需要使用c3p0,那么,我们就没办法了吗?我们就要编程式 ...

  4. druid连接池获取不到连接的一种情况

    数据源一开始配置: jdbc.initialSize=1jdbc.minIdle=1jdbc.maxActive=5 程序运行一段时间后,执行查询抛如下异常: exception=org.mybati ...

  5. druid配置数据库连接使用密文密码

    spring使用druid配置dataSource片段代码 dataSource配置 <!-- 基于Druid数据库链接池的数据源配置 --> <bean id="data ...

  6. [转]阿里巴巴数据库连接池 druid配置详解

    一.背景 java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池.数据库连接池有很多选择,c3p.dhcp.proxool等,druid作为一名后起之秀,凭借其出色 ...

  7. 技术杂记-改造具有监控功能的数据库连接池阿里Druid,支持simple-jndi,kettle

    kettle内置的jndi管理是simple-jndi,功能确实比较简单,我需要监控kettle性能,druid确实是很不错的选择,但没有提供对应的支持,我改进了druid源码,实现了simple-j ...

  8. sql 连接数不释放 ,Druid异常:wait millis 40000, active 600, maxActive 600

    Hibernate + Spring + Druid 数据库mysql 由于配置如下 <bean id="dataSource" class="com.alibab ...

  9. druid sql黑名单 报异常 sql injection violation, part alway true condition not allow

    最近使用druid,发现阿里这个连接池 真的很好用,可以监控到连接池活跃连接数 开辟到多少个连接数 关闭了多少个,对于我在项目中查看错误 问题,很有帮助, 但是最近发现里面 有条sql语句 被拦截了, ...

随机推荐

  1. [Noi2016]区间

    题目描述 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间,使得这 m个区间共同包含至少一个位置.换句话说,就是使得存在一个 x,使得对于每一 ...

  2. [Codeforces]853E - Lada Malina

    题目大意:给出平面上$n$个带权点$f_{i}$,再给出$k$个向量$v_{i}$,每次询问给出一个点$p$和一个值$t$,求能满足$f_{i}+\sum w_{j}v_{j}=p(-t<=w_ ...

  3. ●BZOJ 2049 [Sdoi2008]Cave洞穴勘测

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题解: LCT入门题 就是判两个点是否在同一颗树里 代码: #include<c ...

  4. 【CodeVs 6128 Lence的方块们】

    ·希望除了内部人员以外能有人通过这道题,因为这是大米饼第一次改编的题 ·我所见到的"本题原版"的题解也很少,搜索一下应该是: #include<stdio.h> #in ...

  5. jQuery Datetable 渲染

    渲染器 有些情况下,使用表时,表中的行的数据源不包含您希望在表中直接显示的值.您可能希望将其转换为不同的表示形式(时间戳为人类可读的格式),合并数据点(名字和姓氏)或对该值执行一些计算(计算营业额和费 ...

  6. 阿里 & 酷家乐:实习生面试

    最近海投了十家公司,暂时有阿里两面(已凉).酷家乐两面(大概凉了).网易一面.前两个都是基础知识发挥得还可以,两家公司二面都凉凉. 阿里一面(3.21 26min) 刚好买了中饭回宿舍打开正准备吃的时 ...

  7. numpy.random中的shuffle和permutation以及mini-batch调整数据集(X, Y)

    0. numpy.random中的shuffle和permutation numpy.random.shuffle(x) and numpy.random.permutation(x),这两个有什么不 ...

  8. Android ImgView属性

    ImageView是用于界面上显示图片的控件. 属性 1.为ImageView设置图片 ①android:src="@drawable/img1": src设置图片,默认图片等比例 ...

  9. Dubbo介绍和服务架构分析

    Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成.使用zookeeper作为服务的注册中心,对外提供服务 ...

  10. 在OpenCV3.1.0中使用SIFT,SURF算法

    写在前边: 1.我使用的是python2.7 + OpenCV3.1.0 2.OpenCV3.0.0+的文档有很大问题,很多文档写的还是OpenCV2.0+, OpenCV3.0+根本用不了,其中有一 ...