pveselov

finally stress test

Nov 8th, 2021
2,165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. import javax.crypto.Cipher;
  2. import java.io.FileOutputStream;
  3. import java.io.OutputStreamWriter;
  4. import java.io.PrintWriter;
  5. import java.util.concurrent.ThreadLocalRandom;
  6. import java.util.concurrent.atomic.AtomicLong;
  7.  
  8.  
  9. class Scratch {
  10.  
  11.     static ThreadLocal<byte[]> tl = new ThreadLocal<>();
  12.     static long last;
  13.     static final Object lastLock = new Object();
  14.     static long count;
  15.     static AtomicLong cycles = new AtomicLong();
  16.  
  17.     public static void main(String[] args) {
  18.  
  19.         for (int i=0; i<50; i++) {
  20.         spawn();
  21.         }
  22.  
  23.         while (true) {
  24.             try {
  25.                 Thread.sleep(10000);
  26.             } catch (Exception ignored) {}
  27.         }
  28.  
  29.     }
  30.  
  31.     private static void spawn() {
  32.             new Thread(()->{
  33.         cycles.getAndIncrement();
  34.         int lim = ThreadLocalRandom.current().nextInt(10000) + 100;
  35.                 while (lim-- > 0) {
  36.                     new Scratch().doIt();
  37.                 }
  38.         spawn();
  39.             }).start();
  40.     }
  41.  
  42.     private void doIt() {
  43.  
  44.         byte [] stuff = new byte[1024];
  45.         ThreadLocalRandom.current().nextBytes(stuff);
  46.  
  47.         tl.set(stuff);
  48.  
  49.         try {
  50.             uselessWork();
  51.         } catch (Exception e) {
  52.             dumpException(e);
  53.         } finally {
  54.             tl.remove();
  55.         }
  56.  
  57.         if (tl.get() != null) {
  58.             System.out.println("Finally broke");
  59.             System.exit(1);
  60.         }
  61.  
  62.         long now = System.currentTimeMillis();
  63.         synchronized (lastLock) {
  64.             if (now - last > 10000) {
  65.                 last = now;
  66.                 System.out.println("I are OK "+(++count) + " " + cycles.get());
  67.             }
  68.         }
  69.  
  70.     }
  71.  
  72.     private void dumpException(Throwable e) {
  73.         try {
  74.             try (FileOutputStream fos = new FileOutputStream("/dev/null"); OutputStreamWriter w = new OutputStreamWriter(fos); PrintWriter pw = new PrintWriter(
  75.                     w)) {
  76.                 e.printStackTrace(pw);
  77.             }
  78.         } catch (Exception e2) {
  79.             e2.printStackTrace();
  80.             System.exit(1);
  81.         }
  82.     }
  83.  
  84.     private void uselessWork() throws Exception {
  85.  
  86.         Cipher c = Cipher.getInstance("AES_192/CFB/NoPadding");
  87.         byte [] data = c.update(tl.get());
  88.         byte [] fin = c.doFinal();
  89.  
  90.         if (fin.length == 0) { fin = data; }
  91.  
  92.         if (ThreadLocalRandom.current().nextDouble() > 0.5) {
  93.             throw new IllegalArgumentException(toStr(fin));
  94.         }
  95.  
  96.     }
  97.  
  98.     static private String toStr(byte[] b) {
  99.         StringBuilder sb = new StringBuilder();
  100.         for (byte ib : b) {
  101.             sb.append(ib);
  102.         }
  103.         return sb.toString();
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment