oinume journal

Scratchpad of what I learned

Detecting duplicated code in Golang with CPD and Jenkins

CPD (Copy Paste Detecor)

Detecting duplicated code is a good way to make source code clean. Since I couldn't find how to detect duplicated code in Golang, I sent a pull request that CPD(Copy Paste Detector) is able to accept Golang. CPD is a software included in PMD.

And there is a useful Jenkins plugin, DRY. It visualizes how much duplicated code exist. I'll show you how to make it visible with Jenkins.

Setting up Jenkins

Use docker-toolbox to shortcut installing jenkins. After installing docker-toolbox, you just type following command.

$ docker run -p 8080:8080 jenkins

And then web can access to http://192.168.99.100:8080/ and you'll see first Jenkins page.

Installing PMD Plugin

  • Click "Manage Jenkins" on a top page
  • Click "Manage Plugins"
  • Click "Available" Tab
  • Select "Duplicate Code Scanner Plug-in"
  • Click "Download now and install after restart"
  • Jenkins will be restarted after plugin installed

Create a job to detect duplicated code

We'll use terraform repository as an example.

  • Click "create new jobs" on a top page
  • Select "Freestyle project" and type name for your job.
  • Click Add build step and select "Execute shell" and paste following shell commands
export PMD=pmd-bin-5.3.3
if [ ! -d $PMD ]; then
  curl -o $PMD.zip -L -O http://downloads.sourceforge.net/project/pmd/pmd/5.3.3/pmd-bin-5.3.3.zip\?r\=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fpmd%2Ffiles%2Fpmd%2F5.3.3%2F\&ts\=1440850447\&use_mirror\=jaist
  [ -e $PMD.zip ] && unzip $PMD.zip
fi

[ ! -d terraform ] && git clone https://github.com/hashicorp/terraform.git
   
$PMD/bin/run.sh cpd --minimum-tokens 100 --files terraform --language go --format xml > cpd.xml || echo
  • Click "Add post-build action" on "Post-build Actions" and select "Publish duplicate code analysis results"
    • Duplicate code results: cpd.xml
    • High priority threshold: 50 (default)
    • Normal priority threshold: 25 (default)
  • Save the job and run it.
  • We can see a link "Duplicate Code" on the job page

Duplicate Code View Jenkins DRY Plugin

Duplicated code of terraform Duplicated code of terraform

Options of CPD

  • --minimum-tokens: The minimum token length which should be reported as a duplicate. I recommend 70 - 100.
  • --files: a directory to check duplication.
  • --language: a programming language.
  • --format: xml or plain

For further information, type following.

pmd-bin-5.3.3/bin/run.sh cpd -h

Finally

We can detect duplicated code with CPD and Jenkins.

YAPC::Asia 2015に行ってきた(2日目)

前夜祭、1日目のエントリ

oinume.hatenablog.com oinume.hatenablog.com

ちなみにトークはYouTubeにアップロードされる予定だそうなので、正座して待ちましょう。

実はホットでオープンな Microsoft Azure

@myfinderさん。前夜祭で会った時に少しだけ話を聞いていて、面白そうなので聞いてみることにした。自分はもっぱらAWSしか使ってないけど、データのストリーム処理のところは使えそうだなぁと思った。その辺は自分たちのチームで開発しているのではなく、社内の別のチームに丸投げしているのだけど、かなり便利そうなのでもう自分たちでやるのもありなのでは?と感じた。

Azure

  • IaaS/PaasS/SaaS
  • 2014/02に日本データセンター開設
  • たくさんサービスある(500+)
  • リージョンたくさん
    • 日本だけで2箇所!

いけてるサービス

  • データ解析サービスの組み合わせ
  • Azure Stream Analytics with Event Hubs (リアルタイムデータ処理&分析)

Azure Stream Analytics with Event Hubs

  • Azure Event Hubs
    • 1500万req/sぐらいさばける!!(あと4倍ぐらいいけるのでは?)
  • Stream Analytics リアルタイムデータ処理
    • SQLライクに処理できる

Power BI Dashboard

http://qiita.com/myfinder/items/88cdad77c7472a02db61

