GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — sql-benchmark/java/SQLBenchmark.java at master · go-sql-driver/sql-benchmark Expand file tree/ Copy pathSQLBenchmark.javaMore file actions 204 lines (184 loc) · 6.41 KB / Copy pathSQLBenchmark.javaFile metadata and controls204 lines (184 loc) · 6.41 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * * @author Julien Schmidt <mail at julienschmidt.com> */ public class SQLBenchmark { private static void printResult(String name, long ms, long bytes, int i) { System.out.println( name + ": " + (ms / 1000.0) + "s \t " + Math.round(1000.0 * i / ms) + " queries/second \t" /* Does someone know how to get useable values? * If yes, please make a pull-request to fix this! */ //(bytes/i) + " B/query" ); } /** * @param args the command line arguments */ public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (Exception ex) { System.err.println("Loading MySQL-Driver failed!"); } Connection conn = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/gotest?user=root&password=root"); } catch (SQLException ex) { if(conn != null) { try { conn.close(); } catch(Exception iDontCare) { } finally { conn = null; } } System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); System.exit(1); } Statement stmt = null; ResultSet rs = null; Runtime runtime = Runtime.getRuntime(); try { long startTime, endTime; long startTotalMem; PreparedStatement pStmt; int num; stmt = conn.createStatement(); stmt.setPoolable(true); // SimpleExec startTotalMem = runtime.totalMemory()-runtime.freeMemory(); startTime = System.currentTimeMillis(); for(int i = 0; i < 500000; i++) { stmt.execute("DO 1"); } endTime = System.currentTimeMillis(); SQLBenchmark.printResult( "SimpleExec", (endTime-startTime), (runtime.totalMemory()-runtime.freeMemory()-startTotalMem), 500000 ); // PreparedExec startTotalMem = runtime.totalMemory()-runtime.freeMemory(); startTime = System.currentTimeMillis(); pStmt = conn.prepareStatement("DO 1"); for(int i = 0; i < 500000; i++) { pStmt.execute(); } pStmt.close(); endTime = System.currentTimeMillis(); SQLBenchmark.printResult( "PreparedExec", (endTime-startTime), (runtime.totalMemory()-runtime.freeMemory()-startTotalMem), 500000 ); // SimpleQueryRow startTotalMem = runtime.totalMemory()-runtime.freeMemory(); startTime = System.currentTimeMillis(); for(int i = 0; i < 500000; i++) { if (stmt.execute("SELECT 1")) { rs = stmt.getResultSet(); if(rs.next()) { num = rs.getInt(1); } else { throw new Exception("No result"); } } else { throw new Exception("No result"); } } endTime = System.currentTimeMillis(); SQLBenchmark.printResult( "SimpleQueryRow", (endTime-startTime), (runtime.totalMemory()-runtime.freeMemory()-startTotalMem), 500000 ); // PreparedQueryRow startTotalMem = runtime.totalMemory()-runtime.freeMemory(); startTime = System.currentTimeMillis(); pStmt = conn.prepareStatement("SELECT 1"); for(int i = 0; i < 500000; i++) { if (pStmt.execute()) { rs = pStmt.getResultSet(); if(rs.next()) { num = rs.getInt(1); } else { throw new Exception("No result"); } } else { throw new Exception("No result"); } } pStmt.close(); endTime = System.currentTimeMillis(); SQLBenchmark.printResult( "PreparedQueryRow", (endTime-startTime), (runtime.totalMemory()-runtime.freeMemory()-startTotalMem), 500000 ); // PreparedQueryRowParam startTotalMem = runtime.totalMemory()-runtime.freeMemory(); startTime = System.currentTimeMillis(); pStmt = conn.prepareStatement("SELECT ?"); for(int i = 0; i < 500000; i++) { pStmt.setInt(1, i); if (pStmt.execute()) { rs = pStmt.getResultSet(); if(rs.next()) { num = rs.getInt(1); } else { throw new Exception("No result"); } } else { throw new Exception("No result"); } } pStmt.close(); endTime = System.currentTimeMillis(); SQLBenchmark.printResult( "PreparedQueryRowParam", (endTime-startTime), (runtime.totalMemory()-runtime.freeMemory()-startTotalMem), 500000 ); } catch (Exception e) { System.err.println(e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException iDontCare) { } finally { rs = null; } } if (stmt != null) { try { stmt.close(); } catch (SQLException iDontCare) { } finally { stmt = null; } } try { conn.close(); } catch(Exception iDontCare) { } finally { conn = null; } } } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |