Skip to content

Commit 46a5b9b

Browse files
committed
change tmp_ and add speed
1 parent a3a0fee commit 46a5b9b

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

main.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import (
1313
"math"
1414
"net/http"
1515
"os"
16+
"path/filepath"
1617
"strconv"
1718
"time"
18-
"path/filepath"
1919
)
2020

21-
const VERSION = "1.0.2"
21+
const VERSION = "1.0.3"
2222

2323
var queue, redo, finish chan int
2424
var cor, size, length, timeout int
@@ -64,8 +64,8 @@ func main() {
6464
return
6565
}
6666

67-
if dst==""{
68-
_,dst=filepath.Split(url)
67+
if dst == "" {
68+
_, dst = filepath.Split(url)
6969
}
7070

7171
startTime := time.Now()
@@ -116,7 +116,7 @@ func main() {
116116
defer file.Close()
117117
var offset int64 = 0
118118
for x := 0; x < fragment; x++ {
119-
filename := fmt.Sprintf("tmp_%d", x)
119+
filename := fmt.Sprintf("%s_%d", dst, x)
120120
buf, err := ioutil.ReadFile(filename)
121121
if err != nil {
122122
log.Println(err)
@@ -141,7 +141,8 @@ func main() {
141141
}
142142

143143
finishTime := time.Now()
144-
log.Printf("Time:%f\n", finishTime.Sub(startTime).Seconds())
144+
duration := finishTime.Sub(startTime).Seconds()
145+
log.Printf("Time:%f Speed:%f Kb/s\n", duration, float64(length)/duration/1024)
145146
}
146147

147148
func Do(request *http.Request, fragment, no int) {
@@ -153,6 +154,8 @@ func Do(request *http.Request, fragment, no int) {
153154
return
154155
}
155156
for {
157+
cStartTime := time.Now()
158+
156159
i := <-queue
157160
//log.Printf("[%d][%d]Start download\n",no, i)
158161
start := i * size
@@ -163,13 +166,13 @@ func Do(request *http.Request, fragment, no int) {
163166
end = length - 1
164167
}
165168

166-
filename := fmt.Sprintf("tmp_%d", i)
169+
filename := fmt.Sprintf("%s_%d", dst, i)
167170
if cache {
168-
filesize := int64(end - start+1)
171+
filesize := int64(end - start + 1)
169172
file, err := os.Stat(filename)
170-
if err==nil && file.Size()==filesize{
171-
log.Printf("[%d][%d]Hint cached %s, size:%d\n", no, i, filename,filesize)
172-
finish<-i
173+
if err == nil && file.Size() == filesize {
174+
log.Printf("[%d][%d]Hint cached %s, size:%d\n", no, i, filename, filesize)
175+
finish <- i
173176
continue
174177
}
175178
}
@@ -206,7 +209,9 @@ func Do(request *http.Request, fragment, no int) {
206209
redo <- i
207210
continue
208211
}
209-
log.Printf("[%d][%d]Wrote successfully:%d\n", no, i, n)
212+
cEndTime := time.Now()
213+
duration := cEndTime.Sub(cStartTime).Seconds()
214+
log.Printf("[%d][%d]Download successfully:%f Kb/s\n", no, i, float64(n)/duration/1024)
210215

211216
file.Close()
212217
resp.Body.Close()

0 commit comments

Comments
 (0)