package com.tngtech.jgiven.impl;

import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;

public class ProxyClassPerformanceTest {

    @Test
    public void test_creation_of_proxy_classes(){
        Set<Long> results = new HashSet<>();
        for( int i = 0; i < 1000; i++ ) {
            ScenarioBase scenario = new ScenarioBase();
            TestStage testStage = scenario.addStage( TestStage.class );
            testStage.something();
            if( i % 100 == 0 ) {
                System.gc();
                long usedMemory = ( Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() ) / ( 1024 * 1024 );
                System.out.println( "Used memory: " + usedMemory );
                results.add( usedMemory );
            }
        }

        assertThat( results )
                .describedAs( "Only should contains 1 or 2 items, as first iteration might use more memory might contain 2 items" )
                .hasSizeBetween( 1, 2 );
    }

    public static class TestStage {
        public void something(){
        }
    }
}

@@ -38,21 +38,56 @@
 ort 
-org.junit.Tes
+static org.assertj.core.api.Assertions.assertTha
 t;%0A%0A
@@ -138,64 +138,28 @@
 et;%0A
-%0A
 import 
-static org.assertj.core.api.Assertions.assertTha
+org.junit.Tes
 t;%0A%0A
@@ -258,16 +258,17 @@
 lasses()
+ 
 %7B%0A      
@@ -279,23 +279,44 @@
 t%3CLong%3E 
-results
+megabytesOfMemoryUsedInCycle
  = new H
@@ -338,18 +338,18 @@
      for
-(
  
+(
 int i = 
@@ -364,17 +364,16 @@
 000; i++
- 
 ) %7B%0A    
@@ -476,17 +476,16 @@
 ddStage(
- 
 TestStag
@@ -491,17 +491,16 @@
 ge.class
- 
 );%0A     
@@ -543,18 +543,18 @@
       if
-(
  
+(
 i %25 100 
@@ -557,17 +557,16 @@
 100 == 0
- 
 ) %7B%0A    
@@ -628,99 +628,51 @@
 y = 
-( Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() ) / ( 1024 * 1024 
+calculateMemoryUsageRoundedDownToMegabytes(
 );%0A 
@@ -705,17 +705,16 @@
 println(
- 
 %22Used me
@@ -725,33 +725,32 @@
 : %22 + usedMemory
- 
 );%0A             
@@ -756,21 +756,41 @@
     
-results
+megabytesOfMemoryUsedInCycle
 .add(
- 
 used
@@ -795,17 +795,16 @@
 edMemory
- 
 );%0A     
@@ -846,23 +846,38 @@
 hat(
- results )%0A    
+megabytesOfMemoryUsedInCycle)%0A
     
@@ -901,22 +901,20 @@
 dAs(
- %22Only
+%22Set
  should 
 cont
@@ -913,33 +913,62 @@
 uld 
+only 
 contain
-s
  1 
-or 2 
 item
-s
 , 
-as
+%22%0A                    + %22but the 
  fir
@@ -1001,16 +1001,53 @@
 e memory
+, %22%0A                    + %22so the set
  might c
@@ -1061,17 +1061,16 @@
 2 items%22
- 
 )%0A      
@@ -1075,20 +1075,16 @@
         
- 
-   
 .hasSize
@@ -1095,14 +1095,188 @@
 een(
- 
 1, 2
- 
+);%0A    %7D%0A%0A    private long calculateMemoryUsageRoundedDownToMegabytes() %7B%0A        return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024 * 1024
 );%0A 
@@ -1348,16 +1348,17 @@
 ething()
+ 
 %7B%0A      

Improve expressiveness of proxy class memory usage test