test_performance.dart 8.5 KB
Newer Older
1 2 3 4 5 6
import 'dart:sky' as sky;
import 'dart:math' as math;

import 'package:sky/mojo/asset_bundle.dart';
import 'package:sky/rendering/object.dart';
import 'package:sky/theme/colors.dart' as colors;
7
import 'package:sky/widgets.dart';
8
import 'package:skysprites/skysprites.dart';
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

AssetBundle _initBundle() {
  if (rootBundle != null)
    return rootBundle;
  return new NetworkAssetBundle(Uri.base);
}

final AssetBundle _bundle = _initBundle();

ImageMap _images;
SpriteSheet _spriteSheet;
TestApp _app;

main() async {
  _images = new ImageMap(_bundle);

  await _images.load([
    'assets/sprites.png'
  ]);

  String json = await _bundle.loadString('assets/sprites.json');
  _spriteSheet = new SpriteSheet(_images['assets/sprites.png'], json);

  _app = new TestApp();
  runApp(_app);
}

class TestApp extends App {

  Widget build() {
    ThemeData theme = new ThemeData(
      brightness: ThemeBrightness.light,
      primarySwatch: colors.Purple
    );

    return new Theme(
      data: theme,
46 47
      child: new Title(
        title: 'Test Sprite Performance',
48 49 50 51 52 53 54 55
        child: new SpriteWidget(new TestPerformance())
      )
    );
  }
}

class TestPerformance extends NodeWithSize {
  final int numFramesPerTest = 100;
56
  final int numTests = 5;
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

  TestPerformance() : super(new Size(1024.0, 1024.0)) {
  }

  int test = 0;
  int frame = 0;
  int testStartTime;

  void update(double dt) {
    if (frame % numFramesPerTest == 0) {
      if (test > 0 && test <= numTests) {
        // End last test
        int currentTime = new DateTime.now().millisecondsSinceEpoch;
        int totalTestTime = currentTime - testStartTime;
        double millisPerFrame =
          totalTestTime.toDouble() / numFramesPerTest.toDouble();
73
        print("  - RESULT fps: ${(1.0 / (millisPerFrame / 1000)).toStringAsFixed(1)} millis/frame: ${millisPerFrame.round()}");
74 75 76 77 78 79 80

        // Clear test
        removeAllChildren();
      }

      if (test < numTests) {
        // Start new test
81 82 83 84 85
        PerformanceTest perfTest = createTest(test);
        addChild(perfTest);

        print("TEST ${test + 1}/$numTests STARTING: ${perfTest.name}");

86 87 88 89 90 91 92
        testStartTime = new DateTime.now().millisecondsSinceEpoch;
      }
      test++;
    }
    frame++;
  }

93
  PerformanceTest createTest(int n) {
94 95
    if (test == 0) {
      // Test atlas performance
96
      return new TestPerformanceAtlas();
97
    } else if (test == 1) {
98 99
      // Test atlas performance
      return new TestPerformanceAtlas2();
100
    } else if (test == 2) {
101 102 103 104 105 106
      // Test sprite performance
      return new TestPerformanceSprites();
    } else if (test == 3) {
      // Test sprite performance
      return new TestPerformanceSprites2();
    } else if (test == 4) {
107
      // Test particle performance
108
      return new TestPerformanceParticles();
109
    }
110
    return null;
111 112 113
  }
}

114 115 116 117 118 119 120
abstract class PerformanceTest extends Node {
  String get name;
}

class TestPerformanceParticles extends PerformanceTest {
  String get name => "64 particle systems";

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  final grid = 8;
  TestPerformanceParticles() {
    for (int x = 0; x < grid; x++) {
      for (int y = 0; y < grid; y++) {
        ParticleSystem particles = new ParticleSystem(
            _spriteSheet["explosion_particle.png"],
            rotateToMovement: true,
            startRotation:90.0,
            startRotationVar: 0.0,
            endRotation: 90.0,
            startSize: 0.3,
            startSizeVar: 0.1,
            endSize: 0.3,
            endSizeVar: 0.1,
            emissionRate:100.0,
            greenVar: 127,
            redVar: 127
        );
        particles.position = new Point(x * 1024.0 / (grid - 1), y * 1024.0 / (grid - 1));
        addChild(particles);
      }
    }
  }
}

146 147 148
class TestPerformanceSprites extends PerformanceTest {
  String get name => "1001 sprites (24% offscreen)";

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  final int grid = 100;

  TestPerformanceSprites() {
    for (int x = 0; x < grid; x++) {
      for (int y = 0; y < grid; y++) {
        Sprite sprt = new Sprite(_spriteSheet["asteroid_big_1.png"]);
        sprt.scale = 1.0;
        sprt.position = new Point(x * 1024.0 / (grid - 1), y * 1024.0 / (grid - 1));
        addChild(sprt);

        //sprt.actions.run(new ActionRepeatForever(new ActionTween((a) => sprt.rotation = a, 0.0, 360.0, 1.0)));
      }
    }

    Sprite sprt = new Sprite(_spriteSheet["asteroid_big_1.png"]);
    sprt.position = new Point(512.0, 512.0);
    addChild(sprt);
  }

