6 use Benchmark 'timethis';
8 use File::Path 'mkpath';
11 use Text::Tradition::Directory;
12 use Test::More 'no_plan';
14 ## Using t/data/besoin.xml / t/data/besoin.dot as a large test example:
15 my $test_name = 'besoin';
16 # my $test_name = 'simple';
18 ## Data file for repeated benchmarks:
19 my $benchmark_file = 't/data/load-save-benchmark.json';
21 ## SQL file (previously dumped KiokuDB) for testing tradition directory loading:
22 my $load_sql = 't/data/speed_test_load.sql';
24 ## uuid to load from the above stored db:
25 my $load_uuid = '7D0AA7C0-92C2-11E1-98B2-D7BDA89F4671';
27 ## Pass the git hash to identify this performance improvement, if you
28 ## want to save the results of this run. Pass nothing to just run a test
29 ## of the current code against the previous best.
33 diag "Will save results using $git_hash as a key";
35 diag "No git hash passed in, just running test";
39 mkpath('t/var') if(!-d 't/var');
41 my $tradition = Text::Tradition->new(
43 'file' => "t/data/${test_name}.xml"
44 ## smaller for testing the test!
45 # 'input' => 'Tabular',
46 # 'file' => 't/data/simple.txt',
48 $tradition->add_stemma(dotfile => "t/data/${test_name}.dot");
50 #my $fh = File::Temp->new();
51 #my $file = $fh->filename;
53 ## use t/var so you can look at the results after if neccessary:
55 my $load_db = 't/var/speed_test_load.db';
56 unlink($load_db) if(-e $load_db);
57 my $load_dsn = "dbi:SQLite:dbname=$load_db";
58 ## Prime db from .sql file:
60 `sqlite3 $load_db < $load_sql`;
62 my $save_db = 't/var/speed_test_save.db';
63 unlink($save_db) if(-e $save_db);
64 my $save_dsn = "dbi:SQLite:dbname=${save_db}";
66 my $benchmark_data = load_benchmark($benchmark_file);
69 unlink($save_db) if(-e $save_db);
71 my $dir = Text::Tradition::Directory->new(
73 extra_args => { create => 1 },
75 ## This seems to be a required magic incantation:
76 my $scope = $dir->new_scope;
78 ## save the tradition (with stemma) to the db:
79 my $uuid = $dir->save($tradition);
80 # print STDERR "UUID: $uuid\n";
85 my $dir = Text::Tradition::Directory->new(
89 ## This seems to be a required magic incantation:
90 my $scope = $dir->new_scope;
92 my $tradition = $dir->tradition($load_uuid);
95 my $last_benchmark = $benchmark_data->[-1];
96 ## Benchmark current code:
97 my $new_save_result = timethis(5, $test_save);
99 ok($new_save_result->[1] + $new_save_result->[2] < $last_benchmark->{save_times}[1] + $last_benchmark->{save_times}[2], 'Saving to a Tradition Directory got faster');
101 my $new_load_result = timethis(20, $test_load);
103 ok($new_load_result->[1] + $new_load_result->[2] < $last_benchmark->{load_times}[1] + $last_benchmark->{load_times}[2], 'Loading from a Tradition Directory got faster');
106 push(@{ $benchmark_data }, {
107 git_hash => $git_hash,
108 load_times => $new_load_result,
109 save_times => $new_save_result,
112 save_benchmark($benchmark_data);
115 ## -----------------------------------------------------------------------------
120 my $loaded_data = [];
123 open( my $fh, '<', $filename ) || die "$!";
124 my $json_text = <$fh>;
126 $loaded_data = decode_json( $json_text );
128 ## bare bones default table:
132 load_times => [1000, 1000, 1000, 0, 0, 5],
133 save_times => [1000, 1000, 1000, 0, 0, 5],
142 my ($filename, $new_benchmarks) = @_;
144 my $json_text = encode_json($new_benchmarks);
146 open(my $fh, '>', $filename) || die "$!";
147 $fh->print($json_text);