sselecta.java

来自「主要对各种数据库性能进行测试」· Java 代码 · 共 138 行

JAVA
138
字号
package sqlite;
 
import java.sql.ResultSet;
import java.sql.SQLException; 
import java.util.Random;

public class SselectA extends ServerFactory {
    
	ExecSql  exec = new ExecSql();;
	Configure config = new Configure();
	
	static int thenum = 0;
	static long max,min,total,ist;
	long avg; 
	
	String table = "users"; 
	WriteFile file = new WriteFile(); 
	String dbtype="";
	
	public SselectA() {
		this.init();
	}
	@Override
	public int Exec() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		try { 
			thenum ++;
			int randnum = Integer.parseInt(config.getProperty("randnum"));
			Random rand = new Random();
			int newval = thenum;
			int selectnum = Integer.parseInt(config.getProperty("selectnum"));	 
			String showprint = config.getProperty("showprint");
			
			long lstart = System.currentTimeMillis(); 
			if(selectnum > 0){
				int id = 0; 
				System.out.println("正在查询数据..."); 
				for(int i = 1 ;i <= selectnum;i++) {  
					id = rand.nextInt(randnum); 
					String sql = "select * from " + table + " where userid="+id; 
					ResultSet rs = exec.select(conn, sql);  
					if(showprint.equalsIgnoreCase("yes")){
						while(rs.next()){
							String in = i+":	"+rs.getInt(1)+"	"+rs.getString(2)+"	"+rs.getString(3);
							System.out.println(in);
							file.aLine(in);
						} 
					}  
					exec.closeResult(rs); 
					if(rs!=null){
						rs.close();   
					}  
					exec.closeStmt();  
				}   
			}
			 
			String in = "";
			 
			long lend = System.currentTimeMillis();
			long ltook = lend-lstart;
			int select_thread_num = 0;
			
			Integer.parseInt(config.getProperty("select_thread_num"));  
			select_thread_num = Integer.parseInt(config.getProperty("select_thread_num"));
			ist++;
			long iik = ist;
			if(ist==1){
				min = ltook;
			}
			if(max<ltook){
				max = ltook;
			}
			if(min>ltook){
				min = ltook;  
			}
			total += ltook;
			String ls = ""; 
			 
			System.out.println("curThreadID.isRandom="+curThreadID.isRandom);
			if (curThreadID.isRandom == 1 ){
				if (iik == curThreadID.selectNum){
					avg = total/iik;
					ls= "\n/**********************************************************************************************************************/\n";
					ls+="/***操作:查询数据,线程数:"+iik+",每个线程查询行数:"+selectnum+"," +
							"最小等待时间:"+min+"ms,最大等待时间:"+max+"ms,总共等待时间:"+total+"ms"+",平均等待时间:"+avg+"ms。***/\n";
					ls+="/**********************************************************************************************************************/\n";
				}
			}else{	
				if(iik == select_thread_num){
					avg = total/iik;
					ls= "\n/**********************************************************************************************************************/\n";
					ls+="/***操作:查询数据,线程数:"+iik+",每个线程查询行数:"+selectnum+"," +
							"最小等待时间:"+min+"ms,最大等待时间:"+max+"ms,总共等待时间:"+total+"ms"+",平均等待时间:"+avg+"ms。***/\n";
					ls+="/**********************************************************************************************************************/\n";
				}
			}
			in = "第"+newval+"个查询"+selectnum+"条数据,用时:"+(lend-lstart)+"ms,从"+lstart+"到"+lend;

			System.out.println( in + "\n" + ls);  
			file.aLine(in+"\n"+ls);
			  
			return 1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{ 
			freeConnection(this.dbtype);
			if(conn!=null) {
				exec.closeConn(conn); 
			}
			System.out.println("disconnect");
			file.aLine("disconnect");
		} 
		return 0;
	}

	@Override
	public int init() {
		// TODO Auto-generated method stub
		this.dbtype = config.getProperty("dbtype"); 
		if(!initConnection(this.dbtype)) {
			System.out.println("create database connection failed!");
			return -1;
		}
		return 0;
	}
	
	public static void main(String[] args) { 
		SselectA a = new SselectA();
		a.Exec();
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?