Package ca.jimr.gae.profiler.MiniProfiler

Examples of ca.jimr.gae.profiler.MiniProfiler.Step


      step.close();
    }
  }

  private void uploadDisks(BlobKey zipBlobKey) throws GameEngineException {
    Step step = MiniProfiler.step("API.uploadDisks");
    try {
      if (zipBlobKey == null) {
        return;
      }
      final ZipInputStream zis;
      try {

        zis = getZipFile(zipBlobKey);

        ZipEntry ze;

        CSVParser p = new CSVParser();

        while ((ze = zis.getNextEntry()) != null) {

          final ByteArrayOutputStream baos = new ByteArrayOutputStream();

          final byte[] b = new byte[2048];

          int size = 0;
          while ((size = zis.read(b, 0, b.length)) != -1) {
            baos.write(b, 0, size);
          }

          String[] parts = ze.getName().split("/");
          String fileName = parts[parts.length - 1];

          if (fileName.contains("disks.csv")) {

            final String csv = new String(baos.toByteArray());

            List<?> headers = null;
            boolean h = true;

            for (String line : csv.split("\n")) {

              line = line.trim();

              // first row
              if (h) {
                headers = Arrays.asList(p.parseLine(line));
                h = false;
              }

              // data
              else {
                final String[] diskData = p.parseLine(line);

                final int length = diskData.length;
                if (length <= 1) {
                  break;
                }

                final String diskName = diskData[headers
                    .indexOf("name")];

                final String type = diskData[headers
                    .indexOf("type")];

                int attack = Integer.parseInt(diskData[headers
                    .indexOf("attack")]);
                int defense = Integer.parseInt(diskData[headers
                    .indexOf("defense")]);
                int toughness = Integer
                    .parseInt(diskData[headers
                        .indexOf("toughness")]);
                int movement = Integer
                    .parseInt(diskData[headers
                        .indexOf("movement")]);
                int wounds = Integer.parseInt(diskData[headers
                    .indexOf("wounds")]);
                int indexOfFlying = headers.indexOf("flying");
                String s = diskData[indexOfFlying];
                boolean flying = Boolean.parseBoolean(s);
                boolean swashbuckler = headers
                    .indexOf("swashbuckler") > -1
                    && !diskData[headers
                        .indexOf("swashbuckler")]
                        .equals("null") ? Boolean
                    .parseBoolean(diskData[headers
                        .indexOf("swashbuckler")])
                    : false;
                boolean archer = headers.indexOf("archer") > -1
                    && !diskData[headers.indexOf("archer")]
                        .equals("null") ? Boolean
                    .parseBoolean(diskData[headers
                        .indexOf("archer")]) : false;
                int arrows = headers.indexOf("arrows") > -1
                    && !diskData[headers.indexOf("arrows")]
                        .equals("null") ? Integer
                    .parseInt(diskData[headers
                        .indexOf("arrows")]) : 0;
                int bolts = headers.indexOf("bolts") > -1
                    && !diskData[headers.indexOf("bolts")]
                        .equals("null") ? Integer
                    .parseInt(diskData[headers
                        .indexOf("bolts")]) : 0;
                int fireballs = headers.indexOf("fireballs") > -1
                    && !diskData[headers
                        .indexOf("fireballs")]
                        .equals("null") ? Integer
                    .parseInt(diskData[headers
                        .indexOf("fireballs")]) : 0;
                int boulders = headers.indexOf("boulders") > -1
                    && !diskData[headers
                        .indexOf("boulders")]
                        .equals("null") ? Integer
                    .parseInt(diskData[headers
                        .indexOf("boulders")]) : 0;
                boolean missileImmunity = headers
                    .indexOf("missileImmunity") > -1
                    && !diskData[headers
                        .indexOf("missileImmunity")]
                        .equals("null") ? Boolean
                    .parseBoolean(diskData[headers
                        .indexOf("missileImmunity")])
                    : false;
                boolean firstblow = Boolean
                    .parseBoolean(headers
                        .indexOf("firstblow") > -1
                        && !diskData[headers
                            .indexOf("firstblow")]
                            .equals("null") ? diskData[headers
                        .indexOf("firstblow")]
                        : "false");
                int spellcaster = Integer
                    .parseInt(headers
                        .indexOf("spellcaster") > -1
                        && !diskData[headers
                            .indexOf("spellcaster")]
                            .equals("null") ? diskData[headers
                        .indexOf("spellcaster")] : "0");
                int limit = headers.indexOf("limit") > -1
                    && !diskData[headers.indexOf("limit")]
                        .equals("null") ? Integer
                    .parseInt(diskData[headers
                        .indexOf("limit")]) : 0;
                int cost = Integer.parseInt(diskData[headers
                    .indexOf("cost")]);
                ScriptableObject disk = createDisk(
                    diskName,
                    type,
                    attack,
                    defense,
                    toughness,
                    movement,
                    wounds,
                    flying,
                    swashbuckler,
                    archer,
                    arrows,
                    bolts,
                    fireballs,
                    boulders,
                    missileImmunity,
                    firstblow,
                    spellcaster,
                    limit,
                    cost,
                    diskData[headers.indexOf("faction")],
                    diskData[headers.indexOf("alignment")],
                    diskData[headers.indexOf("diameter")],
                    diskData[headers.indexOf("description")],
                    diskData[headers.indexOf("price")]);

                saveDisk(disk);
              }
            }
          }
        }
        zis.close();
      } catch (FileNotFoundException fnf) {
        fnf.printStackTrace();
      } catch (IOException ioe) {
        ioe.printStackTrace();
      } finally {
        // delete the zip
        if (zipBlobKey != null) {
          BlobstoreService blobstoreService = BlobstoreServiceFactory
              .getBlobstoreService();
          blobstoreService.delete(zipBlobKey);
        }
      }
    } finally {
      step.close();
    }
  }
View Full Code Here


  private String[] includes;
  private String[] invokes;

  public GameEngine(String[] jsFiles) {
    Step step = MiniProfiler.step("GameEngine");
    this.response = null;

    Step step1 = MiniProfiler.step("GameEngine set up context and scope");

    this.cx = new ContextFactory().enterContext();
    this.scope = this.cx.initStandardObjects(null, true);

    step1.close();

    Step step2 = MiniProfiler.step("GameEngine load IO.ioWrappers");

    this.loadScript(IO.ioWrappers, "IO.ioWrappers");

    step2.close();

    Step step3 = MiniProfiler
        .step("GameEngine load Persistence.persistenceWrappers");

    this.loadScript(Persistence.persistenceWrappers,
        "Persistence.persistenceWrappers");

    step3.close();

    Step step4 = MiniProfiler.step("GameEngine load custom scripts");

    for (String js : jsFiles) {
      this.loadScriptFromFile(js);
    }

    step4.close();

    // engine.put("e", this);
    this.scope.put("e", this.scope, Context.javaToJS(this, this.scope));

    step.close();
View Full Code Here

    }
  }

  public Object invoke(Scriptable object, String methodName)
      throws GameEngineException {
    Step step = MiniProfiler.step("GameEngine.invoke(Scriptable,String)");
    try {
      Object result = null;
      try {
        result = ScriptableObject.callMethod(object, methodName, null);
      } catch (EcmaError ee) {
        throw new GameEngineException(ee);
      }
      return result;
    } finally {
      step.close();
    }
  }
View Full Code Here

   * @throws GameEngineException
   * @throws Exception
   */
  public Object invoke(Scriptable object, String methodName, Object[] args)
      throws GameEngineException {
    Step step = MiniProfiler.step("GameEngine.invoke");
    try {
      Object result = null;
      try {
        result = ScriptableObject.callMethod(object, methodName, args);
      } catch (EcmaError ee) {
        throw new GameEngineException(ee);
      }
      return result;
    } finally {
      step.close();
    }
  }
View Full Code Here

    }
    return result;
  }

  public void loadScript(String script, String name) {
    Step step = MiniProfiler.step("GameEngine.loadScript " + name);
    this.cx.evaluateString(this.scope, script, name, 1, null);
    step.close();
  }
