当前位置:网站首页>Application of JDBC in performance test
Application of JDBC in performance test
2022-06-30 20:06:00 【Alibaba cloud yunqi】
brief introduction : Can we get around http agreement , Directly test the performance of the database ? Do you feel like exporting from the database CSV It is very troublesome to construct the pressure measurement data by file ? How to clean up data after pressure measurement ? Can I insert into the database ( Delete ) Record assertions made to pressure test requests ? Use alicloud performance testing tools PTS The above problems can be easily solved .
author : Huangyandi
Preface
Can we get around http agreement , Directly test the performance of the database ? Do you feel like exporting from the database CSV It is very troublesome to construct the pressure measurement data by file ? How to clean up data after pressure measurement ? Can I insert into the database ( Delete ) Record assertions made to pressure test requests ? Use alicloud performance testing tools PTS The above problems can be easily solved .
What is? JDBC
JDBC(Java DataBase Connectivity,Java Database connection ) It's one for execution SQL Of the statement Java API, It can provide unified access for a variety of relational databases , It's used by a group Java Language written classes and interfaces .JDBC It provides a benchmark , From this, you can build more advanced tools and interfaces , Enable database developers to write database applications .
In short ,JDBC Do three things : Establish a connection to the database 、 Send the statement to operate the database and process the result .
JDBC Design principle of
The overall architecture
JDBC Developed a set of standards to interact with the database , The database manufacturer provides the implementation of this set of standards , In this way, we can pass the unified JDBC Interface to connect various databases . so to speak JDBC The function of is to shield the differences between the underlying databases , Make the user follow JDBC The code can be executed on various databases . So how does this come true ? As shown in the figure below :
JDBC Defined Driver Interface , This interface is the database driver , All operations dealing with the database will come down to this in the end , The database manufacturer must implement this interface , This interface is used to complete the interaction between the caller of the upper application and the underlying concrete database .Driver It's through JDBC Provided DriverManager To register , The registration code is written in Driver In the static block of , Such as MySQL The registration code of is as follows :
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
As a specification for driving definitions Driver, Its main purpose is to establish a connection with the database , So its interface is also very simple , As shown below :
public interface Driver {
// Establishing a connection
Connection connect(String url, java.util.Properties info)
throws SQLException;
boolean acceptsURL(String url) throws SQLException;
DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info)
throws SQLException;
int getMajorVersion();
int getMinorVersion();
boolean jdbcCompliant();
public Logger getParentLogger() throws SQLFeatureNotSupportedException;
}
As Driver The manager of DriverManager, It is not only responsible for Driver Registration of / Cancellation , You can also get the connection directly . How does it do it ? Look at the following code and find , Actually, it has been registered through traversal Driver, Find one that can successfully establish a connection Driver, And will Connection return ,DriverManager Like an agent , The process of establishing the real connection is still left to the specific Driver.
for(DriverInfo aDriver : registeredDrivers) {
// If the caller does not have permission to load the driver then
// skip it.
if(isDriverAllowed(aDriver.driver, callerCL)) {
try {
println(" trying " + aDriver.driver.getClass().getName());
Connection con = aDriver.driver.connect(url, info);
if (con != null) {
// Success!
println("getConnection returning " + aDriver.driver.getClass().getName());
return (con);
}
} catch (SQLException ex) {
if (reason == null) {
reason = ex;
}
}
} else {
println(" skipping: " + aDriver.getClass().getName());
}
}
Connection Design
From the previous section, we know that the database provider implements Driver Interface to provide services to users ,Driver The core method of the interface is to obtain the connection .Connection It is the core interface for dealing with databases , Let's take a look at its design .
By looking at the design drawings, we find that there are mainly two types of interfaces :DataSource and Connection. Let's introduce them one by one .
- DataSource
Look at the source code directly , As shown below , The core method of discovery is Driver equally , It is also to obtain the connection . Then why do we have to DataSource Well ?Driver Isn't it the one that gets the connection ? Let's take a look DataSource How to get the connection .
public interface DataSource extends CommonDataSource, Wrapper {
Connection getConnection() throws SQLException;
Connection getConnection(String username, String password)
throws SQLException;
}
But we found that JDBC It only defines DataSource The interface of , There is no specific implementation , So let's do that Spring Realized SimpleDriverDataSource For example , Let's see how it's done , The code is as follows , Find out DataSource Of getConnection(...) Method , In the end, it was left to driver.connect(...) To really make a connection . So back to what we described at the beginning , Driver Is the real interface to deal with the database .
protected Connection getConnectionFromDriver(Properties props) throws SQLException {
Driver driver = getDriver();
String url = getUrl();
Assert.notNull(driver, "Driver must not be null");
if (logger.isDebugEnabled()) {
logger.debug("Creating new JDBC Driver Connection to [" + url + "]");
}
return driver.connect(url, props);
}
So here comes the question , Why do we still need DataSource Such an interface , Why don't you kill me with one stone ? Obviously not. .DataSource It's an enhanced version Driver. It leaves the core process of establishing connections to Driver perform , For creating a cache , Handle distributed transactions, connection pools, and other things that seem unrelated to establishing connections . As shown in the design diagram of the class , With PTS The use of Druid Take the connection pool as an example :
ConnectionPoolDataSource: Implementation of connection pool , This data source implementation does not directly create a physical connection to the database , It's a logical implementation , Its role is to pool database physical connections .
PooledConnection: coordination ConnectionPoolDataSource, It gets a pooled object PooledConnection, And then through the PooledConnection Get the physical connection indirectly .
obviously , Through connection pooling, we can get away from the management of connections , Improve connection utilization efficiency , It can also improve the pressure capacity of the press .
Statement Design
Once the connection is established , Users may want to start writing SQL sentence , And it was handed over to the database for execution . These are through Statement To achieve . It is mainly divided into :
Statement: Define a static SQL sentence , The database needs to be recompiled every time it is executed , It is generally used when the query is executed only once and the result is returned .
PreparedStatement: Define a precompiled with parameters SQL sentence , Next execution , It will fetch the statements after times from the cache , Without having to recompile , It is applicable to..., which executes the same logic multiple times SQL sentence , Of course, it can also prevent SQL Injection and other functions , High security and efficiency , More frequently used . For performance testing , choice PreparedStatement Most suitable .
CallableStatement: Used to call stored procedures .
ResultSet Design
JDBC Use ResultSet Interface to undertake Statement The results of the implementation of .ResultSet How to use the pointer (next()) To get the search results one by one , When the pointer points to a piece of data , Users can freely choose to obtain the data of a certain column .PTS By way of ResultSet Turn it into CSV file , Assist the user with a SQL sentence , Construct complex pressure measurement data .
JDBC Architecture summary
Through the above introduction, we find that ,JDBC The design of is still hierarchical .
(1)Driver and DriverManager It is database oriented , Designed a set of Java Specifications for accessing data , Database vendors only need to implement this set of specifications ;
(2)DataSource and Connection For application developers , They don't care JDBC How to interact with the database , Through unified DataSource You can get the interface Connection, Users' data operations can be performed through this Connection To implement the ;
(3)Statement It carries the concrete SQL command , Users can define different Statement To send instructions to the database ;
(4)ResultSet Is used to carry SQL Command execution results .
thus , It's done The load driver -> Establishing a connection -> Carry out orders -> Return results The whole process of interacting with the database . If this process is flexibly embedded in PTS In the performance test , The problems mentioned in the preface can be solved .
JDBC Application in performance testing
Database performance test
background
Most operations on databases are done through HTTP、FTP Or other agreements , But in some cases , It is also meaningful to test the database directly without intermediate protocols . For example, we want not to trigger all related queries , And only test specific high-value Query performance ; Verify the performance of the new database under high load .2. Verify some database connection pool parameters , For example, the maximum number of connections 3. Save time and resources . When we want to optimize SQL when , Modify the SQL Statements and other database operations are tedious , adopt JDBC Pressure measurement , We can avoid hacking code , Focus on SQL Tuning .
step
1、 Create a scene . We are PTS Console 【 Pressure measuring center 】->【 Create a scene 】 Created in PTS Pressure test scenario ;
2、 Scene configuration .PTS Support for MySQL、PostgreSQL And other four databases to initiate pressure test . The user fills in JDBC URL、 user name 、 Passwords and SQL Pressure measurement can be initiated . meanwhile ,PTS It also supports extraction ResultSet The data in is used as the output parameter , To the downstream API Use ; Assert the response .
3、 Monitoring and pressure test report during pressure test .PTS It supports binding to Alibaba cloud RDS Cloud resource monitoring , Observe during pressure measurement RDS Real time performance index . Besides ,PTS Clear and complete pressure test reports and sampling logs are also provided , For users to view at any time .
Pressure measurement data structure
background
Simulate different user login 、 In scenarios such as pressure test business parameter transmission , It is necessary to use the parameter function to realize various dynamic operations in the pressure measurement request . If traditional CSV File parameters , Will be limited by file size , And manual creation takes effort . Use JDBC To construct pressure measurement data , Can avoid the above problems .
step
1、 Add data sources . Edit in the scene - Data source management , Select Add DB data source , Input URL、 user name 、 Passwords and SQL.
2、 Add parameter . Fill in the custom parameter name and column index .
3、 Debugging verification . Click debug scenario , You can verify whether the extracted result set meets the expectations . next , We can use any
Place to use ${} Just quote .
Clean the dirty data of pressure measurement
background
Pressure test for write request , A lot of dirty data will be generated in the database . How to automatically clean up after pressure measurement ?
step
PTS Provides users with solutions .PTS It supports logical sequencing of serial links , That is, the front link 、 Common link and post link . The order of execution is from first to last . Set a serial link as a post link , Fill in the number of cycles .
More exchanges , Welcome to the nail group to communicate ,PTS User AC nail group number :11774967.
Besides ,PTS Recently, the sales method has been upgraded , The price of the basic version has dropped 50%!5W Concurrent pricing is just 199, Eliminate the trouble of self operation and maintenance pressure test platform ! More new users 0.99 Experience version 、VPC Exclusive version of pressure measurement , Welcome to buy !
Link to the original text :click.aliyun.com/m/100034773…
This article is the original content of Alibaba cloud , No reprint without permission .
边栏推荐
- 企业中台规划和IT架构微服务转型
- c语言数组截取,C# 字符串按数组截取方法(C/S)
- pycharm从安装到全副武装,学起来才嗖嗖的快,图片超多,因为过度详细!
- 太湖 “中国健康农产品·手机直播万里行”走进太湖
- CADD课程学习(1)-- 药物设计基础知识
- arthas调试 确定问题工具包
- 计网 | 【五 传输层、六 应用层】知识点及例题
- Convert seconds to * * hours * * minutes
- Buttons to achieve various effects and functions. Reading this article is enough
- Simple usage of LinkedList (2022.6.13-6.19)
猜你喜欢

TorchDrug--药物属性预测

Primary school, session 3 - afternoon: Web_ sessionlfi

WordPress 博客使用火山引擎 veImageX 进行静态资源 CDN 加速(免费)

超视频时代的音视频架构建设|Science和英特尔联袂推出“架构师成长计划”第二季

实现各种效果和功能的按钮,读这篇文章就够了

盘点华为云GaussDB(for Redis)六大秒级能力

盘点华为云GaussDB(for Redis)六大秒级能力

Friends in Guangzhou can join us if they have the opportunity

线上线下双结合,VR全景是家具线上转型好方法!

软件工程最佳实践——项目需求分析
随机推荐
Convert seconds to * * hours * * minutes
《微信小程序-基础篇》带你了解小程序中的生命周期(二)
MySQL数据库查询优化
8 - function
c语言数组截取,C# 字符串按数组截取方法(C/S)
Spark - 一文搞懂 Partitioner
闲鱼难“翻身”
TorchDrug--药物属性预测
The prospectus of pelt medical was "invalid" for the second time in the Hong Kong stock exchange, and the listing plan was substantially delayed
IT外包驻场人员怎么定位自己的痛点?
当我们在看待产业互联网的时候,总是会站在消费互联网的对立面来看待它
Solution to rollback of MySQL database by mistake deletion
1. 爬虫之Beautifulsoup解析库&在线解析图片验证码
实现各种效果和功能的按钮,读这篇文章就够了
VR全景添加对比功能,让差异化效果展示更直观!
传输层 使用滑动窗口实现流量控制
线上线下双结合,VR全景是家具线上转型好方法!
微信小程序开发实战 云音乐
“更福特、更中国”拨云见日,长安福特王牌产品订单过万
达梦数据库重新初始化实例操作记录