Skip to content

Instantly share code, notes, and snippets.

@niczero
niczero / transpose_bm.pl
Created March 31, 2017 12:57
Benchmarking matrix transposition
perl -MMojo::Base=strict -Mojo -MMojar::Config -E'$a = Mojar::Config->load(q{matrix.conf})->{matrix}; my $width = @{$$a[0]}; my $height = @$a; n { my @cols = map { [map undef, 1 .. $height] } 1 .. $width; for (my $j = 0; $j < $height; ++$j) { for (my $i = 0; $i < $width; ++$i) { $cols[$i][$j] = $$a[$j][$i] } } } 1000'
# 18.3393 wallclock secs (18.31 usr + 0.01 sys = 18.32 CPU) @ 54.59/s (n=1000)
perl -MMojo::Base=strict -Mojo -MMojar::Config -E'$a = Mojar::Config->load(q{matrix.conf})->{matrix}; my $width = @{$$a[0]}; my $height = @$a; n { my @cols; push @cols, [] for 1 .. $width; for (my $i = 0; $i < $width; ++$i) { for (my $j = 0; $j < $height; ++$j) { push @{$cols[$i]}, $$a[$j][$i] } } } 1000'
# 17.6024 wallclock secs (17.58 usr + 0.00 sys = 17.58 CPU) @ 56.88/s (n=1000)
perl -MMojo::Base=strict -Mojo -MMojar::Config -E'$a = Mojar::Config->load(q{matrix.conf})->{matrix}; my $width = @{$$a[0]}; my $height = @$a; n { my @cols = map { my $i = $_; [ map $$a[$_][$i], 0 .. $height - 1] } 0 .. $width - 1 }
@niczero
niczero / fwp.sql
Last active April 10, 2017 09:13
Group into 4-week periods (in MySQL)
SET @day = CURDATE();
SELECT FLOOR(DATEDIFF(DATE_SUB(@day, INTERVAL DAYOFWEEK(@day) DAY), '1970-01-01') / 28) AS FWP
-- 1970-01-04 is the start of FWP 0
SELECT FLOOR((TO_DAYS(@day) - TO_DAYS('1970-01-04') + 0.01) / 28) AS FWP
@niczero
niczero / mysql_quiesce
Created February 1, 2017 13:23
Quiesce a MySQL database (eg for hot rsync)
perl -MMojo::Base=strict -MMojo::Util=spurt
-MMojar::Mysql::Connector=cnf,/home/me/.mysql/super_production-02.cnf
-E'my $d = Mojar::Mysql::Connector->new->connection;
$d->do(q{FLUSH LOGS});
my $unquiesce = $d->quiesce;
spurt $$ => q{/tmp/quiesced};
say q{Quiesced!};
while (-f q{/tmp/quiesced} and $d->ping) {
say join q{ | }, $d->selectrow_array(q{SHOW MASTER STATUS});
sleep 30
@niczero
niczero / metacpan-source-print.css
Last active January 20, 2017 12:27
Style overrides for clean sourcecode printouts on http://metacpan.org
// Omits most clutter, but you still need to omit pp first and last
@media print {
ul.nav-list, ul.nav, div.col-md-4, div.footer, div.hidden-phone {
display: none;
}
div.content {
margin-left: 0;
padding-left: 0;
}
@niczero
niczero / decode_utf8.pl
Last active November 22, 2016 17:11
Translating utf8 byte strings
# Steal bits, but don't take at face value
# Originally stolen from https://gist.github.com/nicwolff/1484073
sub _decode {
my ($self, $in) = @_;
my $log = $self->app->log;
my $out = '';
use bytes; # Who said C was dead?
my ($i, $length) = (0, length $in);
while ($i < $length) {
my $b = substr $in, $i, 1;
@niczero
niczero / get_mojolicious_plugins.pl
Last active August 22, 2016 15:05
Dump all Mojolicious plugins from CPAN for analysis
#!/usr/bin/env perl
use strict;
use warnings;
use 5.10.1;
# http://www.cpan.org/modules/02packages.details.txt.gz
my $module_list_filename = '02packages.details.txt';
my @modules;
open my $fh, '<', $module_list_filename or die "Failed to open module list: $!";
@niczero
niczero / finish_logging.pl
Created June 29, 2016 14:10
Attach code to the worker shutdown
#!/usr/bin/env perl
use Mojolicious::Lite;
app->config(hypnotoad => {
accepts => 1,
clients => 1,
listen => ['http://*:8818'],
requests => 1,
workers => 1
});
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mojo::IOLoop;
get '/' => sub {
my $c = shift;
my $log = $c->app->log;
Mojo::IOLoop->timer(1 => sub { $log->debug('one') });

v0.38

Clean start with 2000 jobs
Enqueued 2000 jobs in 23.1921298503876 seconds (86.236/s)
8900 will finish 200 jobs
8900 finished 200 jobs in 20.0055611133575 seconds (9.997/s)
8901 will finish 200 jobs
8901 finished 200 jobs in 20.0034348964691 seconds (9.998/s)
8895 has started 2 workers
2 workers finished 200 jobs each in 20.1112771034241 seconds (19.889/s)
@niczero
niczero / rebus-minion.sh
Created June 10, 2016 13:21 — forked from mrenvoize/rebus-minion.sh
init.d script for starting multiple minion workers (Mojolicious)
#!/bin/bash
### BEGIN INIT INFO
# Provides: rebus-minion
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Provides Minion Workers for rebus:list jobs
# Description: This script with start/stop minion worker