-
-
Save ymotongpoo/342fde1b517aad2635746066ca26af3c to your computer and use it in GitHub Desktop.
pprof特訓会場 演習1 少し改善版
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2019 Yoshi Yamaguchi | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
// This application is Go port of `cut` command. | |
// This is just a sample application so it's not the perfect clone. | |
// This is used for the exercise part of https://connpass.com/event/144347/ | |
// | |
// For testing, prepare large file where fields are splitted by same characters. | |
// * http://eforexcel.com/wp/downloads-18-sample-csv-files-data-sets-for-testing-sales/ | |
// * https://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=236 | |
package main | |
import ( | |
"bufio" | |
"bytes" | |
"flag" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
"runtime/pprof" | |
) | |
var ( | |
delimiter byte | |
field int | |
) | |
func init() { | |
var delimiterStr string | |
flag.StringVar(&delimiterStr, "d", ",", "delimiter") | |
delimiter = delimiterStr[0] | |
flag.IntVar(&field, "f", 1, "field position") | |
} | |
// 使用方法: ./cut1 -f 3 -d ',' foo.csv | |
// フィールドに関しては範囲を表すハイフンは利用しない。 | |
func main() { | |
flag.Parse() | |
f, err := os.Open(flag.Arg(0)) | |
if err != nil { | |
log.Fatalf("Could not open file %q: %v", flag.Arg(0), err) | |
} | |
defer f.Close() | |
report, _ := os.Create("cpu.prof") | |
defer report.Close() | |
_ = pprof.StartCPUProfile(report) | |
defer pprof.StopCPUProfile() | |
pos := field - 1 | |
r := bufio.NewReader(f) | |
for { | |
// ファイルからの読み込みが出来ない場合やファイル末尾の場合は終了する | |
line, err := r.ReadBytes('\n') | |
if err == io.EOF { | |
break | |
} | |
if err != nil { | |
log.Fatalf("Could not read file %q properly: %v", flag.Arg(0), err) | |
} | |
fields := bytes.SplitN(line, []byte(","), field) | |
fmt.Println(string(fields[pos])) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment