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