Guest User

Untitled

a guest
Dec 22nd, 2020
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.60 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.HashSet;
  3. import java.util.Map;
  4. import java.util.Random;
  5. import java.util.Set;
  6.  
  7. public class Main {
  8.  
  9.     static int NUMBER_DISTINCT_KEYS = 500_000_000;
  10.     static int STRING_SIZE_FACTOR = 1;
  11.     static int CHECKPOINT_INTERVAL = 1000;
  12.     static int METRICS_INTERVAL_MILLIS = 1000;
  13.  
  14.     static boolean FAST_FILL_MAP = false;
  15.  
  16.     static boolean OPTIMIZE_VALUE_ALLOCATION = false;
  17.  
  18.     static boolean CREATE_EXTRA_GARBAGE = false;
  19.     static int GARBAGE_PASSES_PER_LOOP = 1;
  20.     static int GARBAGE_BIN_SIZE = 2000;
  21.  
  22.     public static void main(String[] args) {
  23.         stress();
  24.     }
  25.  
  26.     private interface Mapper {
  27.         int updateStats(String key);
  28.         long size();
  29.     }
  30.  
  31.     private static class IntegerMapper implements Mapper {
  32.         Map<String, Integer> values = new HashMap<>();
  33.  
  34.         @Override
  35.         public int updateStats(String key) {
  36.             return values.compute(key, (k, i) -> i == null ? 1 : i + 1);
  37.         }
  38.  
  39.         @Override
  40.         public long size() {
  41.             return values.size();
  42.         }
  43.     }
  44.  
  45.     private static class MemoryOptimizedMapper implements Mapper {
  46.         Map<String, IntHolder> values = new HashMap<>();
  47.  
  48.         @Override
  49.         public int updateStats(String key) {
  50.             return values.compute(key, (k, h) -> h == null ? new IntHolder(1) : h.add()).accumulator;
  51.         }
  52.  
  53.         @Override
  54.         public long size() {
  55.             return values.size();
  56.         }
  57.  
  58.         private static class IntHolder {
  59.             public IntHolder(int accumulator) {
  60.                 this.accumulator = accumulator;
  61.             }
  62.  
  63.             int accumulator = 0;
  64.  
  65.             public IntHolder add() {
  66.                 accumulator++;
  67.                 return this;
  68.             }
  69.         }
  70.     }
  71.  
  72.     private static void stress() {
  73.         Mapper mapper = OPTIMIZE_VALUE_ALLOCATION ? new MemoryOptimizedMapper() : new IntegerMapper();
  74.  
  75.         int count = 0;
  76.         long start = System.currentTimeMillis();
  77.         long lastCheckpoint = start;
  78.         int lastCount = 0;
  79.         int largestValue = 0;
  80.  
  81.         Set<String> garbageBin = new HashSet<>();
  82.         long garbageBinSize = 0;
  83.  
  84.         while (true) {
  85.             String key = createKey(count);
  86.             int value = mapper.updateStats(key);
  87.             if (value > largestValue) {
  88.                 largestValue = value;
  89.             }
  90.  
  91.             // Create and eventually discard some extra garbage to simulate extra memory presure.
  92.             if (CREATE_EXTRA_GARBAGE) {
  93.                 for (int i = 0; i < GARBAGE_PASSES_PER_LOOP; i++) {
  94.                     garbageBin.add(String.format("This is some extra garbage: %s, %d", key, i));
  95.                     garbageBinSize = garbageBin.size();
  96.                     if (garbageBinSize == GARBAGE_BIN_SIZE) {
  97.                         garbageBin = new HashSet<>();
  98.                     }
  99.                 }
  100.             }
  101.  
  102.             count++;
  103.             if (count % CHECKPOINT_INTERVAL == 0) {
  104.                 long now = System.currentTimeMillis();
  105.  
  106.                 if (lastCheckpoint + METRICS_INTERVAL_MILLIS <= now) {
  107.                     int delta = count - lastCount;
  108.                     long duration = now - lastCheckpoint;
  109.                     double rate = (double) delta * 1000 / duration;
  110.                     long size = mapper.size();
  111.                     long totalDuration = now - start;
  112.                     double totalRate = (double) count * 1000 / totalDuration;
  113.  
  114.                     System.out.printf("Size: %d;  Inserted: %d;  Rate since last: %.2f;  Rate since start: %.2f; Max value: %d; Garbage bin size: %d%n", size, delta, rate, totalRate, largestValue, garbageBinSize);
  115.                     lastCheckpoint = now;
  116.                     lastCount = count;
  117.                 }
  118.             }
  119.         }
  120.     }
  121.  
  122.     static Random random = new Random(System.currentTimeMillis());
  123.  
  124.     // Create a large string.  MAX_VALUE possible distinct values.
  125.     private static String createKey(int count) {
  126.         int keyNumericValue;
  127.         if (FAST_FILL_MAP && count < NUMBER_DISTINCT_KEYS) {
  128.             keyNumericValue = count;
  129.         } else {
  130.             keyNumericValue = random.nextInt(NUMBER_DISTINCT_KEYS);
  131.         }
  132.         String base = Integer.toString(keyNumericValue);
  133.         if (STRING_SIZE_FACTOR == 1) {
  134.             return base;
  135.         } else {
  136.             StringBuilder sb = new StringBuilder();
  137.             for (int i = 0; i < STRING_SIZE_FACTOR; i++) {
  138.                 sb.append(base);
  139.             }
  140.             return sb.toString();
  141.         }
  142.     }
  143. }
  144.  
Advertisement
Add Comment
Please, Sign In to add comment