Skip to content
Myr

form_mapping_benchmark_test.go

This file

Path

binding/form_mapping_benchmark_test.go

67 lines · 1.4 KiB · Go

Last changed by

refactor: use b.Loop() to simplify the code and improve performance (#4432)efckingbefore adoptiona85ef5c

History of this file

Take it

1// Copyright 2019 Gin Core Team. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package binding67import (8	"testing"9	"time"1011	"github.com/stretchr/testify/assert"12)1314var form = map[string][]string{15	"name":      {"mike"},16	"friends":   {"anna", "nicole"},17	"id_number": {"12345678"},18	"id_date":   {"2018-01-20"},19}2021type structFull struct {22	Name    string   `form:"name"`23	Age     int      `form:"age,default=25"`24	Friends []string `form:"friends"`25	ID      *struct {26		Number      string    `form:"id_number"`27		DateOfIssue time.Time `form:"id_date" time_format:"2006-01-02" time_utc:"true"`28	}29	Nationality *string `form:"nationality"`30}3132func BenchmarkMapFormFull(b *testing.B) {33	var s structFull34	for b.Loop() {35		err := mapForm(&s, form)36		if err != nil {37			b.Fatalf("Error on a form mapping")38		}39	}40	b.StopTimer()4142	t := b43	assert.Equal(t, "mike", s.Name)44	assert.Equal(t, 25, s.Age)45	assert.Equal(t, []string{"anna", "nicole"}, s.Friends)46	assert.Equal(t, "12345678", s.ID.Number)47	assert.Equal(t, time.Date(2018, 1, 20, 0, 0, 0, 0, time.UTC), s.ID.DateOfIssue)48	assert.Nil(t, s.Nationality)49}5051type structName struct {52	Name string `form:"name"`53}5455func BenchmarkMapFormName(b *testing.B) {56	var s structName57	for b.Loop() {58		err := mapForm(&s, form)59		if err != nil {60			b.Fatalf("Error on a form mapping")61		}62	}63	b.StopTimer()6465	t := b66	assert.Equal(t, "mike", s.Name)67}