2011-10-22 14:20:06 +00:00
|
|
|
|
|
|
|
//Benchmark that loads, and optionally extracts data from and/or emits a YAML file.
|
|
|
|
|
2011-10-23 14:10:27 +00:00
|
|
|
import std.conv;
|
2011-10-22 14:20:06 +00:00
|
|
|
import std.datetime;
|
|
|
|
import std.stdio;
|
2011-10-23 14:10:27 +00:00
|
|
|
import std.string;
|
2013-12-17 14:09:19 +00:00
|
|
|
import dyaml.all;
|
2011-10-22 14:20:06 +00:00
|
|
|
|
|
|
|
///Print help information.
|
|
|
|
void help()
|
|
|
|
{
|
|
|
|
string help =
|
|
|
|
"D:YAML benchmark\n"
|
2014-08-01 15:09:05 +00:00
|
|
|
"Copyright (C) 2011-2014 Ferdinand Majerech\n"
|
2011-10-22 14:20:06 +00:00
|
|
|
"Usage: yaml_bench [OPTION ...] [YAML_FILE]\n"
|
|
|
|
"\n"
|
|
|
|
"Loads and optionally extracts data and/or dumps a YAML file.\n"
|
|
|
|
"\n"
|
|
|
|
"Available options:\n"
|
|
|
|
" -h --help Show this help information.\n"
|
2014-08-01 15:09:05 +00:00
|
|
|
" -g --get Extract data from the file (using Node.as()).\n"
|
2011-10-23 14:10:27 +00:00
|
|
|
" -d --dump Dump the loaded data (to YAML_FILE.dump).\n"
|
|
|
|
" -r --runs=NUM Repeat the benchmark NUM times.\n";
|
2011-10-22 14:20:06 +00:00
|
|
|
writeln(help);
|
|
|
|
}
|
|
|
|
|
|
|
|
///Get data out of every node.
|
|
|
|
void extract(ref Node document)
|
|
|
|
{
|
|
|
|
void crawl(ref Node root)
|
|
|
|
{
|
|
|
|
if(root.isScalar) switch(root.tag)
|
|
|
|
{
|
2011-10-22 15:06:32 +00:00
|
|
|
case "tag:yaml.org,2002:null": auto value = root.as!YAMLNull; break;
|
|
|
|
case "tag:yaml.org,2002:bool": auto value = root.as!bool; break;
|
|
|
|
case "tag:yaml.org,2002:int": auto value = root.as!long; break;
|
|
|
|
case "tag:yaml.org,2002:float": auto value = root.as!real; break;
|
|
|
|
case "tag:yaml.org,2002:binary": auto value = root.as!(ubyte[]); break;
|
|
|
|
case "tag:yaml.org,2002:timestamp": auto value = root.as!SysTime; break;
|
|
|
|
case "tag:yaml.org,2002:str": auto value = root.as!string; break;
|
2011-10-22 14:20:06 +00:00
|
|
|
default: writeln("Unrecognozed tag: ", root.tag);
|
|
|
|
}
|
|
|
|
else if(root.isSequence) foreach(ref Node node; root)
|
|
|
|
{
|
|
|
|
crawl(node);
|
|
|
|
}
|
|
|
|
else if(root.isMapping) foreach(ref Node key, ref Node value; root)
|
|
|
|
{
|
|
|
|
crawl(key);
|
|
|
|
crawl(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
crawl(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
void main(string[] args)
|
|
|
|
{
|
|
|
|
bool get = false;
|
|
|
|
bool dump = false;
|
2011-10-23 14:10:27 +00:00
|
|
|
uint runs = 1;
|
2011-10-22 14:20:06 +00:00
|
|
|
string file = null;
|
|
|
|
|
|
|
|
//Parse command line args
|
|
|
|
foreach(arg; args[1 .. $])
|
|
|
|
{
|
2011-10-23 14:10:27 +00:00
|
|
|
auto parts = arg.split("=");
|
|
|
|
if(arg[0] == '-') switch(parts[0])
|
2011-10-22 14:20:06 +00:00
|
|
|
{
|
|
|
|
case "--help", "-h": help(); return;
|
|
|
|
case "--get", "-g": get = true; break;
|
|
|
|
case "--dump", "-d": dump = true; break;
|
2011-10-23 14:10:27 +00:00
|
|
|
case "--runs", "-r": runs = to!uint(parts[1]); break;
|
2011-10-22 14:20:06 +00:00
|
|
|
default: writeln("\nUnknown argument: ", arg, "\n\n"); help(); return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(file !is null)
|
|
|
|
{
|
|
|
|
writeln("\nUnknown argument or file specified twice: ", arg, "\n\n");
|
|
|
|
help();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file = arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(file is null)
|
|
|
|
{
|
|
|
|
writeln("\nFile not specified.\n\n");
|
|
|
|
help();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2011-10-23 14:10:27 +00:00
|
|
|
while(runs--)
|
2011-10-22 14:20:06 +00:00
|
|
|
{
|
2011-10-23 14:10:27 +00:00
|
|
|
auto nodes = Loader(file).loadAll();
|
|
|
|
if(dump)
|
|
|
|
{
|
|
|
|
Dumper(file ~ ".dump").dump(nodes);
|
|
|
|
}
|
|
|
|
if(get) foreach(ref node; nodes)
|
|
|
|
{
|
|
|
|
extract(node);
|
|
|
|
}
|
2011-10-22 14:20:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(YAMLException e)
|
|
|
|
{
|
|
|
|
writeln("ERROR: ", e.msg);
|
|
|
|
}
|
|
|
|
}
|