  void update(double dt) {
    for (Sprite sprt in children) {
      sprt.rotation += 1;
    }
  }
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
class TestPerformanceSprites2 extends PerformanceTest {
  String get name => "1001 sprites (24% offscreen never added)";

  final int grid = 100;

  TestPerformanceSprites2() {
    for (int x = 12; x < grid - 12; x++) {
      for (int y = 0; y < grid; y++) {
        Sprite sprt = new Sprite(_spriteSheet["asteroid_big_1.png"]);
        sprt.scale = 1.0;
        sprt.position = new Point(x * 1024.0 / (grid - 1), y * 1024.0 / (grid - 1));
        addChild(sprt);

        //sprt.actions.run(new ActionRepeatForever(new ActionTween((a) => sprt.rotation = a, 0.0, 360.0, 1.0)));
      }
    }

    Sprite sprt = new Sprite(_spriteSheet["asteroid_big_1.png"]);
    sprt.position = new Point(512.0, 512.0);
    addChild(sprt);
  }

  void update(double dt) {
    for (Sprite sprt in children) {
      sprt.rotation += 1;
    }
  }
}

class TestPerformanceAtlas extends PerformanceTest {
  String get name => "1001 rects drawAtlas (24% offscreen)";

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  final int grid = 100;

  double rotation = 0.0;
  List<Rect> rects = [];
  Paint cachedPaint = new Paint()
    ..setFilterQuality(sky.FilterQuality.low)
    ..isAntiAlias = false;

  TestPerformanceAtlas() {
    for (int x = 0; x < grid; x++) {
      for (int y = 0; y < grid; y++) {
        rects.add(_spriteSheet["asteroid_big_1.png"].frame);
      }
    }
    rects.add(_spriteSheet["asteroid_big_1.png"].frame);
  }

  void paint(PaintingCanvas canvas) {
    // Setup transforms
    List<sky.RSTransform> transforms = [];

    for (int x = 0; x < grid; x++) {
      for (int y = 0; y < grid; y++) {
        double xPos = x * 1024.0 / (grid - 1);
        double yPos = y * 1024.0 / (grid - 1);

        transforms.add(createTransform(xPos, yPos, rects[0].size.width / 2.0, rects[0].size.height / 2.0, rotation, 1.0));
      }
    }

    transforms.add(createTransform(512.0, 512.0, rects[0].size.width / 2.0, rects[0].size.height / 2.0, rotation, 1.0));

    // Draw atlas
240 241
    Rect cullRect = spriteBox.visibleArea;
    canvas.drawAtlas(_spriteSheet.image, transforms, rects, null, null, cullRect, cachedPaint);
242 243 244 245 246 247 248 249 250 251 252 253 254 255
  }

  void update(double dt) {
    rotation += 1.0;
  }

  sky.RSTransform createTransform(double x, double y, double ax, double ay, double rot, double scale) {
    double scos = math.cos(convertDegrees2Radians(rot)) * scale;
    double ssin = math.sin(convertDegrees2Radians(rot)) * scale;
    double tx = x + -scos * ax + ssin * ay;
    double ty = y + -ssin * ax - scos * ay;
    return new sky.RSTransform(scos, ssin, tx, ty);
  }
}
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

class TestPerformanceAtlas2 extends PerformanceTest {
  String get name => "1001 rects drawAtlas (24% offscreen never added)";

  final int grid = 100;

  double rotation = 0.0;
  List<Rect> rects = [];
  Paint cachedPaint = new Paint()
    ..setFilterQuality(sky.FilterQuality.low)
    ..isAntiAlias = false;

  TestPerformanceAtlas2() {
    for (int x = 12; x < grid - 12; x++) {
      for (int y = 0; y < grid; y++) {
        rects.add(_spriteSheet["asteroid_big_1.png"].frame);
      }
    }
    rects.add(_spriteSheet["asteroid_big_1.png"].frame);
  }

  void paint(PaintingCanvas canvas) {
    // Setup transforms
    List<sky.RSTransform> transforms = [];

    for (int x = 12; x < grid - 12; x++) {
      for (int y = 0; y < grid; y++) {
        double xPos = x * 1024.0 / (grid - 1);
        double yPos = y * 1024.0 / (grid - 1);

        transforms.add(createTransform(xPos, yPos, rects[0].size.width / 2.0, rects[0].size.height / 2.0, rotation, 1.0));
      }
    }

    transforms.add(createTransform(512.0, 512.0, rects[0].size.width / 2.0, rects[0].size.height / 2.0, rotation, 1.0));

    // Draw atlas
    Rect cullRect = spriteBox.visibleArea;
    canvas.drawAtlas(_spriteSheet.image, transforms, rects, null, null, cullRect, cachedPaint);
  }

  void update(double dt) {
    rotation += 1.0;
  }

  sky.RSTransform createTransform(double x, double y, double ax, double ay, double rot, double scale) {
    double scos = math.cos(convertDegrees2Radians(rot)) * scale;
    double ssin = math.sin(convertDegrees2Radians(rot)) * scale;
    double tx = x + -scos * ax + ssin * ay;
    double ty = y + -ssin * ax - scos * ay;
    return new sky.RSTransform(scos, ssin, tx, ty);
  }
}