Azure Machine Learning

Azureの下回りの話

  • コンテナ搬入Video
  • Catapult = 汎用サーバー+専用ボード

IaaSのモデル

  • Compute
  • Network
  • Storage
  • リソースグループ - 複数の要素をまとめるもの

Software Designed Network

  • ユーザーが仮想ネットワークを作ると何が起こるのか
  • NVGRE
  • Internet -> Azure SLB MUX(LoadBalancer) -> Azure SDN Stack(VFP)
  • 「NVGREのすべて」というMSの人が書いた本がある

Azure Service Fabric

質問

Adventures in refactoring

Ben Lavenderさん。

  • work at GitHub on internal tools

Refactoringとは

  • (なるべく)振る舞いをかえないでコードを変えること

  • consistency

  • DRY

Measuring Success refactoring

  • 振る舞いをかえない
  • ソースの行数が減っていること
  • テストカバレッジが改善されていること
  • パフォーマンスの改善

Reason to refactoring

  • 開発者が嬉しい - Developer Happiness
  • パフォーマンスがよくなる
  • 将来に対しての自信になる Gain confidence for Future Work (AKA paying off technical debt)
  • Developer education

technique

  • go fmt
  • types for verbs
  • Add useful abstractions
pull.branch_valid?
pull.branch_exists?
 ↓
pull.branch.valid?
pull.branch.exists?
  • Remove unused abstractions
  • deprecate :fetch, "Migrate" to fetch_with_limit plz"
    • CIはコケる
    • 本番では動く
  • Backwards compatible design

Write tools

  • Backscatter
    • back_scatter 72をコードの中に入れると、どこからcallされているかが別の画面で見れるようになる
    • def owners_team ; backscatter_deprecate_method ; end
  • science
science "widgets.loading" do |e|
  e.use {}
  e.try {}
end

Complaining

  • Really good search/replace
  • Extract resource
  • Language facilities
    • Java @deperecated
    • Ruby
    • compile-time deprecation sucks

わからなかった単語

  • generally speaking
  • criteria

questions

  • Sometimes refactoring is boring but it is necessary, so my question is, Is there a commendation system for refactoring at Github
    • emojiとかで称えてるよ。praise: 褒め称える

ソーシャルゲームにおける AWS 移行事例

RDSへの移行

  • connect時にタイムゾーン設定
  • rebootした時にfailoverさせる機能があるので、検証で使った
  • レプリ時にNOW() 使ってるとタイムゾーンずれちゃう問題
  • インポートだけで209分
    • 手順を工夫して90分

ElastiCache

  • 60秒ぐらいの処理でフェイルオーバーしてしまう

Go Debugging , Profiling, and Optimization

@bradfitzさん。memcachedの開発者でありMogileFSとかDanga::Socketとか有名なソフトウェアを作っている。今はGoogleでGoの開発チームで働いている。せっかくのトークだったのに途中でアラートメールが来て半分以上聞けなかったのが残念だった。Goのオブジェクトのメモリ割り当ての話とか、かなり深いところまでつっこんで話していたので、スライドをあとで熟読したい。

パフォーマンス

  • benchmark: b.ReportAllocs()
  • benchstat っていうコマンドもある
  • import (_ "net/http") でインポートして http://localhost/debug/pprofでアクセスするとプロファイリングできる?

質問

  • プロファイリングの結果をGUIで見れる?

最後に

今日は@kyannyさんと話す機会があって(というか一方的に話しかけただけなんだけど)、Quipperの国際展開(フィリピン、インドネシア、メキシコ)や現地のエンジニアとどういう風に開発しているかという話が聞けて嬉しかった。

YAPCには2006年からほぼ毎年行っているのだけど、YAPCで会って仲良くなって飲みに行くぐらいの仲になったり、ここに来ると純粋にもっと開発したくなる気持ちになるので、今年で(JPA主催の)YAPCは最後になってしまうのはとても残念。でもYAPCがあるから普段話さない人とも話すことがあったし、得られたことがとてもたくさんあったので本当に運営の牧さんやJPAのみなさんには感謝しています。今まで本当にありがとうございました!!

YAPC::Asia 2015に行ってきた(1日目)

1日目感想

遅刻してしまいLarry Wallの話が聞けなかったのが一番悔やまれるところだったけど、他の聞きたいトークは全部聞けたし、前夜祭で初めて話したnekogataさんのトークがすごく良くて、さらにその後懇親会でも話せたのですごく嬉しかった。YAPCは毎年何かしら新しい人との出会いがあって、今年で最後になってしまうというのが本当に悲しい。

あとDMM英会話を最近始めた成果なのか、Electronの発表をしていた人の英語は80%ぐらい聞き取ることができた。たぶんすごく丁寧に喋っていた気がするんだけど、今までは英語のトークはほとんど内容が理解できてなかったので少しは成長したのかなぁと思う。

Effective ES6

@teppeis さん発表。Web+DB Pressの特集でもあったES6の話。JavaScriptがやっとやっと、、、まともな言語になってきた!という印象。これで様々なバッドノウハウベストプラクティスが不要になるのは本当に喜ぶべきことだと思う。

あと、JavaScriptってハッシュにメソッド足していくのとかはPerlに似ているなぁとずっと思ってた。

JavaScriptの落とし穴

  • Prototype inheritance
    • クラスベースの継承じゃない
  • new, this
    • new忘れるとつらい
  • variable scope (function scope)

  • Effective

  • Best practice
  • 開眼JavaScript

ECMAScript 6

  • 2015/6/17にリリースされた
  • こんな風にクラスが書ける
classs Person {
  constructor(name) {
  }
  greet() {
  }
}

ES6のサポート状況

  • Safari9
  • IE11はサポートしてない

Transpiler and polyfill

  • ES6のソースをES5 or ES3に変換する
  • ES6 Polyfill
    • ES6の機能をJavaScript(ES5)で実装したもの

Babel

  • 最も互換性が高いTranspiler(71%)
  • polyfill library搭載 (core-js)
  • REPL
  • JS Fiddle もBabel対応したらしい

ES6 Features

  • 新しい文法
  • 新しく追加されたビルトインクラス
  • 既存のクラスの拡張

Arrow function

var add = (a, b) => {
  return a + b;
};

[ 1, 2, 3 ].filter(n => n % 2 === 0).map(n => n * n);

Class

  • public, privateみたいなものはない

Module

  • CommonJS Module
    • browserify/webpackを使う必要がある
    • 静的に解析するのが難しい
export var foo = "foo";
export function bar() {}
export class Baz {
}

import {foo, bar, Baz} from "./module";
console.log(foo);

use strict

block scope

  • function scope
  • weird hoisting
  • let or const
    • もうvarは使わなくていい
function foo() {
  let num = 1;
  if (true) {
    let num = 2;
  }
  console.log(num);
}

Default parameters

function add (a = 1, b = 2) {
}

Rest parameters

Destructuring assignment

function draw(x, y, {width = 320, height = 160} = {}) {
}

size(0, 0);
size(0, 0, { width: 1 });

Template literal

var name = 'Bob';
var str = `Hello, I'm ${mame}.`;

Other features

  • Iterator
  • Spread Operator
  • Generator
  • Tail call optimization

New build-in claeses

Promise

