Add Email::Template for post-registratin email sending
[scpubgit/stemmatology.git] / t / load-save-speed.t
CommitLineData
c4b7a40d 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Benchmark 'timethis';
7use JSON;
3b94fa8d 8use Sys::Hostname;
c4b7a40d 9use File::Path 'mkpath';
10
11use Text::Tradition;
12use Text::Tradition::Directory;
278698b1 13use Test::More;
14
15## Don't run this test when running make test or prove, to run it use perl -Ilib t/load-save-speed.t
16
17if($ENV{HARNESS_ACTIVE}) {
18 plan skip_all => 'Skipping performance tests under prove/make, run manually to test performance improvements';
19} else {
20 plan 'no_plan';
21}
c4b7a40d 22
23## Using t/data/besoin.xml / t/data/besoin.dot as a large test example:
24my $test_name = 'besoin';
25# my $test_name = 'simple';
26
27## Data file for repeated benchmarks:
28my $benchmark_file = 't/data/load-save-benchmark.json';
29
30## SQL file (previously dumped KiokuDB) for testing tradition directory loading:
31my $load_sql = 't/data/speed_test_load.sql';
32
33## uuid to load from the above stored db:
34my $load_uuid = '7D0AA7C0-92C2-11E1-98B2-D7BDA89F4671';
35
36## Pass the git hash to identify this performance improvement, if you
37## want to save the results of this run. Pass nothing to just run a test
38## of the current code against the previous best.
39my $git_hash = shift;
40
41if($git_hash) {
42 diag "Will save results using $git_hash as a key";
43} else {
44 diag "No git hash passed in, just running test";
45}
46
47## Setup
48mkpath('t/var') if(!-d 't/var');
49
50my $tradition = Text::Tradition->new(
51 'input' => 'Self',
52 'file' => "t/data/${test_name}.xml"
53 ## smaller for testing the test!
54# 'input' => 'Tabular',
55# 'file' => 't/data/simple.txt',
56);
57$tradition->add_stemma(dotfile => "t/data/${test_name}.dot");
58
59#my $fh = File::Temp->new();
60#my $file = $fh->filename;
61#$fh->close;
62## use t/var so you can look at the results after if neccessary:
63
64my $load_db = 't/var/speed_test_load.db';
65unlink($load_db) if(-e $load_db);
66my $load_dsn = "dbi:SQLite:dbname=$load_db";
67## Prime db from .sql file:
68## ?? fails
69`sqlite3 $load_db < $load_sql`;
70
71my $save_db = 't/var/speed_test_save.db';
72unlink($save_db) if(-e $save_db);
73my $save_dsn = "dbi:SQLite:dbname=${save_db}";
74
75my $benchmark_data = load_benchmark($benchmark_file);
76
77my $test_save = sub {
78 unlink($save_db) if(-e $save_db);
79
80 my $dir = Text::Tradition::Directory->new(
81 dsn => $save_dsn,
82 extra_args => { create => 1 },
83 );
84 ## This seems to be a required magic incantation:
85 my $scope = $dir->new_scope;
86
87 ## save the tradition (with stemma) to the db:
88 my $uuid = $dir->save($tradition);
89# print STDERR "UUID: $uuid\n";
90
91};
92
93my $test_load = sub {
94 my $dir = Text::Tradition::Directory->new(
dcfafc0e 95 dsn => $load_dsn,
c4b7a40d 96 );
97
98 ## This seems to be a required magic incantation:
99 my $scope = $dir->new_scope;
100
dcfafc0e 101 my $load_tradition = $dir->tradition($load_uuid);
102# print STDERR $load_tradition->name, $tradition->name, "\n";
c4b7a40d 103};
104
3b94fa8d 105## Find most recent benchmark info on this hostname
106my ($last_benchmark) = grep { $_->{host} eq hostname() } (reverse @{$benchmark_data});
107
108if(!$last_benchmark) {
109 diag "Can't find last benchmark for " . hostname() . ", starting again";
110 $last_benchmark = fresh_benchmark();
111}
112
dcfafc0e 113
c4b7a40d 114## Benchmark current code:
dcfafc0e 115## Should probably run the test the same number of times as the last time it ran
116## Or compare results more sanely
c4b7a40d 117my $new_save_result = timethis(5, $test_save);
118
119ok($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');
120
121my $new_load_result = timethis(20, $test_load);
122
123ok($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');
124
dcfafc0e 125$test_load->();
126
c4b7a40d 127if($git_hash) {
128 push(@{ $benchmark_data }, {
129 git_hash => $git_hash,
3b94fa8d 130 host => hostname(),
dcfafc0e 131 load_times => [@$new_load_result],
132 save_times => [@$new_save_result],
c4b7a40d 133 });
134
dcfafc0e 135 save_benchmark($benchmark_file, $benchmark_data);
c4b7a40d 136}
137
138## -----------------------------------------------------------------------------
139
140sub load_benchmark {
141 my ($filename) = @_;
142
143 my $loaded_data = [];
144 if(-e $filename) {
145 local $/;
146 open( my $fh, '<', $filename ) || die "$!";
147 my $json_text = <$fh>;
148 $fh->close();
149 $loaded_data = decode_json( $json_text );
150 } else {
151 ## bare bones default table:
3b94fa8d 152 $loaded_data = fresh_benchmark();
c4b7a40d 153 }
154
155 return $loaded_data;
156}
157
3b94fa8d 158sub fresh_benchmark {
159 return
160 [
161 {
162 git_hash => '',
163 host => hostname(),
164 load_times => [1000, 1000, 1000, 0, 0, 5],
165 save_times => [1000, 1000, 1000, 0, 0, 20],
166 }
167 ];
168}
169
c4b7a40d 170sub save_benchmark {
171 my ($filename, $new_benchmarks) = @_;
172
dcfafc0e 173 my $json_text = JSON->new->utf8->allow_blessed->encode($new_benchmarks);
c4b7a40d 174
175 open(my $fh, '>', $filename) || die "$!";
176 $fh->print($json_text);
177 $fh->close();
178}