当前位置:网站首页>Application of JDBC in performance test
Application of JDBC in performance test
2022-06-23 20:54:00 【Alibaba cloud native】
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 !
边栏推荐
- Advantages of token mechanism over cookie mechanism
- From different angles, you can learn about the implementation of sliding list in fluent
- 徽商期货交易软件正规吗?如何安全下载?
- Crise de 35 ans? Le volume intérieur est devenu synonyme de programmeur...
- How to convert []byte to io. in go Reader?
- The background receives the post data passed by the fetch
- Model selection and optimization
- Row height, (top line, middle line, baseline, bottom line), vertical align
- Newbeecoder. UI new open source control library DataGrid instructions
- 实现vscode写markdown文档+图片自动上传至腾讯云cos
猜你喜欢

Tupu software digital twin intelligent water service, breaking through the development dilemma of sponge City

SQL聯合查詢(內聯、左聯、右聯、全聯)的語法

Applet development framework recommendation

重庆 奉节耀奎塔,建成后当地连中五名进士,是川江航运的安全塔

UGeek大咖说 | 可观测之超融合存储系统的应用与设计

JS高级程序设计第 4 版:生成器的学习

LeetCode 260. Number III that appears only once
Implementing MySQL fuzzy search with node and express

Implementation of microblog system based on SSM

Can the biggest gamefi crash victim survive the bear market in May| May Monthly Report
随机推荐
Process injection
【Golang】快速复习指南QuickReview(十)——goroutine池
Is it safe for flush to open an account online? Is the Commission high
Application of MySQL time function, simple problem
JS advanced programming version 4: generator learning
Tcp/udp Fundamentals
【Golang】快速复习指南QuickReview(三)——map
[golang] quick review guide quickreview (VIII) -- goroutine
【Golang】快速复习指南QuickReview(七)——interface
ntpupdate. tencentyun. Com has been eliminated
JS naming conventions
. Net cloud native architect training camp (rgca four step architecture method) -- learning notes
How to build a cloud game platform on the server? How to select a cloud game server?
Use of the vs2022scanf function. An error is reported when using scanf - the return value is ignored: Solutions
JS five methods to judge whether a certain value exists in an array
【Golang】快速复习指南QuickReview(八)——goroutine
Short video intelligent audit software intelligent audit or manual audit
【Golang】深究字符串——从byte rune string到Unicode与UTF-8
Memory patch amsi bypass
How to log in to the server through the fortress machine to transfer files? What are the specific steps?