From 633d626e79d0c88cebaafeff32443783868a3c96 Mon Sep 17 00:00:00 2001 From: Tim Ebert Date: Thu, 19 Oct 2023 08:26:12 +0200 Subject: [PATCH] [WIP] Experiment with CPU load --- webhosting-operator/main.go | 60 +++++++++++++++++++ .../controllers/webhosting/reconcile_test.go | 55 +++++++++++++++++ .../scenario/reconcile/reconcile.go | 9 ++- 3 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 webhosting-operator/main.go create mode 100644 webhosting-operator/pkg/controllers/webhosting/reconcile_test.go diff --git a/webhosting-operator/main.go b/webhosting-operator/main.go new file mode 100644 index 00000000..34e87720 --- /dev/null +++ b/webhosting-operator/main.go @@ -0,0 +1,60 @@ +/* +Copyright 2023 Tim Ebert. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "crypto/rand" + "encoding/binary" + "fmt" + "os" + "strconv" +) + +func main() { + // size := 1 << 4 // 16 + size := 1 << 12 // 4 KiB + // size := 1 << 14 // 16 KiB + data := make([]byte, size) + fmt.Printf("len: %d\n", len(data)) + rand.Read(data) + + // b := (&big.Int{}).SetBytes(data) + // fmt.Printf("len: %d\n", len(b.Bytes())) + // // fmt.Printf("data (hex): %s\n", b.Text(16)) + // fmt.Printf("data (10): %s\n", b.Text(10)) + // b2 := big.NewInt(3) + + n, _ := strconv.ParseInt(os.Args[1], 10, 64) + + var s uint64 + // b.Mul(b, b2) + // b.Div(b, b2) + // // truncate b so that it doesn't allocate more memory + // b.SetBytes(b.Bytes()[:size]) + + for i := 0; i < int(n); i++ { + for j := 0; j <= len(data)-8; j += 8 { + u := binary.BigEndian.Uint64(data[j : j+8]) + s += u * u + } + } + // fmt.Printf("sum: %d\n", s) + + // fmt.Printf("len: %d\n", len(b.Bytes())) + // // fmt.Printf("data (hex): %s\n", b.Text(16)) + // fmt.Printf("data (10): %s\n", b.Text(10)) +} diff --git a/webhosting-operator/pkg/controllers/webhosting/reconcile_test.go b/webhosting-operator/pkg/controllers/webhosting/reconcile_test.go new file mode 100644 index 00000000..ed73d373 --- /dev/null +++ b/webhosting-operator/pkg/controllers/webhosting/reconcile_test.go @@ -0,0 +1,55 @@ +/* +Copyright 2023 Tim Ebert. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package webhosting + +import ( + "crypto/sha256" + "fmt" + "io" + "math/rand" + "testing" +) + +func BenchmarkHash100(b *testing.B) { + data := make([]byte, 2<<12) + _, _ = rand.New(rand.NewSource(0)).Read(data) // always returns a nil pointer per documentation + b.ReportAllocs() + + for i := 0; i < b.N; i++ { + h := sha256.New() + for i := 0; i < 100; i++ { + h.Write(data) + } + // hopefully the compiler doesn't optimize this away + fmt.Fprint(io.Discard, h.Sum(nil)) + } +} + +func BenchmarkSHA256(b *testing.B) { + data := make([]byte, 2<<12) + _, _ = rand.New(rand.NewSource(0)).Read(data) // always returns a nil pointer per documentation + b.ReportAllocs() + + h := sha256.New() + for i := 0; i < b.N; i++ { + h.Reset() + for j := 0; j < 100; j++ { + h.Write(data) + } + } + fmt.Println(h.Sum(nil)) +} diff --git a/webhosting-operator/pkg/experiment/scenario/reconcile/reconcile.go b/webhosting-operator/pkg/experiment/scenario/reconcile/reconcile.go index 3999c753..2dadce91 100644 --- a/webhosting-operator/pkg/experiment/scenario/reconcile/reconcile.go +++ b/webhosting-operator/pkg/experiment/scenario/reconcile/reconcile.go @@ -19,7 +19,6 @@ package reconcile import ( "context" "fmt" - "math/rand" "time" "sigs.k8s.io/controller-runtime/pkg/client" @@ -67,11 +66,11 @@ func (s *scenario) Prepare(ctx context.Context) error { } s.Log.Info("Preparing websites") - // random 4 KiB data to add to all websites - data := make([]byte, 1<<12) - _, _ = rand.New(rand.NewSource(0)).Read(data) // always returns a nil error per documentation + // // random 4 KiB data to add to all websites + // data := make([]byte, 1<<12) + // _, _ = rand.New(rand.NewSource(0)).Read(data) // always returns a nil error per documentation - if err := generator.CreateWebsites(ctx, s.Client, 10000, generator.WithLabels(s.Labels), generator.WithData(data)); err != nil { + if err := generator.CreateWebsites(ctx, s.Client, 10000, generator.WithLabels(s.Labels)); err != nil { return err }