Commit | Line | Data |
c4b7a40d |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Benchmark 'timethis'; |
7 | use JSON; |
8 | use File::Path 'mkpath'; |
9 | |
10 | use Text::Tradition; |
11 | use Text::Tradition::Directory; |
12 | use Test::More 'no_plan'; |
13 | |
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'; |
17 | |
18 | ## Data file for repeated benchmarks: |
19 | my $benchmark_file = 't/data/load-save-benchmark.json'; |
20 | |
21 | ## SQL file (previously dumped KiokuDB) for testing tradition directory loading: |
22 | my $load_sql = 't/data/speed_test_load.sql'; |
23 | |
24 | ## uuid to load from the above stored db: |
f84a7d06 |
25 | my $load_uuid = '2FD966F6-946D-11E1-9C51-61717D694B5D'; |
c4b7a40d |
26 | |
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. |
30 | my $git_hash = shift; |
31 | |
32 | if($git_hash) { |
33 | diag "Will save results using $git_hash as a key"; |
34 | } else { |
35 | diag "No git hash passed in, just running test"; |
36 | } |
37 | |
38 | ## Setup |
39 | mkpath('t/var') if(!-d 't/var'); |
40 | |
41 | my $tradition = Text::Tradition->new( |
42 | 'input' => 'Self', |
43 | 'file' => "t/data/${test_name}.xml" |
44 | ## smaller for testing the test! |
45 | # 'input' => 'Tabular', |
46 | # 'file' => 't/data/simple.txt', |
47 | ); |
48 | $tradition->add_stemma(dotfile => "t/data/${test_name}.dot"); |
49 | |
50 | #my $fh = File::Temp->new(); |
51 | #my $file = $fh->filename; |
52 | #$fh->close; |
53 | ## use t/var so you can look at the results after if neccessary: |
54 | |
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: |
59 | ## ?? fails |
60 | `sqlite3 $load_db < $load_sql`; |
61 | |
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}"; |
65 | |
66 | my $benchmark_data = load_benchmark($benchmark_file); |
67 | |
68 | my $test_save = sub { |
69 | unlink($save_db) if(-e $save_db); |
70 | |
71 | my $dir = Text::Tradition::Directory->new( |
72 | dsn => $save_dsn, |
73 | extra_args => { create => 1 }, |
74 | ); |
75 | ## This seems to be a required magic incantation: |
76 | my $scope = $dir->new_scope; |
77 | |
78 | ## save the tradition (with stemma) to the db: |
79 | my $uuid = $dir->save($tradition); |
80 | # print STDERR "UUID: $uuid\n"; |
81 | |
82 | }; |
83 | |
f84a7d06 |
84 | my $load_tradition; |
c4b7a40d |
85 | my $test_load = sub { |
86 | my $dir = Text::Tradition::Directory->new( |
dcfafc0e |
87 | dsn => $load_dsn, |
c4b7a40d |
88 | ); |
89 | |
90 | ## This seems to be a required magic incantation: |
91 | my $scope = $dir->new_scope; |
92 | |
f84a7d06 |
93 | $load_tradition = $dir->tradition($load_uuid); |
dcfafc0e |
94 | # print STDERR $load_tradition->name, $tradition->name, "\n"; |
c4b7a40d |
95 | }; |
96 | |
97 | my $last_benchmark = $benchmark_data->[-1]; |
dcfafc0e |
98 | |
c4b7a40d |
99 | ## Benchmark current code: |
dcfafc0e |
100 | ## Should probably run the test the same number of times as the last time it ran |
101 | ## Or compare results more sanely |
c4b7a40d |
102 | my $new_save_result = timethis(5, $test_save); |
103 | |
f84a7d06 |
104 | my $new_save = $new_save_result->[1] + $new_save_result->[2]; |
105 | my $old_save = $last_benchmark->{save_times}[1] + $last_benchmark->{save_times}[2]; |
106 | ok( $new_save < $old_save, "Saving to a Tradition Directory got faster: $new_save vs $old_save"); |
c4b7a40d |
107 | |
108 | my $new_load_result = timethis(20, $test_load); |
109 | |
f84a7d06 |
110 | my $new_load = $new_load_result->[1] + $new_load_result->[2]; |
111 | my $old_load = $last_benchmark->{load_times}[1] + $last_benchmark->{load_times}[2]; |
112 | ok($new_load < $old_load, "Loading from a Tradition Directory got faster: $new_load vs $old_load"); |
c4b7a40d |
113 | |
dcfafc0e |
114 | $test_load->(); |
f84a7d06 |
115 | isa_ok($load_tradition, 'Text::Tradition'); |
116 | ok($load_tradition->collation->as_svg()); |
dcfafc0e |
117 | |
c4b7a40d |
118 | if($git_hash) { |
119 | push(@{ $benchmark_data }, { |
120 | git_hash => $git_hash, |
dcfafc0e |
121 | load_times => [@$new_load_result], |
122 | save_times => [@$new_save_result], |
c4b7a40d |
123 | }); |
124 | |
dcfafc0e |
125 | save_benchmark($benchmark_file, $benchmark_data); |
c4b7a40d |
126 | } |
127 | |
128 | ## ----------------------------------------------------------------------------- |
129 | |
130 | sub load_benchmark { |
131 | my ($filename) = @_; |
132 | |
133 | my $loaded_data = []; |
134 | if(-e $filename) { |
135 | local $/; |
136 | open( my $fh, '<', $filename ) || die "$!"; |
137 | my $json_text = <$fh>; |
138 | $fh->close(); |
139 | $loaded_data = decode_json( $json_text ); |
140 | } else { |
141 | ## bare bones default table: |
142 | $loaded_data = [ |
143 | { |
144 | git_hash => '', |
145 | load_times => [1000, 1000, 1000, 0, 0, 5], |
dcfafc0e |
146 | save_times => [1000, 1000, 1000, 0, 0, 20], |
c4b7a40d |
147 | } |
148 | ]; |
149 | } |
150 | |
151 | return $loaded_data; |
152 | } |
153 | |
154 | sub save_benchmark { |
155 | my ($filename, $new_benchmarks) = @_; |
156 | |
dcfafc0e |
157 | my $json_text = JSON->new->utf8->allow_blessed->encode($new_benchmarks); |
c4b7a40d |
158 | |
159 | open(my $fh, '>', $filename) || die "$!"; |
160 | $fh->print($json_text); |
161 | $fh->close(); |
162 | } |