Skip to content

Instantly share code, notes, and snippets.

@kuniyoshi
kuniyoshi / access_log.R
Created February 22, 2014 08:14
Calculate Spearman's cor.
portal <- read.table("portal.log", sep="¥t", header=TRUE)
portal.sub <- portal[sample(length(portal[,1]), 9000), ]
ip.order <- data.frame(median=tapply(portal.sub$us, portal.sub$ip, median))
ip.order$order <- order(ip.order)
portal.sub$ip.o <- ip.order[portal.sub$ip, 2]
with(portal.sub, cor(us, ip.o, method="spearman"))
# 0.5039202
@kuniyoshi
kuniyoshi / bench.erl
Created February 26, 2014 17:09
There is no length/1 optimization. Capture length/1 instead of write code every time.
-module(bench).
-export([run/0]).
-define(SEED, {1393,433335,396007}).
-define(LENGTH, trunc(math:pow(2, 14))).
-define(TIMES, 3).
-include_lib("eunit/include/eunit.hrl").
gen() ->
random:seed(?SEED),
lists:map(fun(_) -> random:uniform() end, lists:duplicate(?LENGTH, 0)).
@kuniyoshi
kuniyoshi / sample_signals.erl
Created March 2, 2014 15:03
A chirp sample. The sound's frequency changes from 2500 Hz to 1500 Hz, and fade out it.
gen(chirp, Options) ->
FirstFrequency = proplists:get_value(first_frequency, Options),
LastFrequency = proplists:get_value(last_frequency, Options),
SamplingFrequency = proplists:get_value(sampling_frequency, Options),
Count = proplists:get_value(count, Options),
Samples = lists:map(fun(I) ->
Frequency = FirstFrequency + (LastFrequency - FirstFrequency) / Count * I,
math:sin(2 * math:pi() * Frequency / SamplingFrequency * I)
end,
lists:seq(1, Count)),
@kuniyoshi
kuniyoshi / rstats.erl
Created March 4, 2014 15:28
A plotter.
-module(rstats).
-export([plot/1]).
-define(TMP_FILE, "/tmp/erlstats.data").
-define(R_FILE, "/tmp/erlstats.R").
plot(Points) ->
ok = file:write_file(?TMP_FILE,
[io_lib:format("~p~n", [X]) || X <- Points],
[write]),
Rprogram = ["erl <- read.table(\"", ?TMP_FILE, "\")", "\n",
@kuniyoshi
kuniyoshi / fir.erl
Created March 6, 2014 15:58
a convolution program.
-module(fir).
-export([convolute/2]).
convolute_acc(F, G, Acc) when length(F) =:= 2 * length(G) - 1 ->
lists:reverse(Acc);
convolute_acc(F, G, Acc) ->
V = lists:sum(lists:map(fun({A, B}) -> A * B end,
lists:zip(lists:sublist(F, length(G)), G))),
convolute_acc(tl(F), G, [V | Acc]).
@kuniyoshi
kuniyoshi / iir.erl
Created March 11, 2014 16:13
An IIR filter.
-module(iir).
-export([gen_filter/2, filter/3]).
-export([loop/4, aggregate_filtered/2, apply_filter/3]).
-compile({nowarn_unused_function, [aggregate/1,
aggregate_buffered_acc/3,
aggregate_buffered/2,
aggregate_filtered_acc/3]}).
-define(PI, math:pi()).
-include_lib("eunit/include/eunit.hrl").
@kuniyoshi
kuniyoshi / lcm.erl
Last active August 29, 2015 13:58
Count many labels by Lossy Count Method.
-module(lcm).
-export([start_loop/1, start_loop/0]).
-export([loop/5]).
cut_off_bucket(Counter, BucketSize) ->
Counter2 = lists:filter(fun({_Label, {Count, Bucket}}) ->
Count =< BucketSize - Bucket
end,
Counter),
Counter2.
@kuniyoshi
kuniyoshi / metrics_http.erl
Created April 20, 2014 16:11
Measure HTTP Response Time, and store it.
-module(metrics_http).
-export([start_loop/1, stop/0]).
-export([loop/1]).
-export([measure_tat/1]).
-export([add/1, update/2]).
-export([loop_query/1, start_querier/1, start_querier/0]).
-include_lib("eunit/include/eunit.hrl").
-define(CONNECTION_TIMEOUT, 10 * 1000).
-define(TIMEOUT, 10 * 1000).
-define(USER_AGENT, "MetricsHttp/0.1").
@kuniyoshi
kuniyoshi / slowcat
Created June 16, 2014 13:32
This script prints STDIN, or lines of files which are ARGS. I have a situation that slow print down to STDOUT to see what were print. I saw these lines to check does it almost good. But some times, STDOUT is too fast to see it, thus, this script slows STDOUT down.
#!/usr/bin/perl -s
use utf8;
use strict;
use warnings;
use Time::HiRes qw( sleep );
use constant LINES_PER_SECOND => 100;
our $lps ||= LINES_PER_SECOND;
our $h;
@kuniyoshi
kuniyoshi / README.md
Created July 24, 2014 16:17
HttpPipe

HttpPipe

SYNOPSIS

  [root@receiver]# curl -v -O https://example.com/upload/
  ...
 Location: https://example.com/upload/XXXXXX