Advertisement
Guest User

Untitled

a guest
Nov 7th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.46 KB | None | 0 0
  1. import java.io.*;
  2. import java.sql.*;
  3.  
  4.  
  5. import java.util.Random;
  6.  
  7. public class Main extends Thread {
  8.  
  9. private static volatile boolean acabou;
  10. private static int n = 0;
  11. private static long trtot = 0;
  12. private volatile static int invoiceId = 0;
  13.  
  14. private static synchronized void op(long tr) {
  15. if (acabou)
  16. return;
  17. n++;
  18. trtot += tr;
  19. }
  20.  
  21. private final Connection c;
  22. private final Statement s;
  23. private Random r = new Random();
  24.  
  25. public Main() throws Exception {
  26. c = DriverManager.getConnection("jdbc:postgresql://localhost/mydb");
  27. c.setAutoCommit(false);
  28. s = c.createStatement();
  29. }
  30.  
  31.  
  32. public void run() {
  33. int max = 1024;
  34.  
  35. try {
  36. c.setSavepoint();
  37. } catch (SQLException e) {
  38. e.printStackTrace();
  39. }
  40.  
  41. for (int i = 0; i < max; i++) {
  42. try {
  43. int maxStock = randRange(25, 50);
  44. s.executeUpdate("insert into client (id, name, address) values (" + i + ", 'Cliente de numero " + i + "', 'Endereço aleatorio "
  45. + i + "')");
  46. s.executeUpdate("insert into product (id, description, stock, stockMin, stockMax) values (" + i + ", 'Produto de numero" + i + "', " + maxStock + "," + randRange(5, 20) + ","
  47. + maxStock + ")");
  48. c.commit();
  49. } catch (SQLException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53.  
  54.  
  55. try {
  56. int invIdInsert = 0;
  57. while (!acabou) {
  58. long antes = System.nanoTime();
  59. switch (r.nextInt(1)) {
  60. case 0:
  61.  
  62. int numItems = r.nextInt(5) + 1;
  63. int clientId = rand(max);
  64. int numeroFatura = 0;
  65.  
  66. synchronized (this) {
  67. numeroFatura = invoiceId;
  68. invoiceId++;
  69. }
  70.  
  71. s.executeUpdate("insert into invoice values (" + numeroFatura + "," + clientId + ")");
  72.  
  73. for (int item = 0; item < numItems; item++) {
  74. int productId = rand(max);
  75. int invoiceId = rand(max);
  76. int supplierId = rand(max);
  77.  
  78. ResultSet rs = s.executeQuery("select stock, stockMin, stockMax from product p where p.id = " + productId);
  79.  
  80. rs.next();
  81.  
  82. int currentStock = rs.getInt(1);
  83. int minStock = rs.getInt(2);
  84. int maxStock = rs.getInt(3);
  85.  
  86. System.out.println("Currenstock: " + currentStock + "\t minStock: " + minStock + "\t maxStock: " + maxStock);
  87.  
  88. }
  89.  
  90. System.out.println("Inserting!");
  91. break;
  92.  
  93. case 1:
  94. // List names of products sold to some client
  95. int pid = rand(1024);
  96. int cid = rand(1024);
  97.  
  98. ResultSet rs = s.executeQuery("select p.description from product p, client c, invoice i" +
  99. " where p.id = i.productId and i.clientId = c.id and p.id = " + pid + " " +
  100. " and c.id = " + cid);
  101. while (rs.next()) {
  102. System.out.println(rs.getString(1));
  103. }
  104. rs.close();
  105. break;
  106.  
  107. case 2: // TOP 10, sem materialized view
  108. rs = s.executeQuery("select count(*) as quant, productId from invoice group by productId order by" +
  109. " quant desc limit 10"); // top 10
  110. while (rs.next()) {
  111. System.out.println(rs.getInt(1) + ", " + rs.getInt(2));
  112. }
  113. rs.close();
  114. break;
  115.  
  116. case 3: // ORDER
  117. rs = s.executeQuery("select id, stock, stockMax from product p where p.stock < p.stockMin");
  118. while (rs.next()) {
  119. int productId = rs.getInt(1);
  120. int currentStock = rs.getInt(2);
  121. int stockMax = rs.getInt(4);
  122.  
  123. c.setSavepoint();
  124. s.executeUpdate("insert into orderStock(productId, supplierId, itemQuantity) values (" + productId + ","
  125. + randRange(0, 50) + "," + (stockMax - currentStock) + ")");
  126. c.commit();
  127. }
  128.  
  129. case 4: // DELIVERY
  130. rs = s.executeQuery("select id, productId, supplierId, itemQuantity from orderStock order by random() limit 5");
  131.  
  132. while (rs.next()) {
  133. int orderId = rs.getInt(1);
  134. int productId = rs.getInt(2);
  135. int itemQuantity = rs.getInt(4);
  136.  
  137. ResultSet rsStock = s.executeQuery("select stock from product p where productId = " + productId);
  138. rsStock.next();
  139. int currentStock = rs.getInt(1);
  140.  
  141. s.executeUpdate("update product p set p.stock = " + (currentStock + itemQuantity) + " where p.id = " + productId);
  142. s.executeUpdate("delete from orderStock os where os.id = " + orderId);
  143. }
  144. break;
  145. }
  146.  
  147. long tr = System.nanoTime() - antes;
  148. op(tr);
  149. }
  150.  
  151. } catch (Exception e) {
  152. e.printStackTrace();
  153. }
  154. }
  155.  
  156. public int rand(int val) {
  157. return r.nextInt(val) | r.nextInt(val);
  158. }
  159.  
  160. public static int randRange(int min, int max) {
  161.  
  162. if (min >= max) {
  163. throw new IllegalArgumentException("max must be greater than min");
  164. }
  165.  
  166. return (int) (Math.random() * ((max - min) + 1)) + min;
  167. }
  168.  
  169.  
  170. public void createTables(boolean useIndex) throws Exception {
  171. s.executeUpdate("drop table if exists client, product, invoice, invoiceLine, orderStock");
  172. s.executeUpdate("create table client (id int, name varchar, address varchar, PRIMARY KEY(id))");
  173. s.executeUpdate("create table product (id int, description varchar, stock int, stockMin int, stockMax int, PRIMARY KEY(id))");
  174. s.executeUpdate("create table invoice (id SERIAL, clientId int)");
  175. s.executeUpdate("create table invoiceLine (id SERIAL, invoiceId int, productId int)");
  176. s.executeUpdate("create table orderStock (id SERIAL, productId int, supplierId int, itemQuantity int)");
  177. c.commit();
  178. }
  179.  
  180. public static void main(String[] args) throws Exception {
  181.  
  182. Main m = new Main();
  183.  
  184. File file = new File("/home/mbrito/mei/BDA/dados_lab_2.csv");
  185. if (!file.exists())
  186. file.createNewFile();
  187.  
  188. FileWriter fileWriter = new FileWriter(file, true);
  189. BufferedWriter bw = new BufferedWriter(fileWriter);
  190. bw.write("Threads, Debito, Tempo de Resposta\n");
  191.  
  192.  
  193. for (int numThreads = 1; numThreads <= 1; numThreads *= 2) {
  194. m.createTables(true);
  195. System.out.println("Creating table!");
  196. Main[] t = new Main[numThreads];
  197.  
  198. for (int i = 0; i < t.length; i++)
  199. t[i] = new Main();
  200.  
  201. for (int i = 0; i < t.length; i++)
  202. t[i].start();
  203.  
  204. int d = 45;
  205.  
  206.  
  207. Thread.sleep(d * 1000);
  208.  
  209. acabou = true;
  210.  
  211. for (int i = 0; i < t.length; i++)
  212. t[i].join();
  213.  
  214. double debito = (n / (double) d);
  215. double tr = (trtot / (1e9 * n));
  216. System.out.println("debito = " + (n / (double) d));
  217. System.out.println("tr = " + (trtot / (1e9 * n)));
  218.  
  219. bw.write(numThreads + ", " + debito + ", " + tr + "\n");
  220. System.out.println("Finishing cycle of threads " + numThreads);
  221.  
  222. n = 0;
  223. trtot = 0;
  224. acabou = false;
  225. }
  226.  
  227. bw.close();
  228. }
  229.  
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement