func TestDecode(t *testing.T) { for _, file := range testFiles { fpath := filepath.Join(tfolder, file.folder, file.name) t.Logf("decoding %q", fpath) data, err := ioutil.ReadFile(fpath) if err != nil { t.Fatalf("%q: reading file failed: %v", fpath, err) } fitFile, err := fit.Decode(bytes.NewReader(data)) if !file.wantErr && err != nil { t.Errorf("%q: got error %v, want no error", fpath, err) } if file.wantErr && err == nil { t.Errorf("%q: got no error, want error", fpath) } if file.gobSHA1 == "" { continue } sum := sha1.New() enc := gob.NewEncoder(sum) err = enc.Encode(fitFile) if err != nil { t.Fatalf("%q: gob encode failed:", err) } b64sum := base64.StdEncoding.EncodeToString(sum.Sum(nil)) if b64sum != file.gobSHA1 { t.Errorf("%q: SHA-1 for gob encoded fit file differs", fpath) // TODO(tormoder): Diff using goon? } } }
func Example() { // Read our FIT test file data testFile := filepath.Join("testdata", "fitsdk", "Activity.fit") testData, err := ioutil.ReadFile(testFile) if err != nil { fmt.Println(err) return } // Decode the FIT file data fit, err := fit.Decode(bytes.NewReader(testData)) if err != nil { fmt.Println(err) return } // Inspect the TimeCreated field in the FileId message fmt.Println(fit.FileId.TimeCreated) // Inspect the dynamic Product field in the FileId message fmt.Println(fit.FileId.GetProduct()) // Inspect the FIT file type fmt.Println(fit.FileType()) // Get the actual activity activity, err := fit.Activity() if err != nil { fmt.Println(err) return } // Print the latitude and longitude of the first Record message for _, record := range activity.Records { fmt.Println(record.PositionLat) fmt.Println(record.PositionLong) break } // Print the sport of the first Session message for _, session := range activity.Sessions { fmt.Println(session.Sport) break } // Output: // 2012-04-09 21:22:26 +0000 UTC // Hrm1 // Activity // 41.51393 // -73.14859 // Running }
func BenchmarkDecodeActivityLargeParallel(b *testing.B) { b.ReportAllocs() data, err := ioutil.ReadFile(activityLarge) if err != nil { b.Fatal(err) } b.RunParallel(func(pb *testing.PB) { for pb.Next() { _, err := fit.Decode(bytes.NewReader(data)) if err != nil { b.Fatal(err) } } }) }
func benchmarkDecode(b *testing.B, filename string, bench string) { data, err := ioutil.ReadFile(filename) if err != nil { b.Fatal(err) } b.ReportAllocs() b.SetBytes(int64(len(data))) switch bench { case "Full": b.ResetTimer() for i := 0; i < b.N; i++ { _, err := fit.Decode(bytes.NewReader(data)) if err != nil { b.Fatal(err) } } case "Header": b.ResetTimer() for i := 0; i < b.N; i++ { _, err := fit.DecodeHeader(bytes.NewReader(data)) if err != nil { b.Fatal(err) } } case "HeaderAndFileID": b.ResetTimer() for i := 0; i < b.N; i++ { _, _, err := fit.DecodeHeaderAndFileID(bytes.NewReader(data)) if err != nil { b.Fatal(err) } } default: panic("benchmarkDecode: unknown benchmark") } }
func ExampleSetLocalTimeZone() { testFile := filepath.Join("../testdata", "fitsdk", "Activity.fit") testData, err := ioutil.ReadFile(testFile) if err != nil { fmt.Println(err) return } fit, err := fit.Decode(bytes.NewReader(testData)) if err != nil { fmt.Println(err) return } activity, err := fit.Activity() if err != nil { fmt.Println(err) return } fmt.Println("Post decode & pre SetLocalTimezone:") fmt.Println(activity.Activity.LocalTimestamp) err = timeutil.SetLocalTimeZone(fit) if err != nil { fmt.Println(err) return } fmt.Println("Post SetLocalTimezone:") fmt.Println(activity.Activity.LocalTimestamp) // Output: // Post decode & pre SetLocalTimezone: // 2012-04-09 17:24:51 -0400 FITLOCAL // Post SetLocalTimezone: // 2012-04-09 17:24:51 -0400 EDT }