mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-11 22:31:36 +00:00
good good study, day day up!
This commit is contained in:
102
vendor/gopl.io/ch9/memotest/memotest.go
generated
vendored
Normal file
102
vendor/gopl.io/ch9/memotest/memotest.go
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 272.
|
||||
|
||||
// Package memotest provides common functions for
|
||||
// testing various designs of the memo package.
|
||||
package memotest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
//!+httpRequestBody
|
||||
func httpGetBody(url string) (interface{}, error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return ioutil.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
//!-httpRequestBody
|
||||
|
||||
var HTTPGetBody = httpGetBody
|
||||
|
||||
func incomingURLs() <-chan string {
|
||||
ch := make(chan string)
|
||||
go func() {
|
||||
for _, url := range []string{
|
||||
"https://golang.org",
|
||||
"https://godoc.org",
|
||||
"https://play.golang.org",
|
||||
"http://gopl.io",
|
||||
"https://golang.org",
|
||||
"https://godoc.org",
|
||||
"https://play.golang.org",
|
||||
"http://gopl.io",
|
||||
} {
|
||||
ch <- url
|
||||
}
|
||||
close(ch)
|
||||
}()
|
||||
return ch
|
||||
}
|
||||
|
||||
type M interface {
|
||||
Get(key string) (interface{}, error)
|
||||
}
|
||||
|
||||
/*
|
||||
//!+seq
|
||||
m := memo.New(httpGetBody)
|
||||
//!-seq
|
||||
*/
|
||||
|
||||
func Sequential(t *testing.T, m M) {
|
||||
//!+seq
|
||||
for url := range incomingURLs() {
|
||||
start := time.Now()
|
||||
value, err := m.Get(url)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
fmt.Printf("%s, %s, %d bytes\n",
|
||||
url, time.Since(start), len(value.([]byte)))
|
||||
}
|
||||
//!-seq
|
||||
}
|
||||
|
||||
/*
|
||||
//!+conc
|
||||
m := memo.New(httpGetBody)
|
||||
//!-conc
|
||||
*/
|
||||
|
||||
func Concurrent(t *testing.T, m M) {
|
||||
//!+conc
|
||||
var n sync.WaitGroup
|
||||
for url := range incomingURLs() {
|
||||
n.Add(1)
|
||||
go func(url string) {
|
||||
start := time.Now()
|
||||
value, err := m.Get(url)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
fmt.Printf("%s, %s, %d bytes\n",
|
||||
url, time.Since(start), len(value.([]byte)))
|
||||
n.Done()
|
||||
}(url)
|
||||
}
|
||||
n.Wait()
|
||||
//!-conc
|
||||
}
|
Reference in New Issue
Block a user