From 9688e918dd71cd8c3e91f9e44a8475addeb8de17 Mon Sep 17 00:00:00 2001 From: JeshuaRen <1366929814@qq.com> Date: Thu, 20 Jun 2024 09:51:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9EC=E9=85=8D=E7=BD=AE=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 95 +++++++++++++++++++++++++++++++++++++ sdks/storage/models.go | 2 +- utils/time2/test.go | 104 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 main.go create mode 100644 utils/time2/test.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..b27a0c6 --- /dev/null +++ b/main.go @@ -0,0 +1,95 @@ +package main + +import ( + "fmt" + "io" + "os" + "strconv" + "time" + + cdssdk "gitlink.org.cn/cloudream/common/sdks/storage" +) + +func main() { + test1("http://121.36.5.116:32010") + // test2("http://127.0.0.1:7890") +} + +func test1(url string) { + cli := cdssdk.NewClient(&cdssdk.Config{ + URL: url, + }) + + openLen, err := strconv.ParseInt(os.Args[1], 10, 64) + if err != nil { + fmt.Println(err) + return + } + + readLen, err := strconv.ParseInt(os.Args[2], 10, 64) + if err != nil { + fmt.Println(err) + return + } + + partLen, err := strconv.ParseInt(os.Args[3], 10, 64) + if err != nil { + fmt.Println(err) + return + } + + startTime := time.Now() + obj, err := cli.Object().Download(cdssdk.ObjectDownload{ + UserID: 1, + ObjectID: 470790, + Offset: 0, + Length: &openLen, + PartSize: partLen, + }) + if err != nil { + fmt.Println(err) + return + } + fmt.Printf("Open time: %v\n", time.Since(startTime)) + + startTime = time.Now() + buf := make([]byte, readLen) + _, err = io.ReadFull(obj.File, buf) + fmt.Printf("Read time: %v\n", time.Since(startTime)) + if err != nil { + fmt.Println(err) + return + } + + startTime = time.Now() + obj.File.Close() + fmt.Printf("Close time: %v\n", time.Since(startTime)) +} + +func test2(url string) { + cli := cdssdk.NewClient(&cdssdk.Config{ + URL: url, + }) + + obj, err := cli.Object().Download(cdssdk.ObjectDownload{ + UserID: 1, + ObjectID: 27151, + Offset: 0, + PartSize: 100000000, + // Length: &openLen, + }) + + if err != nil { + fmt.Println(err) + return + } + + f, err := os.Create("test.txt") + if err != nil { + fmt.Println(err) + return + } + + io.Copy(f, obj.File) +} + diff --git a/sdks/storage/models.go b/sdks/storage/models.go index b1dad0c..20e47f6 100644 --- a/sdks/storage/models.go +++ b/sdks/storage/models.go @@ -71,7 +71,7 @@ func (b *RepRedundancy) Value() (driver.Value, error) { return serder.ObjectToJSONEx[Redundancy](b) } -var DefaultECRedundancy = *NewECRedundancy(2, 3, 1024*1024*5) +var DefaultECRedundancy = *NewECRedundancy(3, 6, 1024*1024*5) type ECRedundancy struct { serder.Metadata `union:"ec"` diff --git a/utils/time2/test.go b/utils/time2/test.go new file mode 100644 index 0000000..07be5b2 --- /dev/null +++ b/utils/time2/test.go @@ -0,0 +1,104 @@ +package time2 + +import ( + "fmt" + "path" + "runtime" + "strings" + "time" +) + +type Measurement struct { + startTime time.Time + lastPointTime time.Time + printer func(string) + on bool + title string +} + +func NewMeasurement(printer func(string)) Measurement { + return Measurement{ + printer: printer, + } +} + +func (m *Measurement) Begin(on bool, title ...string) { + if m == nil { + return + } + + m.on = on + m.title = strings.Join(title, ".") + + if on { + m.startTime = time.Now() + m.lastPointTime = m.startTime + + _, file, line, ok := runtime.Caller(1) + + titlePart := "" + if m.title != "" { + titlePart = fmt.Sprintf(":%s", m.title) + } + + if ok { + m.printer(fmt.Sprintf("[begin%v]%v:%v", titlePart, path.Base(file), line)) + } else { + m.printer(fmt.Sprintf("[begin%v]unknown point", titlePart)) + } + } +} + +func (m *Measurement) Point(head ...string) { + if m == nil { + return + } + + if m.on { + m.printer(m.makePointString(strings.Join(head, "."))) + } +} + +func (m *Measurement) makePointString(head string) string { + last := m.lastPointTime + now := time.Now() + m.lastPointTime = now + + _, file, line, ok := runtime.Caller(2) + + prefixCont := "" + + if m.title != "" { + prefixCont = m.title + } + + if head != "" { + if prefixCont == "" { + prefixCont = head + } else { + prefixCont = fmt.Sprintf("%s.%s", prefixCont, head) + } + } + + prefixPart := "" + if prefixCont != "" { + prefixPart = fmt.Sprintf("[%s]", prefixCont) + } + + if ok { + return fmt.Sprintf("%v%v:%v@%v(%v)", prefixPart, path.Base(file), line, now.Sub(last), now.Sub(m.startTime)) + } + + return fmt.Sprintf("%vunknown point@%v(%v)", prefixPart, now.Sub(last), now.Sub(m.startTime)) +} + +func (m *Measurement) End(head ...string) { + if m == nil { + return + } + + if m.on { + m.printer(fmt.Sprintf("[end]%v\n", m.makePointString(strings.Join(head, ".")))) + } +} +