-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtokenmonster_bench.go
62 lines (50 loc) · 1.34 KB
/
tokenmonster_bench.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/alasdairforsythe/tokenmonster/go"
)
func main() {
// Check if a command-line argument is provided
if len(os.Args) < 3 {
fmt.Println("Usage: ./tokenmonster_bench vocabfile textfile")
return
}
vocab, err := tokenmonster.Load(os.Args[1])
if err != nil {
fmt.Println(err)
return
}
var totalSize float64
var totalelapsed float64
var totaltokens int
for i:=2; i<len(os.Args); i++ {
fmt.Println(os.Args[i])
// Read the file content into memory
content, err := ioutil.ReadFile(os.Args[i])
if err != nil {
fmt.Printf("Error reading file: %s\n", err)
return
}
startTime := time.Now()
tokens, _, err := vocab.Tokenize(content)
elapsed := time.Since(startTime)
totaltokens += len(tokens)
// Print the elapsed time
fmt.Printf("Tokens: %d\n", len(tokens))
fmt.Printf("Elapsed time: %s\n", elapsed)
// Calculate speed in MB/s
contentSizeMB := float64(len(content)) / 1024 / 1024
elapsedSeconds := elapsed.Seconds()
speed := contentSizeMB / elapsedSeconds
totalSize += contentSizeMB
totalelapsed += elapsedSeconds
fmt.Printf("Speed: %.2f MB/s\n", speed)
}
// Total
fmt.Printf("Total tokens: %d\n", totaltokens)
fmt.Printf("Total elapsed time: %s\n", totalelapsed)
fmt.Printf("Total speed: %.2f MB/s\n", totalSize / totalelapsed)
}