Added a YAML benchmark that loads and optionally extracts data

from and/or dumps a YAML file.
This commit is contained in:
Ferdinand Majerech 2011-10-22 16:20:06 +02:00
parent 3078262129
commit fb67e775e4
5 changed files with 124 additions and 11 deletions

5
cdc.d
View file

@ -143,9 +143,7 @@ void main(string[] args)
args = args[1 .. $]; args = args[1 .. $];
foreach(arg; args) foreach(arg; args)
{ {
if(arg[0] == '-') if(arg[0] == '-') switch(arg)
{
switch(arg)
{ {
case "--help", "-h": help(); return; case "--help", "-h": help(); return;
case "--dmd": compiler = "dmd"; break; case "--dmd": compiler = "dmd"; break;
@ -154,7 +152,6 @@ void main(string[] args)
default: extra_args ~= arg; default: extra_args ~= arg;
} }
} }
}
if(args.length > 0 && args[$ - 1][0] != '-'){target = args[$ - 1];} if(args.length > 0 && args[$ - 1][0] != '-'){target = args[$ - 1];}
string[] dbg = ["-gc", "-debug"]; string[] dbg = ["-gc", "-debug"];

View file

@ -0,0 +1,5 @@
main:
dmd -w -I../../ -L-L../../ -L-ldyaml yaml_bench.d
clean:
rm yaml_stats yaml_bench.o

View file

@ -0,0 +1,106 @@
//Benchmark that loads, and optionally extracts data from and/or emits a YAML file.
import std.datetime;
import std.stdio;
import yaml;
///Print help information.
void help()
{
string help =
"D:YAML benchmark\n"
"Copyright (C) 2011 Ferdinand Majerech\n"
"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"
" -g --get Extract data from the file (using Node.get()).\n"
" -d --dump Dump the loaded data (to YAML_FILE.dump).\n";
writeln(help);
}
///Get data out of every node.
void extract(ref Node document)
{
void crawl(ref Node root)
{
if(root.isScalar) switch(root.tag)
{
case "tag:yaml.org,2002:null": auto value = root.get!YAMLNull; break;
case "tag:yaml.org,2002:bool": auto value = root.get!bool; break;
case "tag:yaml.org,2002:int": auto value = root.get!long; break;
case "tag:yaml.org,2002:float": auto value = root.get!real; break;
case "tag:yaml.org,2002:binary": auto value = root.get!(ubyte[]); break;
case "tag:yaml.org,2002:timestamp": auto value = root.get!SysTime; break;
case "tag:yaml.org,2002:str": auto value = root.get!string; break;
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;
string file = null;
//Parse command line args
foreach(arg; args[1 .. $])
{
if(arg[0] == '-') switch(arg)
{
case "--help", "-h": help(); return;
case "--get", "-g": get = true; break;
case "--dump", "-d": dump = true; break;
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
{
auto nodes = Loader(file).loadAll();
if(dump)
{
Dumper(file ~ ".dump").dump(nodes);
}
if(get) foreach(ref node; nodes)
{
extract(node);
}
}
catch(YAMLException e)
{
writeln("ERROR: ", e.msg);
}
}

View file

@ -6,6 +6,9 @@ encoding: utf-32
indent: 4 indent: 4
text-width: 40 text-width: 40
#Note: setting collection probabilities too high can lead to stack overflow as
#we end up with extremely deeply nested structures
string: string:
probability: 10 probability: 10
range: {min: 1, max: 40, dist: cubic} range: {min: 1, max: 40, dist: cubic}

View file

@ -1,4 +1,6 @@
///Random YAML generator. Used to generate benchmarking inputs.
import std.conv; import std.conv;
import std.datetime; import std.datetime;
import std.math; import std.math;
@ -8,7 +10,7 @@ import std.string;
import yaml; import yaml;
immutable alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"; immutable alphabet = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
immutable digits = "0123456789"; immutable digits = "0123456789";
Node config; Node config;