new Promise((resolve, reject) => {
}

Hash/Set

  • WeakMap/SeakSet
  • TypedArray
  • Symbol

既存のクラスの拡張

  • Object
  • String

ESLint

結論

  • ES6 is awesome

HTTP/2時代のウェブサイト設計

@kazuho さん。立ち見だったのでメモが取れなかったんだけど、すごく良い発表だった。h2oの最適化が素晴らしい。Nginxじゃなくてh2o使おうかなと思うぐらい。なお、Nginxの開発者の人とも情報交換をしていて、h2oの実装を参考にNginxにもHTTP2の処理が入っていくのではないかとのこと。年内ぐらいには正式にHTTP2に対応したNginxが出てくるといいなぁ。

はじめに

  • 2秒遅くなるとクリック率が4%下がる
  • 4年間でデータの量は倍ぐらい増えてる
  • bandwidthも大きくなってるからいいのでは?
    • webpageのロード時間はbandwidth速くなっても変わらない
  • ページロードはレイテンシが小さいほうがいい

Perlで学ぼう!文系プログラマのための、知識ゼロからのデータ構造と計算量

Shinpei Maruyama (nekogata.com)さん。自分も文系なのでこの手の話はすごくよく分かる。プログラミング初心者にもわかりやすい内容で自分もいい復習になりました。

メモリ

int main(int argc, char* argv[]) {
  int a = 1;
  int b = 2;
  int c;
  c = a + b;
  printf("%d", c);
}


### オーダー法
* データの数がn個のとき、n回計算をしないといけないとき、O(n)という
* O(n)
    * データが増えた時、計算量も同じ量で増える
* O(log n)
    * データが多い時にデータが増えても計算量はそんなに増えない


## [Electron: Building desktop apps with web technologies](http://yapcasia.org/2015/talk/show/7d66b640-0a6b-11e5-b1a0-67dc7d574c3a)
Ben Ogleさん。すごく英語が聞き取りやすかった。かなりゆっくり喋ってくれたのかな?Electron自体はよくしらないけどAtomは普段から使っているので大体知っている内容だった。awesome-electornのリポジトリ見ると、かなりたくさんのアプリがElectronでできてるんだなぁと思ってその勢いの凄さにびっくりした。だってちゃんとAtomがリリースされてからまだ半年も経ってないし。

* Work on Atom
* Engineer at GitHub
* AtomのクールなところはWebテクノロジーを使ってできているところ

### Electron
* JavaScript, HTML, and CSS
* io.js
* Chromium
* OS

### どんなものができるか
* GitHub Atom
* MS Visual Studio Code
* Facebook Nuclide
* Jibo (Electron based robots)
* Fireball
* Slack client

https://github.com/sindresorhus/awesome-electron

### KittyDetect
* 猫の顔があるかを認識するアプリ
* 2時間で作った

### 質疑応答
* AtomもCoffeeScriptやめてES6に書き直している途中らしい
* OAuthで戻ってくるのってどうすればいいの?
    * protocol handler使う

YAPC::Asia 2015行ってきた(前夜祭)

今年でJPA主催のYAPCは最後ということで、いつもは前夜祭は参加しないのだけれど行ってきた。

最初に聞いたのは @hsbt さんの言語開発の現場という話。Rubyはリリース作業が特に大変だと言っていた。自動化したくてもなかなか面倒くさいものなのかなぁ。Rubyは日本人の開発者が多いので、外国人から「日本語でやりとりして仕様が決まっているのではないか」みたいに思われることがあったとか。しかしそのぐらい公共のものになってきている感じがすごいと思った。

その後、@myfinderさんに捕まって出会ってMicrosoft Azureの面白そうな話をいろいろ聞いた。そういえば転職されてから話すのは初めてだったかもしれない。Azureはデータ処理のサービスが面白そうで、NorikraみたいにSQLでストリームのデータにアクセスでき、さらに処理とデータ保存が分離されているのでロックインされずらいそうだ。AzureってMachine Learningもすごそうだし純粋に使ってみたいなぁと思った。

とりあえず明日から本番なので1日目は特に飲みに行ったりせずに大人しく帰宅。

Make VirtualBox's network faster with paravirtualized network adapter

I develop applications with Vagrant and VirtualBox and feel frustrated about slowness of VirtualBox. Since I found virtio (paravirtualized network adapter), I tried it out on my application.

How to enable virtio with Vagrant

Add following lines into your Vagrantfile.

v.customize ["modifyvm", :id, "--nictype1", "virtio"]
v.customize ["modifyvm", :id, "--nictype2", "virtio"]

Results

I ran time vagrant provision with old and new settings and virtio-net is 1.2x faster than 82540EM (default network adapter).

  • vagrant provision 14.85s user 12.49s system 5% cpu 8:20.59 total with Intel PRO/1000 MT Desktop (82540EM)
  • vagrant provision 14.63s user 12.62s system 6% cpu 7:00.70 total with virtio-net

Ansible: Up and Running

Ansible: Up and Running