<commit_before>// Copyright 2019 Authors of Cilium
//
// 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 RuntimeTest

import (
	"bytes"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/cilium/cilium/pkg/logging"
	. "github.com/cilium/cilium/test/ginkgo-ext"
	"github.com/cilium/cilium/test/helpers"

	. "github.com/onsi/gomega"
	"github.com/sirupsen/logrus"
)

func superNetperfRRIPv4(s *helpers.SSHMeta, client string, server string, num int) *helpers.CmdRes {
	var res *helpers.CmdRes

	serverNet, _ := s.ContainerInspectNet(server)
	serverIpv4 := serverNet[helpers.IPv4]
	By("super_netperf to %s from %s (should succeed)", server, client)
	cmd := fmt.Sprintf("super_netperf %d -t TCP_RR -H %s", num, serverIpv4)
	res = s.ContainerExec(client, cmd)
	res.ExpectSuccess("failed: %s", cmd)
	return res
}

// SuperNetperfRR launches 'num' parallel netperf TCP_RR
// (request/response) tests from client to server.
func superNetperfRR(s *helpers.SSHMeta, client string, server string, num int) *helpers.CmdRes {
	return superNetperfRRIPv4(s, client, server, num)
}

func superNetperfStreamIPv4(s *helpers.SSHMeta, client string, server string, num int) *helpers.CmdRes {
	var res *helpers.CmdRes

	serverNet, _ := s.ContainerInspectNet(server)
	serverIpv4 := serverNet[helpers.IPv4]
	By("super_netperf to %s from %s (should succeed)", server, client)
	cmd := fmt.Sprintf("super_netperf %d -f g -t TCP_STREAM -H %s", num, serverIpv4)
	res = s.ContainerExec(client, cmd)
	res.ExpectSuccess("failed: %s", cmd)
	return res
}

// SuperNetperfStream launches 'num' parallel netperf TCP_STREAM
// tests from client to server.
func superNetperfStream(s *helpers.SSHMeta, client string, server string, num int) *helpers.CmdRes {
	return superNetperfStreamIPv4(s, client, server, num)
}

