55
55
import java .util .HashSet ;
56
56
import java .util .Objects ;
57
57
import java .util .concurrent .CountDownLatch ;
58
- import java .util .concurrent .atomic .AtomicBoolean ;
59
58
59
+ import java .util .concurrent .atomic .AtomicInteger ;
60
60
import java .util .concurrent .atomic .AtomicLong ;
61
61
import javax .imageio .ImageIO ;
62
62
import javax .swing .JFrame ;
@@ -80,9 +80,10 @@ public class RenderPerfTest {
80
80
private final static int BH = 50 ;
81
81
private final static int COUNT = 600 ;
82
82
private final static int CYCLE_DELAY = 3 ;
83
- private final static int CYCLES_TILL_PAINT = 10 ;
83
+ private final static int MAX_FRAME_CYCLES = 3000 /CYCLE_DELAY ;
84
+
84
85
private final static int COLOR_TOLERANCE = 10 ;
85
- private final static int MAX_MEASURE_TIME = 5000 ;
86
+ private final static int MAX_MEASURE_CYCLES = 6000 / CYCLE_DELAY ;
86
87
87
88
private final static Color [] marker = {Color .RED , Color .BLUE , Color .GREEN , Color .YELLOW , Color .ORANGE , Color .MAGENTA };
88
89
@@ -608,19 +609,18 @@ static class PerfMeter {
608
609
private JPanel panel ;
609
610
610
611
private double execTime = 0 ;
611
- private int markerIdx = 0 ;
612
- private AtomicBoolean paintOccurred = new AtomicBoolean (false );
612
+ private AtomicInteger markerIdx = new AtomicInteger (0 );
613
613
private AtomicLong markerPaintTime = new AtomicLong (0 );
614
614
615
615
private double fps ;
616
+ private int skippedFrame = 0 ;
616
617
617
618
PerfMeter (String name ) {
618
619
this .name = name ;
619
620
}
620
621
621
622
PerfMeter exec (final Renderable renderable ) throws Exception {
622
623
final CountDownLatch latchFrame = new CountDownLatch (1 );
623
- final long endTime = System .currentTimeMillis () + MAX_MEASURE_TIME ;
624
624
625
625
final JFrame f = new JFrame ();
626
626
f .addWindowListener (new WindowAdapter () {
@@ -638,19 +638,15 @@ public void run() {
638
638
@ Override
639
639
protected void paintComponent (Graphics g ) {
640
640
super .paintComponent (g );
641
- if (markerIdx == 0 ) {
642
- markerPaintTime .set (System .nanoTime ());
643
- }
644
- paintOccurred .set (true );
641
+ markerPaintTime .set (System .nanoTime ());
645
642
646
643
Graphics2D g2d = (Graphics2D ) g .create ();
647
644
renderable .setup (g2d );
648
645
renderable .render (g2d );
649
646
g2d .setClip (null );
650
647
g2d .setPaintMode ();
651
- g2d .setColor (marker [markerIdx ]);
648
+ g2d .setColor (marker [markerIdx . get () ]);
652
649
g2d .fillRect (0 , 0 , BW , BH );
653
- markerIdx = (markerIdx + 1 ) % marker .length ;
654
650
}
655
651
};
656
652
@@ -665,19 +661,14 @@ protected void paintComponent(Graphics g) {
665
661
666
662
Robot robot = new Robot ();
667
663
int cycle = 0 ;
668
- int cycleToPaint = -1 ;
669
664
int frame = 0 ;
670
665
long paintTime = 0 ;
666
+ int maxFrameCycle = -1 ;
671
667
while (frame < COUNT ) {
672
- if (paintOccurred .compareAndSet (true , false )) {
673
- long t = markerPaintTime .getAndSet (0 );
674
- paintTime = t > 0 ? t : paintTime ;
675
- cycleToPaint = cycle + CYCLES_TILL_PAINT ;
676
- }
677
-
678
- if (cycle == cycleToPaint ) {
679
- renderable .update ();
680
- panel .getParent ().repaint ();
668
+ long t ;
669
+ if ((t = markerPaintTime .getAndSet (0 )) > 0 ) {
670
+ paintTime = t ;
671
+ maxFrameCycle = cycle + MAX_FRAME_CYCLES ;
681
672
}
682
673
683
674
if (paintTime > 0 ) {
@@ -692,18 +683,28 @@ protected void paintComponent(Graphics g) {
692
683
}
693
684
}
694
685
695
- if (currIdx == 0 ) {
686
+ if (currIdx == markerIdx . get () ) {
696
687
execTime += System .nanoTime () - paintTime ;
697
688
frame ++;
698
689
paintTime = 0 ;
690
+ maxFrameCycle = -1 ;
691
+ markerIdx .accumulateAndGet (marker .length , (x , y ) -> (x + 1 ) % y );
692
+ renderable .update ();
693
+ panel .getParent ().repaint ();
694
+ } else if (cycle >= maxFrameCycle ) {
695
+ skippedFrame ++;
696
+ paintTime = 0 ;
697
+ maxFrameCycle = -1 ;
698
+ markerIdx .accumulateAndGet (marker .length , (x , y ) -> (x + 1 ) % y );
699
+ panel .getParent ().repaint ();
699
700
}
700
701
}
701
702
try {
702
703
Thread .sleep (CYCLE_DELAY );
703
704
} catch (InterruptedException ex ) {
704
705
ex .printStackTrace ();
705
706
}
706
- if (System . currentTimeMillis () >= endTime ) {
707
+ if (cycle >= MAX_MEASURE_CYCLES ) {
707
708
break ;
708
709
}
709
710
cycle ++;
@@ -724,6 +725,9 @@ protected void paintComponent(Graphics g) {
724
725
}
725
726
726
727
private void report () {
728
+ if (skippedFrame > 0 ) {
729
+ System .err .println (skippedFrame + " frame(s) skipped" );
730
+ }
727
731
System .err .println (name + " : " + String .format ("%.2f FPS" , fps ));
728
732
}
729
733
0 commit comments