File: rebuild-benchmark

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (228 lines) | stat: -rwxr-xr-x 6,198 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/perl -w

use strict;
use lib qw( lib extlib );
binmode STDOUT, ":utf8";
binmode STDERR, ":utf8";

use Getopt::Long;
use LWP::UserAgent;
use HTTP::Request;
use Web::Scraper;
use Data::Dumper;
use Time::HiRes;
use MT;
use MT::Blog;

# Set default option value
my @blog_ids;
my @templates;
my $entry_id  = 0;
my $count     = 1;
my $mt_url    = '';
my $username  = 'Melody';
my $password  = 'Nelson';
my $silent    = 0;
my $usage     = 0;

GetOptions(
    'blog_id=s'  => \@blog_ids,
    'template=s' => \@templates,
    'count=i'    => \$count,
    'mt_url=s'   => \$mt_url,
    'user=s'     => \$username,
    'pass=s'     => \$password,
    'silent'     => \$silent,
    'uaage'      => \$usage,
);

# Construct MT object
my $mt = new MT
  or die "Not MT object " . MT->errstr();

# Validation of required options
if (   $usage
    || ( !$mt_url )
    || ( !$username )
    || ( !$password ) )
{
    usage();
    exit;
}

# Parse and load blog
if (@blog_ids) {
    @blog_ids = split(/,/, join(',', @blog_ids));
}
my @blogs = MT::Blog->load( { ( @blog_ids ? ( id => @blog_ids ) : () ), } )
    or die "Couldn't load blogs. [" . Dumper(@blog_ids) . "]\n";

# Parse template option
if (@templates) {
    @templates = split(/,/, join(',', @templates));
}

# Construct UserAgent object
my $ua = new LWP::UserAgent;

# Create scraper object
my $scraper = scraper {
    process "div.msg-publishing", status_publishing => 'TEXT';
    process "div.msg-error",      status_error      => 'TEXT';
    process "div.msg-success",    status_success    => 'TEXT';

    result 'status_publishing', 'status_error', 'status_success';
};

# call rebuild
for ( my $count_i = 0 ; $count_i < $count ; $count_i++ ) {
    foreach my $blog (@blogs) {
        logging( "rebuilding (" . ($count_i + 1) . ") " . $blog->name . " ...\n" );
        run_rebuild($blog);
    }
}

sub run_rebuild {
    my $blog = shift;

    my @at;
    if ( !@templates ) {
        @at = split( ',', $blog->archive_type );
        push @at, 'index';
    }
    else {
        @at = @templates;
    }

    foreach (@at) {
        my $at       = $_;
        my $archiver = $mt->publisher->archiver($at);
        next if ( !$archiver ) && ( $at ne 'index' );

        my $start = Time::HiRes::time();
        my $total = 0;
        if ($archiver) {
            if ( ( $archiver->entry_based || $archiver->date_based )
                && !$entry_id )
            {
                my $entry_class = $archiver->entry_class || 'entry';
                require MT::Entry;
                my $terms = {
                    class   => $entry_class,
                    status  => MT::Entry::RELEASE(),
                    blog_id => $blog->id,
                };
                $total = MT::Entry->count($terms);
            }
            elsif ( $archiver->category_based ) {
                require MT::Category;
                my $terms = {
                    blog_id => $blog->id,
                    class   => $archiver->category_class,
                };
                $total = MT::Category->count($terms);
            }
            elsif ( $archiver->author_based ) {
                require MT::Author;
                require MT::Entry;
                my $terms = {
                    blog_id => $blog->id,
                    status  => MT::Entry::RELEASE(),
                    class   => 'entry',
                };
                $total = MT::Author->count(
                    undef,
                    {
                        join => MT::Entry->join_on(
                            'author_id', $terms, { unique => 1 }
                        ),
                        unique => 1,
                    }
                );
            }
        }

        my $url =
            $mt_url
          . "?__mode=rebuild"
          . "&blog_id="
          . $blog->id
          . "&next=0"
          . "&offset=0"
          . "&limit=20"
          . "&entry_id="
          . "&is_new="
          . "&old_status="
          . "&old_previous="
          . "&old_next="
          . "&total="
          . $total
          . "&type="
          . $at;
        do {
            $url .= "&username=" . $username;
            $url .= "&password=" . $password;
            my $req = new HTTP::Request( GET => $url );
            my $resp = $ua->request($req);
            if ( $resp->is_success ) {
                my $res = $scraper->scrape( $resp->content() );
                if ( $res->{status_publishing} ) {
                    ( undef, $url ) =
                      $resp->content() =~ /window.location='(.*)\?(.*)'/;
                    $url = $mt_url . "?" . $url;
                }
                elsif ( $res->{status_success} ) {
                    logging( "\t" . $at . " built success." . "\n" );
                    $url = undef;
                }
                elsif ( $res->{status_error} ) {
                    logging("\t" 
                          . $at
                          . " built failed. "
                          . $res->{status_error}
                          . "\n" );
                    $url = undef;
                }
                else {
                    logging("\t" 
                          . $at
                          . " built failed.\n"
                          . $resp->content()
                          . "\n" );
                    $url = undef;
                }
            }
            else {
                logging(
                    "\t" . $at . " request failed (" . $resp->code . ")\n" );
                $url = undef;
            }
        } while ($url);

        logging( "\t  total build time:" . ( Time::HiRes::time() - $start ) . "\n" );
    }
}

sub logging {
    my ($msg) = @_;
    print $msg if !$silent;
}

sub usage {
    print STDERR << "EOT";
usage: $0
  require:
    -mt_url='URL to mt.cgi'
    -user='login account of mt'
    -pass='login password of mt'

  optional:
    -blog_id='target blog id.'
    -template='target archive type'
    -count='the count of cyclic call'
    -silent='no output any logs (1|0)'
    -usage='show this message'

* This script requires Web::Scraper. You must install from CPAN if you not installed yet.
EOT
}