2011-10-21 22:24:29 +00:00
|
|
|
|
2011-10-22 14:20:06 +00:00
|
|
|
///Random YAML generator. Used to generate benchmarking inputs.
|
|
|
|
|
2014-08-02 21:29:55 +00:00
|
|
|
import std.algorithm;
|
2011-10-21 22:24:29 +00:00
|
|
|
import std.conv;
|
|
|
|
import std.datetime;
|
|
|
|
import std.math;
|
|
|
|
import std.random;
|
|
|
|
import std.stdio;
|
|
|
|
import std.string;
|
2018-04-27 03:17:09 +00:00
|
|
|
import dyaml;
|
2011-10-21 22:24:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
Node config;
|
|
|
|
Node function(bool)[string] generators;
|
|
|
|
auto typesScalar = ["string", "int", "float", "bool", "timestamp", "binary"];
|
|
|
|
auto typesScalarKey = ["string", "int", "float", "timestamp"];
|
|
|
|
auto typesCollection = ["map","omap", "pairs", "seq", "set"];
|
|
|
|
ulong minNodesDocument;
|
|
|
|
ulong totalNodes;
|
|
|
|
|
|
|
|
static this()
|
|
|
|
{
|
|
|
|
generators["string"] = &genString;
|
|
|
|
generators["int"] = &genInt;
|
|
|
|
generators["float"] = &genFloat;
|
|
|
|
generators["bool"] = &genBool;
|
|
|
|
generators["timestamp"] = &genTimestamp;
|
|
|
|
generators["binary"] = &genBinary;
|
|
|
|
generators["map"] = &genMap;
|
|
|
|
generators["omap"] = &genOmap;
|
|
|
|
generators["pairs"] = &genPairs;
|
|
|
|
generators["seq"] = &genSeq;
|
|
|
|
generators["set"] = &genSet;
|
|
|
|
}
|
|
|
|
|
2014-08-01 15:09:05 +00:00
|
|
|
real randomNormalized(const string distribution = "linear")
|
2011-10-21 22:24:29 +00:00
|
|
|
{
|
|
|
|
auto generator = Random(unpredictableSeed());
|
|
|
|
const r = uniform!"[]"(0.0L, 1.0L, generator);
|
|
|
|
switch(distribution)
|
|
|
|
{
|
|
|
|
case "linear":
|
|
|
|
return r;
|
|
|
|
case "quadratic":
|
|
|
|
return r * r;
|
|
|
|
case "cubic":
|
|
|
|
return r * r * r;
|
|
|
|
default:
|
2014-08-01 15:09:05 +00:00
|
|
|
writeln("Unknown random distribution: ", distribution,
|
2011-10-21 22:24:29 +00:00
|
|
|
", falling back to linear");
|
|
|
|
return randomNormalized("linear");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-01 15:09:05 +00:00
|
|
|
long randomLong(const long min, const long max, const string distribution = "linear")
|
2011-10-21 22:24:29 +00:00
|
|
|
{
|
|
|
|
return min + cast(long)round((max - min) * randomNormalized(distribution));
|
|
|
|
}
|
|
|
|
|
2014-08-01 15:09:05 +00:00
|
|
|
real randomReal(const real min, const real max, const string distribution = "linear")
|
2011-10-21 22:24:29 +00:00
|
|
|
{
|
|
|
|
return min + (max - min) * randomNormalized(distribution);
|
|
|
|
}
|
|
|
|
|
2014-08-02 21:29:55 +00:00
|
|
|
dchar randomChar(const dstring chars)
|
2011-10-21 22:24:29 +00:00
|
|
|
{
|
|
|
|
return chars[randomLong(0, chars.length - 1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
string randomType(string[] types)
|
|
|
|
{
|
|
|
|
auto probabilities = new uint[types.length];
|
|
|
|
foreach(index, type; types)
|
|
|
|
{
|
2011-10-22 15:06:32 +00:00
|
|
|
probabilities[index] = config[type]["probability"].as!uint;
|
2011-10-21 22:24:29 +00:00
|
|
|
}
|
|
|
|
return types[dice(probabilities)];
|
|
|
|
}
|
|
|
|
|
|
|
|
Node genString(bool root = false)
|
|
|
|
{
|
|
|
|
auto range = config["string"]["range"];
|
|
|
|
|
2014-08-02 21:29:55 +00:00
|
|
|
auto alphabet = config["string"]["alphabet"].as!dstring;
|
|
|
|
|
2014-08-01 15:09:05 +00:00
|
|
|
const chars = randomLong(range["min"].as!uint, range["max"].as!uint,
|
2011-10-22 15:06:32 +00:00
|
|
|
range["dist"].as!string);
|
2011-10-21 22:24:29 +00:00
|
|
|
|
2014-08-02 21:29:55 +00:00
|
|
|
dchar[] result = new dchar[chars];
|
2011-10-21 22:24:29 +00:00
|
|
|
result[0] = randomChar(alphabet);
|
|
|
|
foreach(i; 1 .. chars)
|
|
|
|
{
|
2014-08-02 21:29:55 +00:00
|
|
|
result[i] = randomChar(alphabet);
|
2011-10-21 22:24:29 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 21:29:55 +00:00
|
|
|
return Node(result.to!string);
|
2011-10-21 22:24:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Node genInt(bool root = false)
|
|
|
|
{
|
|
|
|
auto range = config["int"]["range"];
|
|
|
|
|
2014-08-01 15:09:05 +00:00
|
|
|
const result = randomLong(range["min"].as!int, range["max"].as!int,
|
2011-10-22 15:06:32 +00:00
|
|
|
range["dist"].as!string);
|
2011-10-21 22:24:29 +00:00
|
|
|
|
|
|
|
return Node(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node genFloat(bool root = false)
|
|
|
|
{
|
|
|
|
auto range = config["float"]["range"];
|
|
|
|
|
2014-08-01 15:09:05 +00:00
|
|
|
const result = randomReal(range["min"].as!real, range["max"].as!real,
|
2011-10-22 15:06:32 +00:00
|
|
|
range["dist"].as!string);
|
2011-10-21 22:24:29 +00:00
|
|
|
|
|
|
|
return Node(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node genBool(bool root = false)
|
|
|
|
{
|
|
|
|
return Node([true, false][randomLong(0, 1)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node genTimestamp(bool root = false)
|
|
|
|
{
|
|
|
|
auto range = config["timestamp"]["range"];
|
|
|
|
|
2014-08-01 15:09:05 +00:00
|
|
|
auto hnsecs = randomLong(range["min"].as!ulong, range["max"].as!ulong,
|
2011-10-22 15:06:32 +00:00
|
|
|
range["dist"].as!string);
|
2011-10-21 22:24:29 +00:00
|
|
|
|
2011-10-22 15:06:32 +00:00
|
|
|
if(randomNormalized() <= config["timestamp"]["round-chance"].as!real)
|
2011-10-21 22:24:29 +00:00
|
|
|
{
|
|
|
|
hnsecs -= hnsecs % 10000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Node(SysTime(hnsecs));
|
|
|
|
}
|
|
|
|
|
|
|
|
Node genBinary(bool root = false)
|
|
|
|
{
|
|
|
|
auto range = config["binary"]["range"];
|
|
|
|
|
2014-08-01 15:09:05 +00:00
|
|
|
const bytes = randomLong(range["min"].as!uint, range["max"].as!uint,
|
2011-10-22 15:06:32 +00:00
|
|
|
range["dist"].as!string);
|
2011-10-21 22:24:29 +00:00
|
|
|
|
|
|
|
ubyte[] result = new ubyte[bytes];
|
|
|
|
foreach(i; 0 .. bytes)
|
|
|
|
{
|
|
|
|
result[i] = cast(ubyte)randomLong(0, 255);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Node(result);
|
|
|
|
}
|
|
|
|
|
2014-08-01 15:09:05 +00:00
|
|
|
Node nodes(const bool root, Node range, const string tag, const bool set = false)
|
2011-10-21 22:24:29 +00:00
|
|
|
{
|
2011-10-23 14:10:27 +00:00
|
|
|
auto types = config["collection-keys"].as!bool ? typesCollection : [];
|
|
|
|
types ~= (set ? typesScalarKey : typesScalar);
|
2011-10-21 22:24:29 +00:00
|
|
|
|
|
|
|
Node[] nodes;
|
|
|
|
if(root)
|
|
|
|
{
|
|
|
|
while(!(totalNodes >= minNodesDocument))
|
|
|
|
{
|
2014-08-02 21:28:45 +00:00
|
|
|
nodes.assumeSafeAppend;
|
2011-10-21 22:24:29 +00:00
|
|
|
nodes ~= generateNode(randomType(types));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-01 15:09:05 +00:00
|
|
|
const elems = randomLong(range["min"].as!uint, range["max"].as!uint,
|
2011-10-22 15:06:32 +00:00
|
|
|
range["dist"].as!string);
|
2011-10-21 22:24:29 +00:00
|
|
|
|
|
|
|
nodes = new Node[elems];
|
|
|
|
foreach(i; 0 .. elems)
|
|
|
|
{
|
|
|
|
nodes[i] = generateNode(randomType(types));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Node(nodes, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node genSeq(bool root = false)
|
|
|
|
{
|
|
|
|
return nodes(root, config["seq"]["range"], "tag:yaml.org,2002:seq");
|
|
|
|
}
|
|
|
|
|
|
|
|
Node genSet(bool root = false)
|
|
|
|
{
|
|
|
|
return nodes(root, config["seq"]["range"], "tag:yaml.org,2002:set", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node pairs(bool root, bool complex, Node range, string tag)
|
|
|
|
{
|
|
|
|
Node[] keys, values;
|
|
|
|
|
|
|
|
if(root)
|
|
|
|
{
|
|
|
|
while(!(totalNodes >= minNodesDocument))
|
|
|
|
{
|
2014-08-02 21:28:33 +00:00
|
|
|
const key = generateNode(randomType(typesScalarKey ~ (complex ? typesCollection : [])));
|
|
|
|
// Maps can't contain duplicate keys
|
|
|
|
if(tag.endsWith("map") && keys.canFind(key)) { continue; }
|
|
|
|
keys.assumeSafeAppend;
|
|
|
|
values.assumeSafeAppend;
|
|
|
|
keys ~= key;
|
2011-10-21 22:24:29 +00:00
|
|
|
values ~= generateNode(randomType(typesScalar ~ typesCollection));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-01 15:09:05 +00:00
|
|
|
const pairs = randomLong(range["min"].as!uint, range["max"].as!uint,
|
2011-10-22 15:06:32 +00:00
|
|
|
range["dist"].as!string);
|
2011-10-21 22:24:29 +00:00
|
|
|
|
|
|
|
keys = new Node[pairs];
|
|
|
|
values = new Node[pairs];
|
2014-08-02 21:28:33 +00:00
|
|
|
outer: foreach(i; 0 .. pairs)
|
2011-10-21 22:24:29 +00:00
|
|
|
{
|
2014-08-02 21:28:33 +00:00
|
|
|
auto key = generateNode(randomType(typesScalarKey ~ (complex ? typesCollection : [])));
|
|
|
|
// Maps can't contain duplicate keys
|
2018-04-27 03:17:09 +00:00
|
|
|
while(tag.endsWith("map") && keys[0 .. i].canFind(key))
|
2014-08-02 21:28:33 +00:00
|
|
|
{
|
|
|
|
key = generateNode(randomType(typesScalarKey ~ (complex ? typesCollection : [])));
|
|
|
|
}
|
|
|
|
keys[i] = key;
|
2011-10-21 22:24:29 +00:00
|
|
|
values[i] = generateNode(randomType(typesScalar ~ typesCollection));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Node(keys, values, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node genMap(bool root = false)
|
|
|
|
{
|
|
|
|
Node range = config["map"]["range"];
|
2011-10-22 15:06:32 +00:00
|
|
|
const complex = config["complex-keys"].as!bool;
|
2011-10-21 22:24:29 +00:00
|
|
|
|
|
|
|
return pairs(root, complex, range, "tag:yaml.org,2002:map");
|
|
|
|
}
|
|
|
|
|
|
|
|
Node genOmap(bool root = false)
|
|
|
|
{
|
|
|
|
Node range = config["omap"]["range"];
|
2011-10-22 15:06:32 +00:00
|
|
|
const complex = config["complex-keys"].as!bool;
|
2011-10-21 22:24:29 +00:00
|
|
|
|
|
|
|
return pairs(root, complex, range, "tag:yaml.org,2002:omap");
|
|
|
|
}
|
|
|
|
|
|
|
|
Node genPairs(bool root = false)
|
|
|
|
{
|
|
|
|
Node range = config["pairs"]["range"];
|
2011-10-22 15:06:32 +00:00
|
|
|
const complex = config["complex-keys"].as!bool;
|
2011-10-21 22:24:29 +00:00
|
|
|
|
|
|
|
return pairs(root, complex, range, "tag:yaml.org,2002:pairs");
|
|
|
|
}
|
|
|
|
|
2014-08-01 15:09:05 +00:00
|
|
|
Node generateNode(const string type, bool root = false)
|
2011-10-21 22:24:29 +00:00
|
|
|
{
|
|
|
|
++totalNodes;
|
|
|
|
return generators[type](root);
|
|
|
|
}
|
|
|
|
|
2014-08-01 15:09:05 +00:00
|
|
|
Node[] generate(const string configFileName)
|
2011-10-21 22:24:29 +00:00
|
|
|
{
|
2018-05-31 07:02:21 +00:00
|
|
|
config = Loader.fromFile(configFileName).load();
|
2011-10-21 22:24:29 +00:00
|
|
|
|
2011-10-22 15:06:32 +00:00
|
|
|
minNodesDocument = config["min-nodes-per-document"].as!long;
|
2011-10-21 22:24:29 +00:00
|
|
|
|
|
|
|
Node[] result;
|
2011-10-22 15:06:32 +00:00
|
|
|
foreach(i; 0 .. config["documents"].as!uint)
|
2011-10-21 22:24:29 +00:00
|
|
|
{
|
2011-10-22 15:06:32 +00:00
|
|
|
result ~= generateNode(config["root-type"].as!string, true);
|
2011-10-21 22:24:29 +00:00
|
|
|
totalNodes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void main(string[] args)
|
|
|
|
{
|
|
|
|
//Help message.
|
|
|
|
if(args.length == 1)
|
|
|
|
{
|
|
|
|
writeln("Usage: yaml_gen FILE [CONFIG_FILE]\n");
|
|
|
|
writeln("Generates a random YAML file and writes it to FILE.");
|
|
|
|
writeln("If provided, CONFIG_FILE overrides the default config file.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string configFile = args.length >= 3 ? args[2] : "config.yaml";
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
//Generate and dump the nodes.
|
|
|
|
Node[] generated = generate(configFile);
|
|
|
|
|
2018-06-22 03:59:10 +00:00
|
|
|
auto dumper = dumper(File(args[1], "w").lockingTextWriter);
|
2014-08-01 15:09:05 +00:00
|
|
|
auto encoding = config["encoding"];
|
|
|
|
|
2011-10-22 15:06:32 +00:00
|
|
|
dumper.indent = config["indent"].as!uint;
|
|
|
|
dumper.textWidth = config["text-width"].as!uint;
|
2018-06-22 03:59:10 +00:00
|
|
|
switch(encoding.as!string)
|
|
|
|
{
|
|
|
|
case "utf-16": dumper.dump!wchar(generated); break;
|
|
|
|
case "utf-32": dumper.dump!dchar(generated); break;
|
|
|
|
default: dumper.dump!char(generated); break;
|
|
|
|
}
|
2011-10-21 22:24:29 +00:00
|
|
|
}
|
|
|
|
catch(YAMLException e)
|
|
|
|
{
|
|
|
|
writeln("ERROR: ", e.msg);
|
|
|
|
}
|
|
|
|
}
|