A Java library for tracking statistics over time in a round robin database. A round robin database is a fixed size database with several time spans it accumulates statistics over.
- StatPoint - a single statistical point in an archive, contains total number of stats, their sum, min, max, and average
- StatArchive - An archive contains a fixed number of points. Each point contains a summary for each statistic added to that point in the interval of time. When a point must be added when the archive is full the oldest point is overwritten.
- StatDatabase - A database of round robin archives which hold the summary of a statistic over time
- A database can be stored in memory, in a file, or in a memory-mapped file
- Adding statistics to databases is a non-blocking operation that adds virtually no overhead.
- All data can be written out to the backing store (memory or file) whenever there's a statistic added or at some interval.
- An archive can be exported to a chart as a PNG, JPG, BMP, or a data file (CSV).
- A group of databases stored in a folder can be handled/loaded all with one class (StatGroup).
- Memory-mapped databases are fast in memory databases that the OS flushes out to a file - this is the most reliable store type.
public class Stats
{
public static StatFormat FORMAT;
static
{
StatService.get();
FORMAT = new StatFormat( ) ; // A format that covers a year (365.25 days)
FORMAT.set( 0, 300205L, 12 ); // one point every 5 minutes for an hour
FORMAT.set( 1, 3602467L, 24 ); // one point every hour for a day
FORMAT.set( 2, 43229589L, 14 ); // one point every 12 hours for a week
FORMAT.set( 3, 86459178L, 31 ); // one point every day for a month
FORMAT.set( 4, 7606876923L, 52 ); // one point for every week in a year
FORMAT.compile(); // database size = 3328 bytes
}
// Save Statistics for MyClass
public static StatDatabase DB_1 = new StatDatabase(MyClass.class, FORMAT);
// Save Statistics in "stat.db"
public static StatDatabase DB_2 = new StatDatabase(new FileStore("stat.db"), FORMAT);
}
// Adding one when an event occurs.
Stats.DB_1.add(1f);
// Adding a value over time (for example time).
Stats.DB_2.add(2.435f);
// Close the databases
StatGroup.getRoot().close();
// Stop Statistics Thread
StatService.get().stop();
Example that saves all databases in memory-mapped files in a specific directory AND databases are dynamically created/loaded (through take).
public class Stats
{
public static StatFormat FORMAT;
public static StatGroup GROUP;
static
{
StatService.get();
FORMAT = new StatFormat( 5 ); // A format that covers a year (365.25 days)
FORMAT.set( 0, 300205L, 12 ); // one point every 5 minutes for an hour
FORMAT.set( 1, 3602467L, 24 ); // one point every hour for a day
FORMAT.set( 2, 43229589L, 14 ); // one point every 12 hours for a week
FORMAT.set( 3, 86459178L, 31 ); // one point every day for a month
FORMAT.set( 4, 7606876923L, 52 ); // one point for every week in a year
FORMAT.compile(); // database size = 3328 bytes
GROUP = new StatGroup( "/home/stat/my_app_stats" );
GROUP.setFactory( new MappedStoreFactory() );
GROUP.setEnabledDefault(true);
GROUP.setFormatDefault(FORMAT);
}
public static void add(String name, float statistic)
{
GROUP.take( name ).add( statistic );
}
public static close()
{
// Close the databases
GROUP.close();
// Stop Statistics Thread
StatService.get().stop();
}
}
// Adding one when an event occurs and creating the "number_of_events" database if it doesn't exist already.
Stats.add( "number_of_events", 1.0f );
// Adding a value over time (for example elapsed time) and creating the "event_elapsed_time" database if it
// doesn't exist already.
Stats.add( "event_elapsed_time", 2.435f );
Stats.close();
// Archive 0 = past hour
StatExport.export(Stats.DB_1.getArchive(0), Type.CSV, new File("sdb.csv"));
StatExport.export(Stats.DB_2.getArchive(0), Type.PNG, new File("sdb.png"));
- statastics-1.0.0.jar
- statastics-src-1.0.0.jar - includes source code
- statastics-all-1.0.0.jar - includes all dependencies
- statastics-all-src-1.0.0.jar - includes all dependencies and source code