View Full Code Here

    step.close();
  }

  private void loadScriptFromFile(String pathName) {

    Step step = MiniProfiler.step("GameEngine.loadScriptFromFile");

    Step step1 = MiniProfiler
        .step("GameEngine.loadScriptFromFile getResourceAsStream");

    InputStream ras = this.getClass().getResourceAsStream(pathName);

    step1.close();

    if (ras == null)
      try {
        throw new Exception(pathName + " not found");
      } catch (Exception e) {
        e.printStackTrace();
        return;
      }

    Step step2 = MiniProfiler
        .step("GameEngine.loadScriptFromFile new Scanner");

    Scanner s = new Scanner(ras);

    step2.close();

    StringBuilder builder = new StringBuilder();

    Step step3 = MiniProfiler
        .step("GameEngine.loadScriptFromFile nextLine");

    while (s.hasNextLine()) {
      builder.append("\n").append(s.nextLine());
    }

    step3.close();

    try {
      ras.close();
    } catch (IOException e) {
      e.printStackTrace();
View Full Code Here

      return e;
    }

    @SuppressWarnings("unchecked")
    public Entity save(String clazz, long id, Map<String, Object> properties) {
      Step step = MiniProfiler
          .step("GameEngine.Persistence.save(String,long,Map<String,Object>)");
      try {
        DatastoreService datastore = DatastoreServiceFactory
            .getDatastoreService();
        Entity e = new Entity(KeyFactory.createKey(clazz, id));
        {
          // set properties
          for (Entry<String, Object> entry : properties.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof String
                && ((String) value).length() >= 500) {
              e.setProperty(entry.getKey(), new Text(
                  (String) value));
            } else if (value instanceof String) {
              e.setProperty(entry.getKey(), ((String) value));
            } else if (value instanceof Collection) {
              e.setProperty(entry.getKey(),
                  ((Collection<?>) value));
            } else if (value instanceof Object[]) {
              e.setProperty(entry.getKey(),
                  Arrays.asList((Object[]) value));
            } else {
              e.setProperty(entry.getKey(), value);
            }
          }

          try {
            datastore.put(e);
          } catch (RequestTooLargeException rtle) {
          }
        }

        try {
          Cache cache = CacheManager.getInstance().getCacheFactory()
              .createCache(Collections.emptyMap());
          cache.put((String) (clazz + id),
              (Map<String, Object>) properties);

          // update all cache
          Map<Object, Map<String, Object>> objects = (Map<Object, Map<String, Object>>) cache
              .get("__ALL__" + clazz);
          if (objects != null) {

            objects.put(id, properties);

            cache.put("__ALL__" + clazz, objects);
          }
        } catch (CacheException ce) {
          // ...
        }
        return e;
      } finally {
        step.close();
      }
    }
View Full Code Here

  @Override
  public void doGet(final HttpServletRequest request,
      final HttpServletResponse response) {

    Step step = MiniProfiler.step("APIServlet.doGet");

    Step headerStep = MiniProfiler.step("APIServlet.doGet headers");

    response.setContentType("application/json");
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
    response.setHeader("Access-Control-Allow-Credentials", "true");

    headerStep.close();

    Step getParameterMapStep = MiniProfiler
        .step("APIServlet.doGet getParameters");

    @SuppressWarnings("unchecked")
    final Map<String, Object> parameters = new HashMap<String, Object>(
        request.getParameterMap());

    getParameterMapStep.close();

    final String user = API.getUser(request);

    Step uploadStuffStep = MiniProfiler
        .step("APIServlet.doGet upload stuff");

    // check the content type header
    // multipart/form-data
    final String contentType = request.getContentType();

    if (contentType != null && contentType.contains("multipart/form-data")) {

      final BlobstoreService blobstoreService = BlobstoreServiceFactory
          .getBlobstoreService();

      final Map<String, List<BlobKey>> blobKeysValues = blobstoreService
          .getUploads(request);
      for (final Entry<String, List<BlobKey>> blobKeyValues : blobKeysValues
          .entrySet()) {
        for (final BlobKey blobKey : blobKeyValues.getValue()) {
          final BlobInfo blobInfo = new BlobInfoFactory()
              .loadBlobInfo(blobKey);
          final long size = blobInfo.getSize();
          if (size > 0) {
            // process blob
            parameters.put(blobKeyValues.getKey(), blobKey);
          } else {
            blobstoreService.delete(blobKey);
          }
        }
      }
    }

    uploadStuffStep.close();

    try {
      final String result = new API().process(parameters, user, response);
      // long b = System.nanoTime();
      // log.info("doGet:" + (b - a) / 1000000 + "ms");
View Full Code Here

  }

  @Override
  public void doPost(final HttpServletRequest request,
      final HttpServletResponse response) {
    Step step = MiniProfiler.step("APIServlet.doPost");
    doGet(request, response);
    step.close();
  }
View Full Code Here

public class MiniProfilerTest
{
  @Test
  public void testProfileNotStarted()
  {
    Step s = MiniProfiler.step("Test");
    s.close();

    Profile p = MiniProfiler.stop();
    assertNull("No profile should be generated", p);
  }
View Full Code Here

TOP

Related Classes of ca.jimr.gae.profiler.MiniProfiler.Step

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.