-
Notifications
You must be signed in to change notification settings - Fork 24
feat(kas): collect metrics #1702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
1f730c0
feat(kas) - collect metrics
sujankota 11123cf
Merge branch 'main' into feat/collect-metric
sujankota 3d74003
missed some files to merge
sujankota cb445e3
missed some files to merge
sujankota 546da3a
fix lint errors
sujankota 106fd9d
minor fix
sujankota 767ce61
Merge branch 'main' into feat/collect-metric
sujankota f0f72c0
more cleanup
sujankota 8aae5d9
fix lint errors
sujankota baeb532
minor cleanup
sujankota 6e584bb
enable benchmark ci
sujankota b460634
debug benchmark ci
sujankota bd1e0be
debug benchmark ci
sujankota 1061c3a
debug benchmark ci
sujankota afbfc3b
debug benchmark ci
sujankota 85d08d9
Merge branch 'main' into feat/collect-metric
sujankota 9c9ae6e
debug benchmark ci
sujankota 128c0b4
debug benchmark ci
sujankota 9da3fc0
minor cleanup
sujankota 7838552
fix the nanotdf sdk performance
sujankota 9e77426
Merge branch 'main' into feat/collect-metric
sujankota 04dc857
fix the build
sujankota 7238368
fix the build
sujankota 0de2afe
Merge branch 'main' into feat/collect-metric
sujankota 04b4a61
Merge branch 'main' into feat/collect-metric
sujankota f9726e3
Merge branch 'main' into feat/collect-metric
sujankota 3f2144c
code cleanup
sujankota cdc0a93
fix the build
sujankota 50ffdf2
go.sum changes
sujankota 1021e7a
Merge branch 'main' into feat/collect-metric
sujankota 4afe3df
Merge branch 'main' into feat/collect-metric
sujankota 3a1cda5
Merge branch 'main' into feat/collect-metric
sujankota 2b48524
Add attributes
sujankota fb681ce
Add attributes
sujankota ae88f78
Add attributes
sujankota 5f1093a
Merge branch 'main' into feat/collect-metric
dmihalcik-virtru ff13dc1
lint, testify cleanup
dmihalcik-virtru 02a0399
cleanups, hopefully fixes ci
dmihalcik-virtru 77b1f73
Update examples.go
dmihalcik-virtru 2659a6c
tls cleanup
dmihalcik-virtru e825a68
Update checks.yaml
dmihalcik-virtru f2acffa
make tracing optional; add otel config; srv opt
dmihalcik-virtru ef59554
remove debug logging
dmihalcik-virtru 20f8b23
Update checks.yaml
dmihalcik-virtru 0dc29ae
hmm
dmihalcik-virtru d0c71dd
hmmm
dmihalcik-virtru 0a6c7ce
Merge branch 'main' into feat/collect-metric
dmihalcik-virtru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -45,3 +45,4 @@ sensitive.txt.tdf | |
keys/ | ||
/examples/sensitive.txt.ntdf | ||
sensitive.txt.ntdf | ||
traces/ |
This file contains hidden or 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,228 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/opentdf/platform/sdk" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type TDFFormat string | ||
|
||
const ( | ||
TDF3 TDFFormat = "tdf3" | ||
NanoTDF TDFFormat = "nanotdf" | ||
) | ||
|
||
func (f *TDFFormat) String() string { | ||
return string(*f) | ||
} | ||
|
||
func (f *TDFFormat) Set(value string) error { | ||
switch value { | ||
case "tdf3", "nanotdf": | ||
*f = TDFFormat(value) | ||
return nil | ||
default: | ||
return errors.New("invalid TDF format") | ||
} | ||
} | ||
|
||
func (f *TDFFormat) Type() string { | ||
return "TDFFormat" | ||
} | ||
|
||
type BenchmarkConfig struct { | ||
TDFFormat TDFFormat | ||
ConcurrentRequests int | ||
RequestCount int | ||
RequestsPerSecond int | ||
TimeoutSeconds int | ||
} | ||
|
||
var config BenchmarkConfig | ||
|
||
func init() { | ||
benchmarkCmd := &cobra.Command{ | ||
Use: "benchmark", | ||
Short: "OpenTDF benchmark tool", | ||
Long: `A OpenTDF benchmark tool to measure throughput and latency with configurable concurrency.`, | ||
RunE: runBenchmark, | ||
} | ||
|
||
benchmarkCmd.Flags().IntVar(&config.ConcurrentRequests, "concurrent", 10, "Number of concurrent requests") | ||
benchmarkCmd.Flags().IntVar(&config.RequestCount, "count", 100, "Total number of requests") | ||
benchmarkCmd.Flags().IntVar(&config.RequestsPerSecond, "rps", 50, "Requests per second limit") | ||
benchmarkCmd.Flags().IntVar(&config.TimeoutSeconds, "timeout", 30, "Timeout in seconds") | ||
benchmarkCmd.Flags().Var(&config.TDFFormat, "tdf", "TDF format (tdf3 or nanotdf)") | ||
ExamplesCmd.AddCommand(benchmarkCmd) | ||
} | ||
|
||
func runBenchmark(cmd *cobra.Command, args []string) error { | ||
in := strings.NewReader("Hello, World!") | ||
|
||
// Create new offline client | ||
client, err := newSDK() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
out := os.Stdout | ||
if outputName != "-" { | ||
out, err = os.Create("sensitive.txt.tdf") | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
defer func() { | ||
if outputName != "-" { | ||
out.Close() | ||
} | ||
}() | ||
|
||
var dataAttributes = []string{"https://example.com/attr/attr1/value/value1"} | ||
if config.TDFFormat == NanoTDF { | ||
nanoTDFConfig, err := client.NewNanoTDFConfig() | ||
if err != nil { | ||
return err | ||
} | ||
nanoTDFConfig.SetAttributes(dataAttributes) | ||
nanoTDFConfig.EnableECDSAPolicyBinding() | ||
err = nanoTDFConfig.SetKasURL(fmt.Sprintf("http://%s/kas", "localhost:8080")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = client.CreateNanoTDF(out, in, *nanoTDFConfig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if outputName != "-" { | ||
err = cat(cmd, outputName) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} else { | ||
tdf, err := | ||
client.CreateTDF( | ||
out, in, | ||
sdk.WithDataAttributes(dataAttributes...), | ||
sdk.WithKasInformation( | ||
sdk.KASInfo{ | ||
URL: fmt.Sprintf("http://%s", "localhost:8080"), | ||
PublicKey: "", | ||
}), | ||
sdk.WithAutoconfigure(false)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
manifestJSON, err := json.MarshalIndent(tdf.Manifest(), "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
cmd.Println(string(manifestJSON)) | ||
} | ||
|
||
var wg sync.WaitGroup | ||
requests := make(chan struct{}, config.ConcurrentRequests) | ||
results := make(chan time.Duration, config.RequestCount) | ||
errors := make(chan error, config.RequestCount) | ||
|
||
// Function to perform the operation | ||
operation := func() { | ||
defer wg.Done() | ||
start := time.Now() | ||
|
||
file, err := os.Open("sensitive.txt.tdf") | ||
if err != nil { | ||
errors <- fmt.Errorf("file open error: %v", err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
if config.TDFFormat == NanoTDF { | ||
_, err = client.ReadNanoTDF(io.Discard, file) | ||
if err != nil { | ||
errors <- fmt.Errorf("ReadNanoTDF error: %v", err) | ||
return | ||
} | ||
} else { | ||
tdfreader, err := client.LoadTDF(file) | ||
if err != nil { | ||
errors <- fmt.Errorf("LoadTDF error: %v", err) | ||
return | ||
} | ||
|
||
_, err = io.Copy(io.Discard, tdfreader) | ||
if err != nil && err != io.EOF { | ||
errors <- fmt.Errorf("read error: %v", err) | ||
return | ||
} | ||
} | ||
|
||
results <- time.Since(start) | ||
} | ||
|
||
// Start the benchmark | ||
startTime := time.Now() | ||
for i := 0; i < config.RequestCount; i++ { | ||
wg.Add(1) | ||
requests <- struct{}{} | ||
go func() { | ||
defer func() { <-requests }() | ||
operation() | ||
}() | ||
} | ||
|
||
wg.Wait() | ||
close(results) | ||
close(errors) | ||
|
||
// Count errors and collect error messages | ||
errorCount := 0 | ||
errorMsgs := make(map[string]int) | ||
for err := range errors { | ||
errorCount++ | ||
errorMsgs[err.Error()]++ | ||
} | ||
|
||
successCount := 0 | ||
var totalDuration time.Duration | ||
for result := range results { | ||
successCount++ | ||
totalDuration += result | ||
} | ||
|
||
totalTime := time.Since(startTime) | ||
averageLatency := totalDuration / time.Duration(successCount) | ||
throughput := float64(successCount) / totalTime.Seconds() | ||
|
||
// Print results | ||
cmd.Printf("\nBenchmark Results:\n") | ||
cmd.Printf("Total Requests: %d\n", config.RequestCount) | ||
cmd.Printf("Successful Requests: %d\n", successCount) | ||
cmd.Printf("Failed Requests: %d\n", errorCount) | ||
cmd.Printf("Concurrent Requests: %d\n", config.ConcurrentRequests) | ||
cmd.Printf("Total Time: %s\n", totalTime) | ||
cmd.Printf("Average Latency: %s\n", averageLatency) | ||
cmd.Printf("Throughput: %.2f requests/second\n", throughput) | ||
|
||
if errorCount > 0 { | ||
cmd.Printf("\nError Summary:\n") | ||
for errMsg, count := range errorMsgs { | ||
cmd.Printf("%s: %d occurrences\n", errMsg, count) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.