Skip to content

Instantly share code, notes, and snippets.

@angelsl
Created July 25, 2012 17:41
Show Gist options
  • Save angelsl/3177464 to your computer and use it in GitHub Desktop.
Save angelsl/3177464 to your computer and use it in GitHub Desktop.
javanx standard benchmark
/**
* nxjava: a library for loading the NX file format
* Copyright (C) 2012 Cedric Van Goethem
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.zepheus.nxjava.tests;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.EnumSet;
import net.zepheus.nxjava.NXFile;
import net.zepheus.nxjava.NXNode;
import net.zepheus.nxjava.NXReadMode;
public class Benchmark {
private static final int COUNT = 10;
private static final String FILE_PATH = "D:\\Data.nx";
private static final EnumSet<NXReadMode> PROPERTIES = EnumSet.of(NXReadMode.LOW_MEMORY);
public static void main(String[] args) throws Throwable {
NXFile file = loadTest(FILE_PATH);
accessTest(file, 1000000);
recurseTest(file);
recurseTest(file);
memoryTest();
file.close();
}
private static NXFile loadTest(String path) throws FileNotFoundException, IOException {
long start = System.nanoTime();
NXFile file = new NXFile(path, PROPERTIES);
long time = System.nanoTime() - start;
System.out.println("Loading file & string table took " + time/1000 + " µs.");
return file;
}
private static void memoryTest() {
System.gc();
long usage = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
long MB = usage / (1024);
System.out.println("Memory usage after testing: " + MB + " KB");
}
private static void accessTest(NXFile file, int times) {
System.gc();
long start = System.currentTimeMillis();
for(int i = 0; i < times * COUNT; i++)
{
NXNode node = file.resolvePath("Effect", "BasicEff.img", "LevelUp", "5", "origin");
}
long time = System.currentTimeMillis() - start;
long average = time / COUNT;
System.out.println("Access took " + average + " ms based on " + COUNT + " results.");
}
private static void recurseTest(NXFile file) {
System.gc();
long start = System.currentTimeMillis();
recurse(file.getRoot());
long time = System.currentTimeMillis() - start;
long average = time;
System.out.println("Full recursion took " + average + " ms");
}
private static void recurse(NXNode node) {
for(NXNode child : node)
recurse(child);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment