Druid VS Antlr4
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的更多相关文章
- Spring + SpringMVC + Druid + MyBatis 给你一个灵活的后端解决方案
生命不息,折腾不止. 折腾能遇到很多坑,填坑我理解为成长. 两个月前自己倒腾了一套用开源框架构建的 JavaWeb 后端解决方案. Spring + SpringMVC + Druid + JPA(H ...
- Spring + SpringMVC + Druid + JPA(Hibernate impl) 给你一个稳妥的后端解决方案
最近手头的工作不太繁重,自己试着倒腾了一套用开源框架组建的 JavaWeb 后端解决方案. 感觉还不错的样子,但实践和项目实战还是有很大的落差,这里只做抛砖引玉之用. 项目 git 地址:https: ...
- 学记:spring boot使用官网推荐以外的其他数据源druid
虽然spring boot提供了4种数据源的配置,但是如果要使用其他的数据源怎么办?例如,有人就是喜欢druid可以监控的强大功能,有些人项目的需要使用c3p0,那么,我们就没办法了吗?我们就要编程式 ...
- druid连接池获取不到连接的一种情况
数据源一开始配置: jdbc.initialSize=1jdbc.minIdle=1jdbc.maxActive=5 程序运行一段时间后,执行查询抛如下异常: exception=org.mybati ...
- druid配置数据库连接使用密文密码
spring使用druid配置dataSource片段代码 dataSource配置 <!-- 基于Druid数据库链接池的数据源配置 --> <bean id="data ...
- [转]阿里巴巴数据库连接池 druid配置详解
一.背景 java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池.数据库连接池有很多选择,c3p.dhcp.proxool等,druid作为一名后起之秀,凭借其出色 ...
- 技术杂记-改造具有监控功能的数据库连接池阿里Druid,支持simple-jndi,kettle
kettle内置的jndi管理是simple-jndi,功能确实比较简单,我需要监控kettle性能,druid确实是很不错的选择,但没有提供对应的支持,我改进了druid源码,实现了simple-j ...
- sql 连接数不释放 ,Druid异常:wait millis 40000, active 600, maxActive 600
Hibernate + Spring + Druid 数据库mysql 由于配置如下 <bean id="dataSource" class="com.alibab ...
- druid sql黑名单 报异常 sql injection violation, part alway true condition not allow
最近使用druid,发现阿里这个连接池 真的很好用,可以监控到连接池活跃连接数 开辟到多少个连接数 关闭了多少个,对于我在项目中查看错误 问题,很有帮助, 但是最近发现里面 有条sql语句 被拦截了, ...
随机推荐
- 2015 多校联赛 ——HDU5360(贪心+优先队列)
Sample Input 4 8 4 1 3 2 2 1 0 3 5 3 6 4 2 1 7 6 8 3 3 2 0 5 0 3 6 4 5 2 7 7 6 7 6 8 2 2 3 3 3 0 0 2 ...
- bzoj5100 [POI2018]Plan metra 构造
5100: [POI2018]Plan metra Time Limit: 40 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 189 Sol ...
- Map 探索
关于Map问题我一直了解的不是很透彻,下面进行比较系统的总结一下. 1.
- 微信小程序-参数传递与事件处理
前言 开发过程中经常会遇到从一个页面携带数据到另一个页面的情况,所以需要知道以下信息,什么是事件?有哪些传递方式?如果传递数组呢?如果传递对象呢? 一.事件 什么是事件 事件是视图层到逻辑层的通讯方式 ...
- SpringMVC中url-pattern /和/*的区别
http://blog.csdn.net/u010648555/article/details/51612030
- Java Native方法
一. 什么是Native Method 简单地讲,一个Native Method就是一个java调用非java代码的接口.一个Native Method是这样一个java的方法:该方法的实现由非j ...
- 初识Redis系列之三:Redis支持的数据类型及使用
支持的数据类型有五种: string(字符串).hash(哈希).list(列表).set(集合)及zset(sorted set:有序集合): 下面分别对这几种类型进行简单的Redis存取操作 1: ...
- PHP 常用函数集合
PHP is_numeric() 函数 由 陈 创建, 最后一次修改 2016-12-02 定义和用法 is_numeric() - 检测变量是否为数字或数字字符串 语法 bool is_numeri ...
- 原生js去掉所有的html标签,最终得到HTML标签中的所有内容
替换掉所有的 html标签,最终得到Html标签中的内容 <script> //替换掉所有的 html标签,最终得到Html标签中的内容 var req="<div sty ...
- python学习之路基础篇(三)
博客参考:http://www.cnblogs.com/wupeiqi/articles/4943406.html http://www.cnblogs.com/luotianshuai/p/4949 ...