-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 04f3ad3
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func timeDiff(url string) (float64, float64, error) { | ||
t0 := time.Now() | ||
resp, err := http.Head(url) | ||
delta := time.Since(t0).Seconds() | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
|
||
date := resp.Header.Get("Date") | ||
if date == "" { | ||
return 0, 0, errors.New("Date header is missing") | ||
} | ||
t1, err := time.Parse(time.RFC1123, date) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
|
||
theta := t1.Sub(t0).Seconds() + 0.5 - delta/2 | ||
return theta, delta, nil | ||
} | ||
|
||
func main() { | ||
n := flag.Uint("n", 10, "Number of requests") | ||
h := flag.String("u", "https://google.com", "Host URL") | ||
flag.Parse() | ||
|
||
var sumT, sumD float64 | ||
for i := uint(0); i < *n; i++ { | ||
theta, delta, err := timeDiff(*h) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
sumT += theta / delta | ||
sumD += 1 / delta | ||
} | ||
fmt.Printf("offset %.5f sec\n", sumT/sumD) | ||
} |