Skip to content

Commit

Permalink
[WIP] Experiment with CPU load
Browse files Browse the repository at this point in the history
  • Loading branch information
timebertt committed Oct 24, 2023
1 parent 3f4d641 commit 3bcb595
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 5 deletions.
60 changes: 60 additions & 0 deletions webhosting-operator/main.go
Original file line number Diff line number Diff line change
@@ -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))
}
55 changes: 55 additions & 0 deletions webhosting-operator/pkg/controllers/webhosting/reconcile_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package reconcile
import (
"context"
"fmt"
"math/rand"
"time"

"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 3bcb595

Please sign in to comment.