var _ = Describe("BenchmarkNetperfPerformance", func() {
	var (
		vm          *helpers.SSHMeta
		monitorStop = func() error { return nil }

		log    = logging.DefaultLogger
		logger = logrus.NewEntry(log)

		PerfLogFile   = "l4bench_perf.log"
		PerfLogWriter bytes.Buffer
	)

	BeforeAll(func() {
		vm = helpers.InitRuntimeHelper(helpers.Runtime, logger)
		ExpectCiliumReady(vm)
	})

	JustBeforeEach(func() {
		monitorStop = vm.MonitorStart()
	})

	JustAfterEach(func() {
		vm.ValidateNoErrorsInLogs(CurrentGinkgoTestDescription().Duration)
		Expect(monitorStop()).To(BeNil(), "cannot stop monitor command")
	})

	AfterFailed(func() {
		vm.ReportFailed(
			"cilium service list",
			"cilium policy get")
	})

	AfterEach(func() {
		LogPerm := os.FileMode(0666)
		testPath, err := helpers.CreateReportDirectory()
		Expect(err).Should(BeNil(), "cannot create log file")
		helpers.WriteOrAppendToFile(filepath.Join(testPath, PerfLogFile), PerfLogWriter.Bytes(), LogPerm)
		PerfLogWriter.Reset()
	}, 500)

	createContainers := func() {
		By("create Client container")
		vm.ContainerCreate(helpers.Client, helpers.NetperfImage, helpers.CiliumDockerNetwork, "-l id.client")
		By("create Server containers")
		vm.ContainerCreate(helpers.Server, helpers.NetperfImage, helpers.CiliumDockerNetwork, "-l id.server")
		vm.PolicyDelAll()
		Expect( vm.WaitEndpointsReady() ).To(BeNil(), "Endpoints are not ready")
	}

	removeContainers := func(containerName string) {
		By("removing container %s", containerName)
		res := vm.ContainerRm(containerName)
		Expect(res.WasSuccessful()).Should(BeTrue(), "Container removal failed")
	}

	deleteContainers := func() {
		removeContainers(helpers.Client)
		removeContainers(helpers.Server)
	}

	superNetperfRRLog := func(client string, server string, num int) {
		res := superNetperfRR(vm, client, server, num)
		fmt.Fprintf(&PerfLogWriter, "%s,", strings.TrimSuffix(res.GetStdOut(), "\n"))
	}

	superNetperfStreamLog := func(client string, server string, num int) {
		res := superNetperfStream(vm, client, server, num)
		fmt.Fprintf(&PerfLogWriter, "%s,", strings.TrimSuffix(res.GetStdOut(), "\n"))
	}

	Context("Benchmark Netperf Tests", func() {
		BeforeAll(func() {
			createContainers()
		})

		AfterAll(func() {<commit_msg>test: Fix unformatted go code<commit_after>// Copyright 2019 Authors of Cilium
//
// 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 RuntimeTest

import (
	"bytes"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/cilium/cilium/pkg/logging"
	. "github.com/cilium/cilium/test/ginkgo-ext"
	"github.com/cilium/cilium/test/helpers"

	. "github.com/onsi/gomega"
	"github.com/sirupsen/logrus"
)

func superNetperfRRIPv4(s *helpers.SSHMeta, client string, server string, num int) *helpers.CmdRes {
	var res *helpers.CmdRes

	serverNet, _ := s.ContainerInspectNet(server)
	serverIpv4 := serverNet[helpers.IPv4]
	By("super_netperf to %s from %s (should succeed)", server, client)
	cmd := fmt.Sprintf("super_netperf %d -t TCP_RR -H %s", num, serverIpv4)
	res = s.ContainerExec(client, cmd)
	res.ExpectSuccess("failed: %s", cmd)
	return res
}

// SuperNetperfRR launches 'num' parallel netperf TCP_RR
// (request/response) tests from client to server.
func superNetperfRR(s *helpers.SSHMeta, client string, server string, num int) *helpers.CmdRes {
	return superNetperfRRIPv4(s, client, server, num)
}

func superNetperfStreamIPv4(s *helpers.SSHMeta, client string, server string, num int) *helpers.CmdRes {
	var res *helpers.CmdRes

	serverNet, _ := s.ContainerInspectNet(server)
	serverIpv4 := serverNet[helpers.IPv4]
	By("super_netperf to %s from %s (should succeed)", server, client)
	cmd := fmt.Sprintf("super_netperf %d -f g -t TCP_STREAM -H %s", num, serverIpv4)
	res = s.ContainerExec(client, cmd)
	res.ExpectSuccess("failed: %s", cmd)
	return res
}

// SuperNetperfStream launches 'num' parallel netperf TCP_STREAM
// tests from client to server.
func superNetperfStream(s *helpers.SSHMeta, client string, server string, num int) *helpers.CmdRes {
	return superNetperfStreamIPv4(s, client, server, num)
}

var _ = Describe("BenchmarkNetperfPerformance", func() {
	var (
		vm          *helpers.SSHMeta
		monitorStop = func() error { return nil }

		log    = logging.DefaultLogger
		logger = logrus.NewEntry(log)

		PerfLogFile   = "l4bench_perf.log"
		PerfLogWriter bytes.Buffer
	)

	BeforeAll(func() {
		vm = helpers.InitRuntimeHelper(helpers.Runtime, logger)
		ExpectCiliumReady(vm)
	})

	JustBeforeEach(func() {
		monitorStop = vm.MonitorStart()
	})

	JustAfterEach(func() {
		vm.ValidateNoErrorsInLogs(CurrentGinkgoTestDescription().Duration)
		Expect(monitorStop()).To(BeNil(), "cannot stop monitor command")
	})

	AfterFailed(func() {
		vm.ReportFailed(
			"cilium service list",
			"cilium policy get")
	})

	AfterEach(func() {
		LogPerm := os.FileMode(0666)
		testPath, err := helpers.CreateReportDirectory()
		Expect(err).Should(BeNil(), "cannot create log file")
		helpers.WriteOrAppendToFile(filepath.Join(testPath, PerfLogFile), PerfLogWriter.Bytes(), LogPerm)
		PerfLogWriter.Reset()
	}, 500)

	createContainers := func() {
		By("create Client container")
		vm.ContainerCreate(helpers.Client, helpers.NetperfImage, helpers.CiliumDockerNetwork, "-l id.client")
		By("create Server containers")
		vm.ContainerCreate(helpers.Server, helpers.NetperfImage, helpers.CiliumDockerNetwork, "-l id.server")
		vm.PolicyDelAll()
		Expect(vm.WaitEndpointsReady()).To(BeNil(), "Endpoints are not ready")
	}

	removeContainers := func(containerName string) {
		By("removing container %s", containerName)
		res := vm.ContainerRm(containerName)
		Expect(res.WasSuccessful()).Should(BeTrue(), "Container removal failed")
	}

	deleteContainers := func() {
		removeContainers(helpers.Client)
		removeContainers(helpers.Server)
	}

	superNetperfRRLog := func(client string, server string, num int) {
		res := superNetperfRR(vm, client, server, num)
		fmt.Fprintf(&PerfLogWriter, "%s,", strings.TrimSuffix(res.GetStdOut(), "\n"))
	}

	superNetperfStreamLog := func(client string, server string, num int) {
		res := superNetperfStream(vm, client, server, num)
		fmt.Fprintf(&PerfLogWriter, "%s,", strings.TrimSuffix(res.GetStdOut(), "\n"))
	}

	Context("Benchmark Netperf Tests", func() {
		BeforeAll(func() {
			createContainers()
		})

		AfterAll(func() {
{'fraction_non_alphanumeric': 0.1296377607025247, 'fraction_numerical': 0.00570801317233809, 'mean_word_length': 4.814294830887046, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7118232', 'n_tokens_mistral': 3143, 'n_tokens_neox': 3036, 'n_words': 764}