TestMain.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package analyzer.tests;
  2. import analyzer.Analyzer;
  3. import analyzer.ClientLogParser;
  4. import analyzer.ServerLogParser;
  5. import analyzer.models.Client;
  6. import analyzer.models.Hashtag;
  7. import java.io.BufferedReader;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Scanner;
  14. import java.util.concurrent.ExecutionException;
  15. import java.util.concurrent.ExecutorService;
  16. import java.util.concurrent.Executors;
  17. import java.util.concurrent.Future;
  18. public class TestMain {
  19. private static final int threadPoolSize = 8;
  20. private static volatile int error = 0;
  21. private static volatile double sum = 0.0;
  22. private static volatile int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
  23. private static volatile int correct = 0;
  24. private static volatile double avgSetSize = 0.0;
  25. private static Analyzer A;
  26. static List<Client> group1 = new ArrayList<>(); //>1000 messages
  27. static List<Client> group2 = new ArrayList<>(); //100-1000 messages
  28. static List<Client> group3 = new ArrayList<>(); //10-100 messages
  29. public static void main(String[] args) throws InterruptedException, ExecutionException {
  30. A = new Analyzer();
  31. determineUserGroups();
  32. testMethod1();
  33. testMethod2();
  34. }
  35. public static void determineUserGroups() {
  36. int numOfClients = ClientLogParser.clients.values().size();
  37. ClientLogParser.clients.values().forEach(c -> {
  38. if(c.getTotalPosts() >= 10 && c.getTotalPosts() < 100)
  39. group3.add(c);
  40. else if(c.getTotalPosts() >= 100 && c.getTotalPosts() < 1000)
  41. group2.add(c);
  42. else if(c.getTotalPosts() >= 1000)
  43. group1.add(c);
  44. });
  45. System.out.println(">= 1000: " + (double) group1.size()*100/numOfClients + "%" + " (" + group1.size() + " users)");
  46. System.out.println("100-1000: " + (double) group2.size()*100/numOfClients + "%" + " (" + group2.size() + " users)");
  47. System.out.println("10-100: " + (double) group3.size()*100/numOfClients + "%" + " (" + group3.size() + " users)");
  48. System.out.println();
  49. }
  50. public static void testMethod1() throws ExecutionException, InterruptedException {
  51. System.out.println("Method 1 on Group ");
  52. System.out.print(">= 1000: ");
  53. testMethodOneOnGroup(group1);
  54. System.out.println();
  55. System.out.print("100-1000: ");
  56. testMethodOneOnGroup(group2);
  57. System.out.println();
  58. System.out.print("10-100: ");
  59. testMethodOneOnGroup(group3);
  60. System.out.println();
  61. }
  62. private static void testMethodOneOnGroup(List<Client> group) throws ExecutionException, InterruptedException {
  63. int counter = 0;
  64. int correct;
  65. double averageGroup;
  66. System.out.println("clientList size " + group.size());
  67. ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
  68. List<Future<?>> tasks = new ArrayList<>();
  69. for (Client client : group) {
  70. Future<?> task = executor.submit(new ExcludeUsersTask(client));
  71. tasks.add(task);
  72. }
  73. for(Future<?> task : tasks){
  74. task.get();
  75. counter++;
  76. float progress = (float) Math.round(((float) counter / (float) group.size()) * 1000.0F) / 10.0F;
  77. System.out.print("\r");
  78. System.out.print(progress + " % done");
  79. }
  80. executor.shutdown();
  81. correct = group.size() - error;
  82. averageGroup = sum /correct;
  83. System.out.println(" correct: " + correct + " \naverage no of rounds: " + averageGroup);
  84. System.out.println("Min: " + min + "\nAvg: " + averageGroup + "\nMax: " + max);
  85. }
  86. public static void testMethod2() throws ExecutionException, InterruptedException {
  87. System.out.println("Method 2 on Group ");
  88. System.out.print(">= 1000: ");
  89. testMethodTwoOnGroup(group1);
  90. System.out.println();
  91. System.out.print("100-1000: ");
  92. testMethodTwoOnGroup(group2);
  93. System.out.println();
  94. System.out.print("10-100: ");
  95. testMethodTwoOnGroup(group3);
  96. System.out.println();
  97. }
  98. private static void testMethodTwoOnGroup(List<Client> group) throws ExecutionException, InterruptedException {
  99. avgSetSize = 0.0;
  100. correct = 0;
  101. int counter = 0;
  102. double averageGroup;
  103. System.out.println("clientList size " + group.size());
  104. ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
  105. List<Future<?>> tasks = new ArrayList<>();
  106. for (Client client : group) {
  107. Future<?> task = executor.submit(new RankTopicsTask(client));
  108. tasks.add(task);
  109. }
  110. for(Future<?> task : tasks){
  111. task.get();
  112. counter++;
  113. float progress = (float) Math.round(((float) counter / (float) group.size()) * 1000.0F) / 10.0F;
  114. System.out.print("\r");
  115. System.out.print(progress + " % done");
  116. }
  117. averageGroup = avgSetSize / group.size();
  118. System.out.print(" correct: " + correct + " average set size: " + averageGroup);
  119. executor.shutdown();
  120. }
  121. private record ExcludeUsersTask(Client client) implements Runnable {
  122. private static synchronized void incrementError() {
  123. error++;
  124. }
  125. private static synchronized void incrementAvg(int num) {
  126. if(num > max)
  127. max = num;
  128. if(num < min)
  129. min = num;
  130. sum += num;
  131. }
  132. @Override
  133. public void run() {
  134. int rounds = A.intersectUsers(client, 1, false);
  135. if (rounds == -1) {
  136. incrementError();
  137. } else {
  138. incrementAvg(rounds);
  139. }
  140. }
  141. }
  142. private record RankTopicsTask(Client client) implements Runnable {
  143. public static final String boundnamesPath = Analyzer.logPath + "\\boundnames.txt" ;
  144. private static synchronized void incrementCorrect() {
  145. correct++;
  146. }
  147. private static synchronized void incrementAvgSetSize(int num) {
  148. avgSetSize += num;
  149. }
  150. @Override
  151. public void run() {
  152. List<Map.Entry<String, Integer>> intersection = A.intersectHashtags(client, 10, false);
  153. Hashtag[] hashtags = new Hashtag[intersection.size()];
  154. for(int i = 0; i < hashtags.length; i++)
  155. hashtags[i] = ServerLogParser.hashtags.get(intersection.get(i).getKey());
  156. List<Map.Entry<Hashtag, Integer>> results;
  157. try {
  158. results = A.calculatePointsGivenHashtags(client, hashtags, 1, false);
  159. incrementAvgSetSize(results.size());
  160. results.forEach(e -> {
  161. try {
  162. check(e.getKey());
  163. } catch (IOException ioException) {
  164. ioException.printStackTrace();
  165. }
  166. });
  167. } catch (InterruptedException | ExecutionException e) {
  168. e.printStackTrace();
  169. }
  170. }
  171. private void check(Hashtag hashtag) throws IOException {
  172. FileReader fr = new FileReader(boundnamesPath);
  173. BufferedReader br= new BufferedReader(fr);
  174. String line;
  175. while((line = br.readLine()) != null) {
  176. String[] elements = line.trim().split("\t");//userID client-ID
  177. if(elements[1].equals(client.getId())) {
  178. if(ServerLogParser.users.get(String.valueOf(elements[0])).getHashtags().containsKey(hashtag)) {
  179. br.close();
  180. incrementCorrect();
  181. }
  182. break;
  183. }
  184. }
  185. br.close();
  186. }
  187. }
  188. }