Guest User

Untitled

a guest
Dec 20th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. import java.sql.Array;
  2. import java.sql.CallableStatement;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.Statement;
  6. import java.sql.Types;
  7. import java.util.Properties;
  8. import java.util.stream.LongStream;
  9. import java.util.stream.Stream;
  10.  
  11. public class Oracle {
  12.  
  13. static String lastMessage;
  14.  
  15. public static void main(String[] args) {
  16. try {
  17. Class.forName("oracle.jdbc.OracleDriver");
  18.  
  19. String url = "jdbc:oracle:thin:@192.168.99.100:1521:ORCLCDB";
  20. String user = "TEST";
  21. String password = "TEST";
  22.  
  23. Properties properties = new Properties();
  24. properties.setProperty("user", user);
  25. properties.setProperty("password", password);
  26.  
  27. int max = 50;
  28. long[] getLines1 = new long[max];
  29. long[] getLines10 = new long[max];
  30. long[] getLines1000 = new long[max];
  31. long[] getLine = new long[max];
  32.  
  33. try (Connection c = DriverManager.getConnection(url, properties);
  34. Statement s = c.createStatement()) {
  35.  
  36. for (int warmup = 0; warmup < 2; warmup++) {
  37. for (int i = 0; i < max; i++) {
  38. s.executeUpdate("begin dbms_output.enable(); end;");
  39. String sql = "begin for i in 1 .. 100 loop dbms_output.put_line('Message ' || i); end loop; end;";
  40. long t1 = System.nanoTime();
  41. logGetLines(c, 100, 1, () -> s.executeUpdate(sql));
  42. long t2 = System.nanoTime();
  43. logGetLines(c, 100, 10, () -> s.executeUpdate(sql));
  44. long t3 = System.nanoTime();
  45. logGetLines(c, 100, 1000, () -> s.executeUpdate(sql));
  46. long t4 = System.nanoTime();
  47. logGetLine(c, 100, () -> s.executeUpdate(sql));
  48. long t5 = System.nanoTime();
  49. s.executeUpdate("begin dbms_output.disable(); end;");
  50.  
  51. if (warmup > 0) {
  52. getLines1[i] = t2 - t1;
  53. getLines10[i] = t3 - t2;
  54. getLines1000[i] = t4 - t3;
  55. getLine[i] = t5 - t4;
  56. }
  57. }
  58. }
  59. }
  60.  
  61. // LongSummaryStatistics{count=50, sum=69120455, min=1067521, average=1382409.100000, max=2454614}
  62. // LongSummaryStatistics{count=50, sum=75671496, min=1127253, average=1513429.920000, max=2843735}
  63. // LongSummaryStatistics{count=50, sum=76199718, min=1142187, average=1523994.360000, max=3054935}
  64. // LongSummaryStatistics{count=50, sum=2088201423, min=33737827, average=41764028.460000, max=64498375}
  65.  
  66.  
  67. System.out.println(LongStream.of(getLines1).summaryStatistics());
  68. System.out.println(LongStream.of(getLines10).summaryStatistics());
  69. System.out.println(LongStream.of(getLines1000).summaryStatistics());
  70. System.out.println(LongStream.of(getLine).summaryStatistics());
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74. }
  75.  
  76. interface WhyUNoCheckedExceptionRunnable {
  77. void run() throws Exception;
  78. }
  79.  
  80. static void logGetLines(Connection connection, int size, int fetchSize, WhyUNoCheckedExceptionRunnable runnable) throws Exception {
  81. try (Statement s = connection.createStatement()) {
  82. runnable.run();
  83.  
  84. try (CallableStatement call = connection.prepareCall(
  85. "declare "
  86. + " num integer := ?;"
  87. + "begin "
  88. + " dbms_output.get_lines(?, num);"
  89. + "end;"
  90. )) {
  91. call.setFetchSize(fetchSize);
  92. call.setInt(1, size);
  93. call.registerOutParameter(2, Types.ARRAY, "DBMSOUTPUT_LINESARRAY");
  94. call.execute();
  95.  
  96. Array array = null;
  97. try {
  98. array = call.getArray(2);
  99. Stream.of((Object[]) array.getArray())
  100. .forEach(m -> { lastMessage = (String) m; });
  101. }
  102. finally {
  103. if (array != null)
  104. array.free();
  105. }
  106. }
  107. }
  108. }
  109.  
  110. static void logGetLine(Connection connection, int size, WhyUNoCheckedExceptionRunnable runnable) throws Exception {
  111. try (Statement s = connection.createStatement()) {
  112. runnable.run();
  113.  
  114. for (int i = 0; i < size; i++) {
  115. try (CallableStatement call = connection.prepareCall(
  116. "begin "
  117. + " dbms_output.get_line(?, ?);"
  118. + "end;"
  119. )) {
  120. call.registerOutParameter(1, Types.VARCHAR);
  121. call.registerOutParameter(2, Types.INTEGER);
  122. call.execute();
  123. lastMessage = call.getString(1);
  124. }
  125. }
  126. }
  127. }
  128. }
Add Comment
Please, Sign In to add comment