ngrinder 压测PostgreSQL数据库

ngrinder 压测PostgreSQL数据库

1.获取PostgreSQL的jdbc的jar包,postgresql-42.2.4.jre7

2.数据库配置其实和mysql类似的。请求的链接变为

String url = "jdbc:postgresql://localhost/testdb"; 

3.其他的请求配置都类似,这里不在啰嗦直接上代码

重要的部分我备注啦

import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.plugin.http.HTTPRequest
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.script.GTest
import net.grinder.script.Grinder
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import net.grinder.scriptengine.groovy.junit.annotation.AfterThread
// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import org.junit.Before
import org.junit.After

import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith

import java.util.Date
import java.util.List
import java.util.ArrayList

import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import java.sql.Connection
import java.sql.DriverManager 
import java.sql.ResultSet
import java.sql.SQLException 
import java.sql.Statement
//import java.util.String
import java.lang.String

/**
 * A simple example using the HTTP plugin that shows the retrieval of a
 * single page via HTTP. 
 * 
 * This script is automatically generated by ngrinder.
 * 
 * @author keny
 */
@RunWith(GrinderRunner)
class TestRunner {

    public static GTest test
    public static HTTPRequest request
     //定义链接
      Connection c = null
      Statement stmt = null

    @BeforeProcess
    public static void beforeProcess() {
        HTTPPluginControl.getConnectionDefaults().timeout = 6000
        //定义pgsql
        test = new GTest(1, "testpgsql")
        grinder.logger.info("before process.");
    }

    @BeforeThread 
    public void beforeThread() {
        test.record(this, "test")
        //启用线程链接connection
        getConnection()
        //grinder.statistics.delayReports=true
        grinder.logger.info("before thread.");
    }
    @AfterThread
    public void afterThread(){
    //退出链接池需要释放conn
             if (c!=null){
                 c.close()
                 
             }
        
    }
    
    @Before
    public void before() {
        grinder.logger.info("before thread. init headers and cookies");
    }
    public void getConnection(){
           Class.forName("org.postgresql.Driver")
         if (c == null){
         c = DriverManager
            .getConnection("jdbc:postgresql://XXXX:5432/XX",
            "XXX", "XXX");
         c.setAutoCommit(false);
         System.out.println("Opened database successfully")
         }
    
    
    }
    
    @Test
    public void test(){
     

      try {
         if (c!=null){
             stmt = c.createStatement();
             ResultSet rs = stmt.executeQuery( "SELECT id,name from test " );
             while ( rs.next() ) {
                int id = rs.getInt("id");
                String  name = rs.getString("name");
            
                System.out.println( "ID = " + id );
                //System.out.println( "NAME = " + name );
        
                System.out.println();
             }
             rs.close();
             if (stmt!=null){
                 stmt.close()
             }

        

        }
      } catch (Exception e) {
         System.err.println( e.getClass().getName()+": "+ e.getMessage() )
         System.exit(0)
      }
      //System.out.println("Records created successfully")
    
    
    }

扫描二维码关注公众号,回复: 2701759 查看本文章


    
    public void test1(){
        HTTPResponse result = request.GET("1", params)

        if (result.statusCode == 301 || result.statusCode == 302) {
            grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode); 
        } else {
            assertThat(result.statusCode, is(200));
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/keny88888/article/details/81081192